Skip to content

Commit

Permalink
Added page_size, query private
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Shutik committed Oct 30, 2024
1 parent 3e84645 commit 9664408
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions shopify_client/graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def __build_url(self, **params):
return self.endpoint

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

def query(self, query, variables=None, operation_name=None, paginate=False):
def __query(self, query, variables=None, operation_name=None, paginate=False, page_size=100):
if paginate:
return self.__paginate(query, variables, operation_name)
return self.__paginate(query, variables, operation_name, page_size)
try:
response = self.client.post(
self.__build_url(),
Expand All @@ -45,7 +45,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, variables, 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
12 changes: 6 additions & 6 deletions tests/test_grapgql.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ def graphql(mock_client):

def test_graphql_query(graphql, mock_client):
mock_client.post.return_value = {"data": {"key": "value"}}
response = graphql.query(query="query { key }")
response = graphql(query="query { key }")
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_variables(graphql, mock_client):
mock_client.post.return_value = {"data": {"key": "value"}}
variables = {"var1": "value1"}
response = graphql.query(query="query { key }", variables=variables)
response = graphql(query="query { key }", variables=variables)
mock_client.post.assert_called_once_with("graphql.json", json={"query": "query { key }", "variables": variables, "operationName": None})
assert response == {"data": {"key": "value"}}

Expand All @@ -27,19 +27,19 @@ def test_query_paginated(graphql, mock_client):
{"data": {"pageInfo": {"hasNextPage": True, "endCursor": "cursor1"}}},
{"data": {"pageInfo": {"hasNextPage": False}}}
]
results = list(graphql.query(query="query { pageInfo { hasNextPage, endCursor } }", paginate=True))
results = list(graphql(query="query { pageInfo { hasNextPage, endCursor } }", paginate=True))
assert len(results) == 2
assert results[0] == {"data": {"pageInfo": {"hasNextPage": True, "endCursor": "cursor1"}}}
assert results[1] == {"data": {"pageInfo": {"hasNextPage": False}}}

def test_query_handles_http_error(graphql, mock_client, mocker):
mock_client.post.side_effect = requests.exceptions.HTTPError("HTTP Error")
response = graphql.query(query="query { key }")
response = graphql(query="query { key }")
assert response == {}

def test_query_handles_json_error(graphql, mock_client):
mock_client.post.side_effect = json.JSONDecodeError("JSON Decode Error", "", 0)
response = graphql.query(query="query { key }")
response = graphql(query="query { key }")
assert response == {}

def test_graphql_call(graphql, mock_client):
Expand Down Expand Up @@ -85,7 +85,7 @@ def __call__(self, *args, **kwargs):
return super(CopyingMock, self).__call__(*args, **kwargs)

mock_client.post = CopyingMock(side_effect = [mock_paginated_response_1, mock_paginated_response_2])
response = list(graphql.query(query="query { items { id } pageInfo { hasNextPage, endCursor } }", paginate=True))
response = list(graphql(query="query { items { id } pageInfo { hasNextPage, endCursor } }", paginate=True))

assert response == [mock_paginated_response_1, mock_paginated_response_2]
assert mock_client.post.call_count == 2
Expand Down

0 comments on commit 9664408

Please sign in to comment.