Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 62ed99a

Browse files
authored
Merge pull request #3840 from magento/kh_query-variables
Updating search and pagination topic
2 parents 72995b8 + 2d7775c commit 62ed99a

File tree

6 files changed

+176
-40
lines changed

6 files changed

+176
-40
lines changed

_data/toc/graphql.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ pages:
1010
- label: GraphQL request headers
1111
url: /graphql/send-request.html
1212

13-
- label: Searches and pagination
14-
url: /graphql/search-pagination.html
13+
- label: Queries
14+
url: /graphql/queries.html
1515

1616
- label: Mutations
1717
url: /graphql/mutations.html

guides/v2.3/graphql/develop/create-graphqls-file.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ To illustrate how to configure the `schema.graphqls` file, let's suppose you hav
2323

2424
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.
2525

26-
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).
26+
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 [Queries]({{ page.baseurl }}/graphql/queries.html). The parameter definitions will be discussed in [Specify output attributes](#specify-output-attributes).
2727

2828
``` php
2929
type Query {

guides/v2.3/graphql/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ GraphiQL is an in-browser tool for writing, validating, and testing GraphQL quer
5151

5252
![GraphiQL browser]({{ page.baseurl }}/graphql/images/graphql-browser.png)
5353

54-
To begin using GraphiQL, set the GraphQL endpoint by entering `http://<magento2-3-server>/graphql` in the URL bar, then click **Set endpoint**. You can use the browser in the right column to determine how to set up a query. Additional examples are also available at [Searches and pagination in GraphQL]({{ page.baseurl }}/graphql/search-pagination.html).
54+
To begin using GraphiQL, set the GraphQL endpoint by entering `http://<magento2-3-server>/graphql` in the URL bar, then click **Set endpoint**. You can use the browser in the right column to determine how to set up a query. Additional examples are also available in [Queries]({{ page.baseurl }}/graphql/queries.html).
5555

5656

5757
{:.bs-callout .bs-callout-info}

guides/v2.3/graphql/mutations.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ title: Mutations
55

66
While GraphQL queries perform read operations, mutations change the data. A mutation can create, update, or delete objects and fields. In REST terminology, queries operate like `GET` requests, while mutations are similar to `POST`, `PUT`, and `DELETE`.
77

8+
## Structure of a mutation
9+
810
A mutation contains the following elements:
911

1012
* The keyword `mutation`
@@ -23,7 +25,7 @@ mutation myCreateCustomer{
2325
}
2426
```
2527

26-
In this example, `myCreateCustomer` identifies your implementation. `CustomerInput` is a non-nullable object that defines a customer. (The exclamation point indicates the value is non-nullable.) `CustomerOutput` defines which fields to return.
28+
In this example, `myCreateCustomer` identifies your implementation. `CustomerInput` is a non-nullable object that defines a customer. (The exclamation point indicates the value is non-nullable.) The `CustomerOutput` object defines which fields to return.
2729

2830
Now let's take a look at a fully-defined mutation. This time, we'll specify the minimum fields needed as input to create a customer (`firstname`, `lastname`, `email`, and `password`). We could include the same fields in the output, but GraphQL allows you to return only the data you need, which is the customer `id`.
2931

@@ -78,7 +80,7 @@ mutation myGenerateCustomerToken{
7880

7981
## Mutation variables
8082

81-
Specifying variables in a mutation can help increase code re-use. Consider the following steps when creating a mutation that contains one or more variables:
83+
Specifying variables in a mutation can help increase code re-use. Consider the following requirements when generating a mutation that contains one or more variables:
8284

8385
* All variables must be declared up-front, immediately after the operation name.
8486
* Variables are typed: they can be scalar or an object.

guides/v2.3/graphql/search-pagination.md renamed to guides/v2.3/graphql/queries.md

Lines changed: 162 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,205 @@
11
---
22
group: graphql
3-
title: Searches and pagination in GraphQL
3+
title: Queries
4+
redirect_from:
5+
- /guides/v2.3/graphql/search-pagination.html
46
---
57

6-
A GraphQL search query can contain the following components:
8+
A GraphQL query retrieves data from the Magento server in a similar manner as a REST GET call. The current set of Magento GraphQL queries allow a mobile app or browser to render a wide variety of information, including the following:
9+
10+
* A set of products to be displayed. This can include the entire catalog or those that match customer-specified criteria.
11+
* Customer data. With a customer token, a query can retrieve basic information about a customer as well as billing and shipping addresses, wishlists, order history, and other sensitive data.
12+
* Shopping cart contents. GraphQL supports both guest and logged-in customer carts.
13+
* Store configuration values, including theme and CMS settings, the currency code, and supported countries.
14+
15+
The Magento REST GET endpoints retrieve a wide variety of information on behalf of the merchant. Many of these endpoints are for retrieving backend information. For example, the `GET /V1/customers/search` endpoint can be used to find a subset of customers that meet certain criteria, such as those that live in a particular state or have a birthday this month. Likewise, the `GET /V1/invoices` endpoint can return all the recently-generated invoices. This type of functionality is not required for the frontend, so it is not available in GraphQL queries. The queries are designed to improve the customer's user experience by quickly retrieving the data needed to render pages.
16+
17+
Over time, the Magento GraphQL queries will duplicate the functionality of all storefront-facing GET calls, while making it possible to query more data in one request. The main difference will be that GraphQL will support storefront use cases, while REST will support admin use cases.
18+
19+
## Structure of a query
20+
21+
A query contains the following elements:
22+
23+
* The optional keyword `query`. If no keyword is specified at the beginning of a request, the processor assumes the request is a query.
24+
* An operation name for your local implementation. This name is required if you include variables. Otherwise, it is optional.
25+
* The query name
26+
* The terms to search for. The terms can be in the form of objects, attributes, or a combination. Queries that don't require search terms obtain their context from the customer's authorization token or store ID, both of which are specified in the header of the call.
27+
* The output object, which specifies which data the query returns.
28+
29+
The following example shows the structure of the `cart` query:
30+
31+
```text
32+
query myCartQuery{
33+
cart(cart_id: String!): Cart
34+
}
35+
```
36+
37+
In the preceding example, `myCartQuery` identifies your implementation of the `cart` query. `cart_id` is a non-nullable string that defines the cart to query. (The exclamation point indicates the value is non-nullable.) The `Cart` output object defines which fields to return.
38+
39+
Now let's fully define a query:
40+
41+
```text
42+
query myCartQuery{
43+
cart(cart_id: "1WxKm8WUm3uFKXLlHXezew5WREfVRPAn") {
44+
cart_id
45+
items {
46+
id
47+
qty
48+
}
49+
billing_address {
50+
firstname
51+
lastname
52+
postcode
53+
}
54+
shipping_addresses {
55+
firstname
56+
lastname
57+
postcode
58+
}
59+
}
60+
}
61+
```
62+
63+
In this example, we've supplied a cart ID as input, (which was generated by the `createEmptyCart` mutation). The output includes the `cart_id` as well as selected information about the items in the cart and the billing and shipping addresses.
64+
65+
The following example shows the query response:
66+
67+
```json
68+
{
69+
"data": {
70+
"cart": {
71+
"cart_id": "1WxKm8WUm3uFKXLlHXezew5WREfVRPAn",
72+
"items": [
73+
{
74+
"id": "5",
75+
"qty": 1
76+
}
77+
],
78+
"billing_address": {
79+
"firstname": "Veronica",
80+
"lastname": "Costello",
81+
"postcode": "49628-7978"
82+
},
83+
"shipping_addresses": [
84+
{
85+
"firstname": "Veronica",
86+
"lastname": "Costello",
87+
"postcode": "49628-7978"
88+
}
89+
]
90+
}
91+
}
92+
}
93+
```
94+
95+
{:.bs-callout .bs-callout-tip}
96+
Magento will not run a query that is too complex. The number of fields, objects, and nodes are factors in determining the complexity of a query.
97+
98+
## Query variables
99+
100+
Specifying variables in a query can help increase code re-use. Consider the following requirements when generating a query that contains one or more variables:
101+
102+
* All variables must be declared up-front, immediately after the operation name.
103+
* Variables are typed: they can be scalar or an object.
104+
* You must use all declared variables. Object variables are defined in JSON.
105+
106+
The following example declares the `$cart_id` variable. It is referenced in the `input` statement.
107+
108+
```text
109+
query myCartQueryWithVariable($cart_id: String!) {
110+
cart(cart_id: $cart_id) {
111+
cart_id
112+
items {
113+
id
114+
qty
115+
}
116+
billing_address {
117+
firstname
118+
lastname
119+
postcode
120+
}
121+
shipping_addresses {
122+
firstname
123+
lastname
124+
postcode
125+
}
126+
}
127+
}
128+
```
129+
130+
Variables are defined separately in JSON:
131+
132+
```json
133+
{
134+
"cart_id": "1WxKm8WUm3uFKXLlHXezew5WREfVRPAn"
135+
}
136+
```
137+
138+
## Product search queries
139+
140+
A product search query can contain the following components:
7141

8142
* A full text search keyword
9143
* Filters (search criteria)
10144
* Pagination information
11145
* Sorting instructions
12146

13-
## Specifying full text search keywords
147+
### Specifying full text search keywords
14148

15149
The `search` element causes Magento to perform a full text search on the specified keywords. (This is the same type of search that is performed from the storefront. If multiple keywords are specified, each keyword is evaluated separately.)
16150

17151
The `search` element is optional, but it can be used with or without filters. Each query must contain a `search` or `filter` element.
18152

19-
## Specifying filters
153+
### Specifying filters
20154

21155
The `filter` element defines which search criteria to use to find the desired results. As with a REST call, each filter defines the field to be searched, the condition type, and the search value.
22156

23157
Search filters are logically ANDed unless an `or` statement is specified. The search query can contain unlimited number of nested `or` clauses. However, you cannot perform a logical `or` across two AND clauses, such as (A AND B) OR (X AND Y).
24158

25-
### Search fields
159+
#### Search fields
26160

27161
Each object type defines which fields can be searched. See the object-specific documentation for details.
28162

29163
{:.bs-callout .bs-callout-info}
30164
You cannot specify the same search field twice in a GraphQL query.
31165

32-
### Condition types and search values
166+
#### Condition types and search values
33167

34168
The following table lists available condition types and provides the SQL statement equivalents.
35169

36170
Magento GraphQL clause | SQL equivalent
37171
--- | ---
38-
`eq: "value"` | <code><i>field</i> = 'value'</code>
172+
`eq: "value"` | <code><i>field</i> = 'value'</code>
39173
`neq: "value"` |<code><i>field</i> != 'value'</code>
40-
`like: "value%"` | <code><i>field</i> LIKE 'value%'</code>
41-
`nlike: "value%"` |<code><i>field</i> NOT LIKE 'value%'</code>
42-
`in: [1, 2, 3] ` | <code><i>field</i> IN (1, 2, 3)</code>
43-
`nin: [1, 2, 3]` | <code><i>field</i> NOT IN (1, 2, 3)</code>
44-
`notnull: true` | <code><i>field</i> IS NOT NULL</code>
45-
`null: true` | <code><i>field</i> IS NULL</code>
46-
`lt: "value"` | <code><i>field</i> < 'value'</code>
47-
`gt: "value"` | <code><i>field</i> > 'value'</code>
48-
`gteq: "value"` | <code><i>field</i> >= 'value'</code>
49-
`lteq: "value"` | <code><i>field</i> <= 'value'</code>
174+
`like: "value%"` | <code><i>field</i> LIKE 'value%'</code>
175+
`nlike: "value%"` |<code><i>field</i> NOT LIKE 'value%'</code>
176+
`in: [1, 2, 3]` | <code><i>field</i> IN (1, 2, 3)</code>
177+
`nin: [1, 2, 3]` | <code><i>field</i> NOT IN (1, 2, 3)</code>
178+
`notnull: true` | <code><i>field</i> IS NOT NULL</code>
179+
`null: true` | <code><i>field</i> IS NULL</code>
180+
`lt: "value"` | <code><i>field</i> < 'value'</code>
181+
`gt: "value"` | <code><i>field</i> > 'value'</code>
182+
`gteq: "value"` | <code><i>field</i> >= 'value'</code>
183+
`lteq: "value"` | <code><i>field</i> <= 'value'</code>
50184
`moreq: "value"` | <code><i>field</i> >= 'value'</code>
51185
`from: "value1"` `to: "value2"` | <code><i>field</i> BETWEEN 'value1' AND 'value2'</code>
52-
`finset: [1, 2, 3]` | <code>FINSET(<i>field</i>, '1, 2, 3')</code>
186+
`finset: [1, 2, 3]` | <code>FINSET(<i>field</i>, '1, 2, 3')</code>
53187

54188
`to` and `from` must always be used together. These condition types can be used in the same search term. For example, `qty: {from: "10" to: "20"}`.
55189

56190
`gt` and `lt` can be used in the same search term. For example, `qty: {gt: "10" lt: "20"}`.
57191

58-
## Specifying pagination
192+
### Specifying pagination
59193

60194
Magento's GraphQL implementation of pagination uses offsets so that it operates in the same manner as REST and SOAP API requests.
61195

62-
The `pageSize` attribute specifies the maximum number of items to return. If no value is specified, 20 items are returned.
196+
The `pageSize` attribute specifies the maximum number of items to return. If no value is specified, 20 items are returned.
63197

64198
The `currentPage` attribute specifies which page of results to return. If no value is specified, the first page is returned. If you specify a value that is greater than the number of available pages, an error is returned.
65199

66200
You can include the `total_pages` attribute in the `page_info` section of the output definition to indicate how many pages were returned for the query.
67201

68-
## Sorting instructions
202+
### Sorting instructions
69203

70204
The `sort` object allows you to specify which field or fields to use for sorting the results. If you specify more than one field, Magento sorts by the first field listed. Then, if any items have the same value, those items will be sorted by the secondary field. The value for each field can be set to either `ASC` or `DESC`.
71205

@@ -78,11 +212,11 @@ sort: {
78212
}
79213
```
80214

81-
## Example Searches
215+
### Example Product Searches
82216

83217
The following sections provide examples of each type of search. These examples use the Magento Open Source sample data.
84218

85-
### Full text search
219+
#### Full text search
86220

87221
The following search returns items that contain the word `yoga` or `pants`. The Catalog Search index contains search terms taken from the product `name`, `description`, `short_description` and related attributes.
88222

@@ -116,7 +250,7 @@ The following search returns items that contain the word `yoga` or `pants`. The
116250

117251
The search returns 45 items.
118252

119-
### Full text search with filters
253+
#### Full text search with filters
120254

121255
The following sample query returns a list of products that meets the following criteria:
122256

@@ -212,7 +346,7 @@ The query returns the following:
212346
}
213347
```
214348

215-
### Simple search using a timestamp
349+
#### Simple search using a timestamp
216350

217351
The following search finds all products that were added after the specified time (midnight, November 1, 2017).
218352

@@ -251,7 +385,7 @@ The following search finds all products that were added after the specified time
251385
}
252386
```
253387

