Skip to content

[Feature] Return dictionary and raise GraphQLException on error from GraphQL query #774

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
== Unreleased

- Return dictionary and raise GraphQLException on error from GraphQL query
- Remove requirement to provide scopes to Permission URL, as it should be omitted if defined with the TOML file.

== Version 12.7.0
Expand Down
20 changes: 14 additions & 6 deletions shopify/resources/graphql.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import shopify
from ..base import ShopifyResource
from six.moves import urllib
import json


class GraphQLException(Exception):
def __init__(self, response):
self._response = response

@property
def errors(self):
return self._response['errors']


class GraphQL:
def __init__(self):
self.endpoint = shopify.ShopifyResource.get_site() + "/graphql.json"
Expand All @@ -16,7 +24,6 @@ def merge_headers(self, *headers):
return merged_headers

def execute(self, query, variables=None, operation_name=None):
endpoint = self.endpoint
default_headers = {"Accept": "application/json", "Content-Type": "application/json"}
headers = self.merge_headers(default_headers, self.headers)
data = {"query": query, "variables": variables, "operationName": operation_name}
Expand All @@ -25,8 +32,9 @@ def execute(self, query, variables=None, operation_name=None):

try:
response = urllib.request.urlopen(req)
return response.read().decode("utf-8")
result = json.loads(response.read().decode("utf-8"))
if result.get('errors'):
raise GraphQLException(result)
return result
except urllib.error.HTTPError as e:
print((e.read()))
print("")
raise e
raise GraphQLException(json.load(e.fp))
5 changes: 2 additions & 3 deletions test/graphql_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import shopify
import json
from test.test_helper import TestCase


Expand Down Expand Up @@ -31,7 +30,7 @@ def test_fetch_shop_with_graphql(self):
}
"""
result = self.client.execute(query)
self.assertTrue(json.loads(result)["shop"]["name"] == "Apple Computers")
self.assertTrue(result["shop"]["name"] == "Apple Computers")

def test_specify_operation_name(self):
query = """
Expand All @@ -43,4 +42,4 @@ def test_specify_operation_name(self):
}
"""
result = self.client.execute(query, operation_name="GetShop")
self.assertTrue(json.loads(result)["shop"]["name"] == "Apple Computers")
self.assertTrue(result["shop"]["name"] == "Apple Computers")