You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each module that adds to or extends from a GraphQL schema can do so by placing a `schema.graphqls` file in its `etc` directory. Magento Core adds GraphQL modules based on the purpose of the schema being extended/added, and what core modules they depend on. For example, the CustomerGraphQl adds a query to the graphql endpoint to view customer data, and relies on the Customer core module
9
+
Each module that adds to or extends from a GraphQL schema can do so by placing a `schema.graphqls` file in its `etc` directory. Magento Core adds GraphQl modules based on the purpose of the schema being extended/added and the core modules they depend on. For example, the CustomerGraphQl module adds a query to the graphql endpoint to view customer data and relies on the Customer core module.
10
10
11
11
A GraphQL module's `schema.graphqls` file defines how the attributes defined in the module can be used in a GraphQL query. If your module's attributes are completely self-contained, then the `schema.graphqls` file defines the query, the interfaces used, the data types of all the attributes, and any enumerations that restrict the possible attribute contents. If your module simply extends another module (such as Catalog), then you must define those attributes and ensure that the other module can load your attributes.
12
12
@@ -17,15 +17,15 @@ The `<module_name>/etc/schema.graphqls` file:
17
17
* Points to the resolvers that verify and process the query data and response
18
18
* Serves as the source for displaying the schema in a GraphQL browser
19
19
20
-
The base `schema.graphqls` file, located in the `app/code/GraphQl/etc/` directory, provides the basic structure for all GraphQL queries, including definitions for comparison operators and paging information for search results. The `webonyx/graphql-php` library enforces the syntax of all `schema.graphqls` files.
20
+
The base `schema.graphqls` file, located in the `app/code/GraphQl/etc/` directory, provides the necessary structure for all GraphQL queries, including definitions for comparison operators and paging information for search results. The `webonyx/graphql-php` library enforces the syntax of all `schema.graphqls` files.
21
21
22
22
To illustrate how to configure the `schema.graphqls` file, let's suppose you have a module named Volumizer that calculates the volume of a product, given its height, width, and depth. We'll assume this module extends the Catalog module.
23
23
24
24
## Define the query
25
25
26
26
A query definition can be one line, or it can be complex. If your module's query implements `searchCriteria`, then you must define arguments that define filters and pagination information, all of which adds complexity. However, if you expect a single result from your query, then its definition can be simple.
27
27
28
-
The following example shows the `products` query. The `type` is defined as a `Query`. The `products` definitions define the keywords that are used to construct a query, as shown in [Searches and pagination in GraphQL]({{page.baseurl}}graphql/search-pagination.html). The parameter definitions will be discussed in [Specify output attributes](#specify-output-attributes)
28
+
The following example shows the `products` query. The `type` is defined as a `Query`. The `products` definitions define the keywords that are used to construct a query, as shown in [Searches and pagination in GraphQL]({{page.baseurl}}graphql/search-pagination.html). The parameter definitions will be discussed in [Specify output attributes](#specify-output-attributes).
29
29
30
30
```php
31
31
type Query {
@@ -51,7 +51,7 @@ If all your module's attributes are extension attributes for existing modules, t
51
51
52
52
## Declare input attributes
53
53
54
-
You must explicitly define each attribute that can be used as input in a GraphQL query. In the simplest cases, you can create a single `type`defintion that includes all the input, output, and sorting attributes for an object. This might not be possible if your module performs calculations, or otherwise has attributes that aren't available at the time of the query.
54
+
You must explicitly define each attribute that can be used as input in a GraphQL query. In the simplest cases, you can create a single `type`definition that includes all the input, output, and sorting attributes for an object. This might not be possible if your module performs calculations, or otherwise has attributes that aren't available at the time of the query.
55
55
56
56
The theoretical Volumizer module extends `Catalog`. In this case, you would reference `ProductFilterInput` as the source and make each attribute be of type `FilterTypeInput`. (Both of these entities are defined in `CatalogGraphQl`'s `schema.graphqls` file. In other use cases, you would be required to create your own input type.
57
57
@@ -67,7 +67,7 @@ input ProductFilterInput {
67
67
68
68
## Specify sorting attributes
69
69
70
-
You can also define which attributes can be used for sorting the search results. This list does not not have to be identical to the list of input or output attributes.
70
+
You can also define which attributes can be used for sorting the search results. This list does not have to be identical to the list of input or output attributes.
71
71
72
72
This example allows sorting on the `v_volume` attribute only.
73
73
@@ -85,11 +85,11 @@ input ProductSortInput {
85
85
86
86
You must know the data type of each attribute, whether it is scalar or an object, and whether it can be part of an array. In addition, each attribute within an object must be defined in the same manner.
87
87
88
-
In an`schema.graphqls` file, the output `Interface` defines top-level attributes. Each object returned is defined in a `type` definition.
88
+
In a`schema.graphqls` file, the output `Interface` defines top-level attributes. Each object returned is defined in a `type` definition.
89
89
90
90
### Define the output interface
91
91
92
-
In many cases, the response contains data that was either not available as input, or it was transformed in some manner from the input. For example, when you specify a price in an input filter, Magento evaluates it as a Float value. However, `Price` output objects contain a Float value, a currency value, and possibly minimum/maximum values and tax adjustments. You can define a `typeResolver` to point to the Resolver object, which interprets the GraphQL query. If your module contains only attributes that extend another module, then this parameter is optional. Otherwise, it is required. See [Resolvers]({{page.baseurl}}graphql/resolvers.html) for more information.
92
+
In many cases, the response contains data that was either not available as input, or was transformed in some manner from the input. For example, when you specify a price in an input filter, Magento evaluates it as a Float value. However, `Price` output objects contain a Float value, a currency value, and possibly minimum/maximum values and tax adjustments. You can define a `typeResolver` to point to the Resolver object, which interprets the GraphQL query. If your module contains only attributes that extend another module, then this parameter is optional. Otherwise, it is required. See [Resolvers]({{page.baseurl}}graphql/resolvers.html) for more information.
93
93
94
94
The following example defines module-specific output attributes for the Volumizer module.
95
95
@@ -130,7 +130,7 @@ enum VolumeUnitEnum {
130
130
131
131
## Annotations
132
132
133
-
You can provide a description of any attribute, type definition, or other entity within a `schema.graphqls` file by appending the following to the line:
133
+
You can describe any attribute, type definition, or other entity within a `schema.graphqls` file by appending the following to the line:
Copy file name to clipboardExpand all lines: guides/v2.3/graphql/index.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -18,8 +18,8 @@ Most of the development team's work thus far has been devoted to building the Gr
18
18
GraphQL allows you to define the structure of the data that you need, and the server returns only the data you request. Each GraphQL-capable module contains a declarative schema that defines the syntax for queries that the module supports, as well as the attributes that can be returned. If you run a REST call such as `GET /V1/products/:sku` on a simple product, the system might fetch more than 100 lines of data. If all you need is the current price, the call has returned significantly more information than you need. With GraphQL, a query against the same SKU could return just the price.
19
19
20
20
A GraphQL-enabled module handles externally-defined attributes differently than other Magento modules. We used the following techniques to manage product-related attributes, but you are free to use alternate methods:
21
-
`
22
-
***EAV attributes** are explicitly declared in the `schema.graphqls files.
21
+
22
+
***EAV attributes** are explicitly declared in the `schema.graphqls` files.
23
23
***Custom attributes** are treated as dynamic attributes that might or might not be present. Therefore, they are not declared in the schema. Instead, we've implemented a reader that queries the database and gets any declared custom attributes. These attributes can be declared in the schema if you know they'll always be present.
24
24
***Extension attributes** can be declared in a `schema.graphqls` file or by a custom reader, but they should be declared in a separate `*GraphQl` module. The attributes should extend from the resolver that fetches that model's data.
0 commit comments