2
2
from collections import namedtuple , MutableMapping
3
3
4
4
import 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
8
7
from graphql .execution import ExecutionResult
9
- from graphql .utils .get_operation_ast import get_operation_ast
10
8
11
9
from .error import HttpQueryError
12
10
@@ -19,13 +17,6 @@ class SkipException(Exception):
19
17
GraphQLResponse = namedtuple ('GraphQLResponse' , 'result,status_code' )
20
18
21
19
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
-
29
20
def run_http_query (schema , request_method , data , query_data = None , batch_enabled = False , catch = False , ** execute_options ):
30
21
if request_method not in ('get' , 'post' ):
31
22
raise HttpQueryError (
@@ -110,15 +101,15 @@ def load_json_variables(variables):
110
101
if variables and isinstance (variables , six .string_types ):
111
102
try :
112
103
return json .loads (variables )
113
- except :
104
+ except Exception :
114
105
raise HttpQueryError (400 , 'Variables are invalid JSON.' )
115
106
return variables
116
107
117
108
118
109
def get_graphql_params (data , query_data ):
119
110
query = data .get ('query' ) or query_data .get ('query' )
120
111
variables = data .get ('variables' ) or query_data .get ('variables' )
121
- # id = data.get('id ')
112
+ # document_id = data.get('documentId ')
122
113
operation_name = data .get ('operationName' ) or query_data .get ('operationName' )
123
114
124
115
return GraphQLParams (query , load_json_variables (variables ), operation_name )
@@ -159,51 +150,58 @@ def format_execution_result(execution_result, format_error):
159
150
return GraphQLResponse (response , status_code )
160
151
161
152
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 ):
163
154
if not params .query :
164
155
raise HttpQueryError (400 , 'Must provide query string.' )
165
156
166
157
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 )
175
161
except Exception as e :
176
162
return ExecutionResult (errors = [e ], invalid = True )
177
163
178
164
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' :
181
167
raise HttpQueryError (
182
168
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 ),
184
170
headers = {
185
171
'Allow' : 'POST' ,
186
172
}
187
173
)
188
174
189
175
try :
190
- return execute (
191
- schema ,
192
- ast ,
176
+ return document .execute (
193
177
operation_name = params .operation_name ,
194
- variable_values = params .variables ,
178
+ variables = params .variables ,
195
179
** kwargs
196
180
)
197
-
198
181
except Exception as e :
199
182
return ExecutionResult (errors = [e ], invalid = True )
200
183
201
184
202
185
def load_json_body (data ):
203
186
try :
204
187
return json .loads (data )
205
- except :
188
+ except Exception :
206
189
raise HttpQueryError (
207
190
400 ,
208
191
'POST body sent invalid JSON.'
209
192
)
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