22from collections import namedtuple , MutableMapping
33
44import six
5- from graphql import Source , execute , parse , validate
6- from graphql .error import format_error as format_graphql_error
7- from graphql .error import GraphQLError
5+ from graphql import get_default_backend
6+ from graphql .error import format_error as default_format_error
87from graphql .execution import ExecutionResult
9- from graphql .utils .get_operation_ast import get_operation_ast
108
119from .error import HttpQueryError
1210
@@ -19,13 +17,6 @@ class SkipException(Exception):
1917GraphQLResponse = namedtuple ('GraphQLResponse' , 'result,status_code' )
2018
2119
22- def default_format_error (error ):
23- if isinstance (error , GraphQLError ):
24- return format_graphql_error (error )
25-
26- return {'message' : six .text_type (error )}
27-
28-
2920def run_http_query (schema , request_method , data , query_data = None , batch_enabled = False , catch = False , ** execute_options ):
3021 if request_method not in ('get' , 'post' ):
3122 raise HttpQueryError (
@@ -110,15 +101,15 @@ def load_json_variables(variables):
110101 if variables and isinstance (variables , six .string_types ):
111102 try :
112103 return json .loads (variables )
113- except :
104+ except Exception :
114105 raise HttpQueryError (400 , 'Variables are invalid JSON.' )
115106 return variables
116107
117108
118109def get_graphql_params (data , query_data ):
119110 query = data .get ('query' ) or query_data .get ('query' )
120111 variables = data .get ('variables' ) or query_data .get ('variables' )
121- # id = data.get('id ')
112+ # document_id = data.get('documentId ')
122113 operation_name = data .get ('operationName' ) or query_data .get ('operationName' )
123114
124115 return GraphQLParams (query , load_json_variables (variables ), operation_name )
@@ -159,51 +150,58 @@ def format_execution_result(execution_result, format_error):
159150 return GraphQLResponse (response , status_code )
160151
161152
162- def execute_graphql_request (schema , params , allow_only_query = False , ** kwargs ):
153+ def execute_graphql_request (schema , params , allow_only_query = False , backend = None , ** kwargs ):
163154 if not params .query :
164155 raise HttpQueryError (400 , 'Must provide query string.' )
165156
166157 try :
167- source = Source (params .query , name = 'GraphQL request' )
168- ast = parse (source )
169- validation_errors = validate (schema , ast )
170- if validation_errors :
171- return ExecutionResult (
172- errors = validation_errors ,
173- invalid = True ,
174- )
158+ if not backend :
159+ backend = get_default_backend ()
160+ document = backend .document_from_string (schema , params .query )
175161 except Exception as e :
176162 return ExecutionResult (errors = [e ], invalid = True )
177163
178164 if allow_only_query :
179- operation_ast = get_operation_ast ( ast , params .operation_name )
180- if operation_ast and operation_ast . operation != 'query' :
165+ operation_type = document . get_operation_type ( params .operation_name )
166+ if operation_type and operation_type != 'query' :
181167 raise HttpQueryError (
182168 405 ,
183- 'Can only perform a {} operation from a POST request.' .format (operation_ast . operation ),
169+ 'Can only perform a {} operation from a POST request.' .format (operation_type ),
184170 headers = {
185171 'Allow' : 'POST' ,
186172 }
187173 )
188174
189175 try :
190- return execute (
191- schema ,
192- ast ,
176+ return document .execute (
193177 operation_name = params .operation_name ,
194- variable_values = params .variables ,
178+ variables = params .variables ,
195179 ** kwargs
196180 )
197-
198181 except Exception as e :
199182 return ExecutionResult (errors = [e ], invalid = True )
200183
201184
202185def load_json_body (data ):
203186 try :
204187 return json .loads (data )
205- except :
188+ except Exception :
206189 raise HttpQueryError (
207190 400 ,
208191 'POST body sent invalid JSON.'
209192 )
193+
194+
195+ __all__ = [
196+ 'default_format_error' ,
197+ 'SkipException' ,
198+ 'run_http_query' ,
199+ 'encode_execution_results' ,
200+ 'json_encode' ,
201+ 'load_json_variables' ,
202+ 'get_graphql_params' ,
203+ 'get_response' ,
204+ 'format_execution_result' ,
205+ 'execute_graphql_request' ,
206+ 'load_json_body'
207+ ]
0 commit comments