Skip to content

Commit

Permalink
Merge pull request #5 from groveco/added-queries-dir
Browse files Browse the repository at this point in the history
Added queries dir support
  • Loading branch information
Anton-Shutik authored Oct 31, 2024
2 parents a8e0a0f + 7712e3f commit 5248d61
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ order = client.orders.close(resource_id=1234)
### GraphQL API

```python
# List products
query = '''
# Initialize the client
client = ShopifyClient(api_url='your_api_url', api_token='your_token', api_version='your_api_version', grapgql_queries_dir="queries")

# queries/listProducts.graphql
query products($page_size: Int = 100) {
products(first: $page_size) {
nodes {
Expand All @@ -72,8 +74,10 @@ query products($page_size: Int = 100) {
}
}
}
'''
response = client.query(query)


# List products
response = client.query(query_name="listProducts")

# Limit page size
response = client.query(query, variables={"page_size": 20})
Expand All @@ -95,7 +99,7 @@ query products($page_size: Int = 100, $cursor: String) {
}
}
'''
for page in client.query(query, paginate=True)
for page in client.query(query=query, paginate=True)
print(page)
```

4 changes: 2 additions & 2 deletions shopify_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class ShopifyClient(requests.Session):

def __init__(self, api_url, api_token, api_version=SHOPIFY_API_VERSION):
def __init__(self, api_url, api_token, api_version=SHOPIFY_API_VERSION, grapgql_queries_dir=None):
super().__init__()
self.api_url = api_url
self.api_version = api_version
Expand Down Expand Up @@ -109,7 +109,7 @@ def __init__(self, api_url, api_token, api_version=SHOPIFY_API_VERSION):
self.webhooks = Endpoint(client=self, endpoint="webhooks")

# GraphQL
self.query = GraphQL(client=self)
self.query = GraphQL(client=self, grapgql_queries_dir=grapgql_queries_dir)

self.hooks["response"].append(rate_limit)

Expand Down
22 changes: 18 additions & 4 deletions shopify_client/graphql.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
import os

import requests

Expand All @@ -8,19 +9,32 @@

class GraphQL:

def __init__(self, client):
def __init__(self, client, grapgql_queries_dir=None):
self.client = client
self.endpoint = "graphql.json"
self.grapgql_queries_dir = grapgql_queries_dir

def __build_url(self, **params):
return self.endpoint

def __call__(self, *args, **kwargs):
return self.__query(*args, **kwargs)

def query_from_name(self, name):
assert self.grapgql_queries_dir, "GraphQL queries directory is not set"

query_path = os.path.join(self.grapgql_queries_dir, f"{name}.graphql")
with open(query_path, "r") as f:
return f.read()

def __query(self, query=None, query_name=None, variables=None, operation_name=None, paginate=False, page_size=100):
assert query or query_name, "Either 'query' or 'query_name' must be provided"

def __query(self, query, variables=None, operation_name=None, paginate=False, page_size=100):
if query is None and query_name:
query = self.query_from_name(query_name)

if paginate:
return self.__paginate(query, variables, operation_name, page_size)
return self.__paginate(query=query, variables=variables, operation_name=operation_name, page_size=page_size)
try:
response = self.client.post(
self.__build_url(),
Expand All @@ -45,7 +59,7 @@ def __paginate(self, query, variables=None, operation_name=None, page_size=100):

while has_next_page:
variables["cursor"] = cursor
response = self.__query(query, variables, operation_name)
response = self.__query(query=query, variables=variables, operation_name=operation_name)
page_info = self.__find_page_info(response)
has_next_page = page_info.get("hasNextPage", False)
cursor = page_info.get("endCursor", None)
Expand Down
14 changes: 13 additions & 1 deletion tests/test_graphql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from copy import deepcopy
import json
from unittest.mock import call
from unittest.mock import MagicMock, call, mock_open, patch
import requests
import pytest
from shopify_client.graphql import GraphQL
Expand All @@ -16,6 +16,18 @@ def test_graphql_query(graphql, mock_client):
mock_client.post.assert_called_once_with("graphql.json", json={"query": "query { key }", "variables": None, "operationName": None})
assert response == {"data": {"key": "value"}}

def test_graphql_query_with_query_name(graphql, mock_client):
mock_query_content = "query { items { id } }"
graphql.grapgql_queries_dir = "queries"
with patch("builtins.open", mock_open(read_data=mock_query_content)):
mock_client.post.return_value = {"data": {"items": []}}
response = graphql(query_name="test_query")
mock_client.post.assert_called_once_with(
"graphql.json",
json={"query": mock_query_content, "variables": None, "operationName": None},
)
assert response == {"data": {"items": []}}

def test_graphql_query_with_variables(graphql, mock_client):
mock_client.post.return_value = {"data": {"key": "value"}}
variables = {"var1": "value1"}
Expand Down

0 comments on commit 5248d61

Please sign in to comment.