Skip to content

Commit

Permalink
Added queries dir support
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Shutik committed Oct 31, 2024
1 parent a8e0a0f commit 652b366
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
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
20 changes: 16 additions & 4 deletions shopify_client/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,31 @@

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"

with open(f"{self.grapgql_queries_dir}/{name}.graphql") 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 +57,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 652b366

Please sign in to comment.