Skip to content

Commit 9b3981e

Browse files
authored
Merge pull request #571 from JJSphar/master
Add operation_name parameter to Graphql.execute
2 parents b9d89e7 + 1a35343 commit 9b3981e

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,45 @@ This library also supports Shopify's new [GraphQL API](https://help.shopify.com/
183183
result = shopify.GraphQL().execute('{ shop { name id } }')
184184
```
185185

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+
```
186225

187226
## Using Development Version
188227

shopify/resources/graphql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ def merge_headers(self, *headers):
1515
merged_headers.update(header)
1616
return merged_headers
1717

18-
def execute(self, query, variables=None):
18+
def execute(self, query, variables=None, operation_name=None):
1919
endpoint = self.endpoint
2020
default_headers = {"Accept": "application/json", "Content-Type": "application/json"}
2121
headers = self.merge_headers(default_headers, self.headers)
22-
data = {"query": query, "variables": variables}
22+
data = {"query": query, "variables": variables, "operationName": operation_name}
2323

2424
req = urllib.request.Request(self.endpoint, json.dumps(data).encode("utf-8"), headers)
2525

test/graphql_test.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def setUp(self):
99
shopify.ApiVersion.define_known_versions()
1010
shopify_session = shopify.Session("this-is-my-test-show.myshopify.com", "unstable", "token")
1111
shopify.ShopifyResource.activate_session(shopify_session)
12-
client = shopify.GraphQL()
12+
self.client = shopify.GraphQL()
1313
self.fake(
1414
"graphql",
1515
method="POST",
@@ -20,6 +20,8 @@ def setUp(self):
2020
"Content-Type": "application/json",
2121
},
2222
)
23+
24+
def test_fetch_shop_with_graphql(self):
2325
query = """
2426
{
2527
shop {
@@ -28,7 +30,17 @@ def setUp(self):
2830
}
2931
}
3032
"""
31-
self.result = client.execute(query)
33+
result = self.client.execute(query)
34+
self.assertTrue(json.loads(result)["shop"]["name"] == "Apple Computers")
3235

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")

0 commit comments

Comments
 (0)