11import shopify
2- from ..base import ShopifyResource
32from six .moves import urllib
43import json
54
65
6+ class GraphQLException (Exception ):
7+ def __init__ (self , response ):
8+ self ._response = response
9+
10+ @property
11+ def errors (self ):
12+ return self ._response ['errors' ]
13+
14+
715class GraphQL :
816 def __init__ (self ):
917 self .endpoint = shopify .ShopifyResource .get_site () + "/graphql.json"
@@ -16,7 +24,6 @@ def merge_headers(self, *headers):
1624 return merged_headers
1725
1826 def execute (self , query , variables = None , operation_name = None ):
19- endpoint = self .endpoint
2027 default_headers = {"Accept" : "application/json" , "Content-Type" : "application/json" }
2128 headers = self .merge_headers (default_headers , self .headers )
2229 data = {"query" : query , "variables" : variables , "operationName" : operation_name }
@@ -25,8 +32,9 @@ def execute(self, query, variables=None, operation_name=None):
2532
2633 try :
2734 response = urllib .request .urlopen (req )
28- return response .read ().decode ("utf-8" )
35+ result = json .loads (response .read ().decode ("utf-8" ))
36+ if result .get ('errors' ):
37+ raise GraphQLException (result )
38+ return result
2939 except urllib .error .HTTPError as e :
30- print ((e .read ()))
31- print ("" )
32- raise e
40+ raise GraphQLException (json .load (e .fp ))
0 commit comments