From 9664408c6258c2df576807c4e758ce499eff035d Mon Sep 17 00:00:00 2001 From: Anton Shutik Date: Wed, 30 Oct 2024 19:57:49 +0100 Subject: [PATCH] Added page_size, query private --- shopify_client/graphql.py | 8 ++++---- tests/test_grapgql.py | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/shopify_client/graphql.py b/shopify_client/graphql.py index 392bebb..cb8482e 100644 --- a/shopify_client/graphql.py +++ b/shopify_client/graphql.py @@ -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(), @@ -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) diff --git a/tests/test_grapgql.py b/tests/test_grapgql.py index c56385b..987c833 100644 --- a/tests/test_grapgql.py +++ b/tests/test_grapgql.py @@ -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"}} @@ -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): @@ -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