254-
### Simple Logical OR search
388+
#### Simple Logical OR search
255389

256390
The following example searches for all products whose `sku` begins with the string `24-MB` or whose `name` ends with `Bag`.
257391

@@ -297,7 +431,7 @@ The following example searches for all products whose `sku` begins with the stri
297431

298432
The query returns 8 items.
299433

300-
### Logical AND and OR search
434+
#### Logical AND and OR search
301435

302436
This query searches for products that have `name` that ends with `Orange` or has a `sku` that indicates the product is a pair of women’s shorts in size 29 (`WSH%29%`). The system performs a logical AND to restrict the results to those that cost from $40 to $49.99.
303437

@@ -343,4 +477,4 @@ This query searches for products that have `name` that ends with `Orange` or has
343477
}
344478
```
345479

346-
The query returns 4 items.
480+
The query returns 4 items.

guides/v2.3/graphql/reference/products.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ Each query attribute is defined below:
2121

2222
Attribute | Description
2323
--- | ---
24-
`search` | Performs a full-text search using the specified key words. This attribute is optional. See [Searches and pagination in GraphQL]({{ page.baseurl }}/graphql/search-pagination.html) for more information.
24+
`search` | Performs a full-text search using the specified key words. This attribute is optional. See [Queries]({{ page.baseurl }}/graphql/queries.html) for more information.
2525
`filter` | Identifies which attributes to search for and return. This attribute is required. See [ProductFilterInput](#ProductFilterInput) for more information.
26-
`pageSize` | Specifies the maximum number of results to return at once. The default value is 20. See [Searches and pagination in GraphQL]({{ page.baseurl }}/graphql/search-pagination.html) for more information.
27-
`currentPage` | Specifies which page of results to return. The default value is 1. See [Searches and pagination in GraphQL]({{ page.baseurl }}/graphql/search-pagination.html) for more information.
28-
`sort` | Specifies which attribute to sort on, and whether to return the results in ascending or descending order. See [Searches and pagination in GraphQL]({{ page.baseurl }}/graphql/search-pagination.html) for more information.
26+
`pageSize` | Specifies the maximum number of results to return at once. The default value is 20. See [Queries]({{ page.baseurl }}/graphql/queries.html) for more information.
27+
`currentPage` | Specifies which page of results to return. The default value is 1. See [Queries]({{ page.baseurl }}/graphql/queries.html) for more information.
28+
`sort` | Specifies which attribute to sort on, and whether to return the results in ascending or descending order. See [Queries]({{ page.baseurl }}/graphql/queries.html) for more information.
2929
`Products` | An output object that contains the results of the query. See [Response](#Response) for details.
3030
{:style="table-layout:auto;"}
3131

@@ -44,7 +44,7 @@ filter: {
4444
}
4545
```
4646

47-
See [Searches and pagination in GraphQL]({{ page.baseurl }}/graphql/search-pagination.html) for more information about the operators.
47+
See [Queries]({{ page.baseurl }}/graphql/queries.html) for more information about the operators.
4848

4949
Magento processes the attribute values specified in a `ProductFilterInput` as simple data types (strings, integers, booleans). However, returned attributes can be a different, complex, data type. For example, in a response, `price` is an object that contains a monetary value and a currency code.
5050

@@ -378,7 +378,7 @@ Field | Type | Description
378378

379379
## Sample query
380380

381-
You can review several general interest `products` queries at [Searches and pagination in GraphQL]({{ page.baseurl }}/graphql/search-pagination.html).
381+
You can review several general interest `products` queries at [Queries]({{ page.baseurl }}/graphql/queries.html).
382382

383383
The following query returns layered navigation for products that have a `sku` containing the string `24-WB`.
384384

0 commit comments

Comments
 (0)