File tree Expand file tree Collapse file tree 3 files changed +57
-6
lines changed Expand file tree Collapse file tree 3 files changed +57
-6
lines changed Original file line number Diff line number Diff line change @@ -183,6 +183,45 @@ This library also supports Shopify's new [GraphQL API](https://help.shopify.com/
183
183
result = shopify.GraphQL().execute(' { shop { name id } }' )
184
184
```
185
185
186
+ You can perform more complex operations using the ` variables ` and ` operation_name ` parameters of ` execute ` .
187
+
188
+ For example, this GraphQL document uses a fragment to construct two named queries - one for a single order, and one for multiple orders:
189
+
190
+ ``` graphql
191
+ # ./order_queries.graphql
192
+
193
+ fragment OrderInfo on Order {
194
+ id
195
+ name
196
+ createdAt
197
+ }
198
+
199
+ query GetOneOrder ($order_id : ID ! ){
200
+ node (id : $order_id ){
201
+ ... OrderInfo
202
+ }
203
+ }
204
+
205
+ query GetManyOrders ($order_ids : [ID ]! ){
206
+ nodes (ids : $order_ids ){
207
+ ... OrderInfo
208
+ }
209
+ }
210
+ ```
211
+
212
+ Now you can choose which operation to execute:
213
+
214
+ ``` python
215
+ # Load the document with both queries
216
+ document = Path(" ./order_queries.graphql" ).read_text()
217
+
218
+ # Specify the named operation to execute, and the parameters for the query
219
+ result = shopify.GraphQL().execute(
220
+ query = document,
221
+ variables = {" order_id" : " gid://shopify/Order/12345" },
222
+ operation_name = " GetOneOrder" ,
223
+ )
224
+ ```
186
225
187
226
## Using Development Version
188
227
Original file line number Diff line number Diff line change @@ -15,11 +15,11 @@ def merge_headers(self, *headers):
15
15
merged_headers .update (header )
16
16
return merged_headers
17
17
18
- def execute (self , query , variables = None ):
18
+ def execute (self , query , variables = None , operation_name = None ):
19
19
endpoint = self .endpoint
20
20
default_headers = {"Accept" : "application/json" , "Content-Type" : "application/json" }
21
21
headers = self .merge_headers (default_headers , self .headers )
22
- data = {"query" : query , "variables" : variables }
22
+ data = {"query" : query , "variables" : variables , "operationName" : operation_name }
23
23
24
24
req = urllib .request .Request (self .endpoint , json .dumps (data ).encode ("utf-8" ), headers )
25
25
Original file line number Diff line number Diff line change @@ -9,7 +9,7 @@ def setUp(self):
9
9
shopify .ApiVersion .define_known_versions ()
10
10
shopify_session = shopify .Session ("this-is-my-test-show.myshopify.com" , "unstable" , "token" )
11
11
shopify .ShopifyResource .activate_session (shopify_session )
12
- client = shopify .GraphQL ()
12
+ self . client = shopify .GraphQL ()
13
13
self .fake (
14
14
"graphql" ,
15
15
method = "POST" ,
@@ -20,6 +20,8 @@ def setUp(self):
20
20
"Content-Type" : "application/json" ,
21
21
},
22
22
)
23
+
24
+ def test_fetch_shop_with_graphql (self ):
23
25
query = """
24
26
{
25
27
shop {
@@ -28,7 +30,17 @@ def setUp(self):
28
30
}
29
31
}
30
32
"""
31
- self .result = client .execute (query )
33
+ result = self .client .execute (query )
34
+ self .assertTrue (json .loads (result )["shop" ]["name" ] == "Apple Computers" )
32
35
33
- def test_fetch_shop_with_graphql (self ):
34
- self .assertTrue (json .loads (self .result )["shop" ]["name" ] == "Apple Computers" )
36
+ def test_specify_operation_name (self ):
37
+ query = """
38
+ query GetShop{
39
+ shop {
40
+ name
41
+ id
42
+ }
43
+ }
44
+ """
45
+ result = self .client .execute (query , operation_name = "GetShop" )
46
+ self .assertTrue (json .loads (result )["shop" ]["name" ] == "Apple Computers" )
You can’t perform that action at this time.
0 commit comments