1
1
import json
2
2
from promise import Promise
3
+ from collections import namedtuple
3
4
4
5
import six
5
6
from flask import Response , request
15
16
from .render_graphiql import render_graphiql
16
17
17
18
19
+ GraphQLParams = namedtuple ('GraphQLParams' , 'query,variables,operation_name,id' )
20
+
18
21
class HttpQueryError (Exception ):
19
22
def __init__ (self , status_code , message = None , is_graphql_error = False , headers = None ):
20
23
self .status_code = status_code
@@ -61,11 +64,12 @@ def get_middleware(self, request):
61
64
def get_executor (self , request ):
62
65
return self .executor
63
66
64
- def render_graphiql (self , ** kwargs ):
67
+ def render_graphiql (self , params , result ):
65
68
return render_graphiql (
69
+ params = params ,
70
+ result = result ,
66
71
graphiql_version = self .graphiql_version ,
67
72
graphiql_template = self .graphiql_template ,
68
- ** kwargs
69
73
)
70
74
71
75
def dispatch_request (self ):
@@ -115,11 +119,9 @@ def dispatch_request(self):
115
119
result = self .json_encode (response , pretty )
116
120
117
121
if show_graphiql :
118
- query , variables , operation_name , id = self .get_graphql_params (data [0 ])
122
+ params = self .get_graphql_params (data [0 ])
119
123
return self .render_graphiql (
120
- query = query ,
121
- variables = variables ,
122
- operation_name = operation_name ,
124
+ params = params ,
123
125
result = result
124
126
)
125
127
@@ -140,23 +142,21 @@ def dispatch_request(self):
140
142
)
141
143
142
144
def get_response (self , execute , data , show_graphiql = False , only_allow_query = False ):
143
- query , variables , operation_name , id = self .get_graphql_params (data )
145
+ params = self .get_graphql_params (data )
144
146
try :
145
147
execution_result = self .execute_graphql_request (
146
148
self .schema ,
147
149
execute ,
148
150
data ,
149
- query ,
150
- variables ,
151
- operation_name ,
151
+ params ,
152
152
only_allow_query ,
153
153
)
154
154
except HttpQueryError :
155
155
if show_graphiql :
156
156
execution_result = None
157
157
else :
158
158
raise
159
- return self .format_execution_result (execution_result , id , self .format_error )
159
+ return self .format_execution_result (execution_result , params . id , self .format_error )
160
160
161
161
@staticmethod
162
162
def format_execution_result (execution_result , id , format_error ):
@@ -223,12 +223,12 @@ def execute(self, schema, *args, **kwargs):
223
223
)
224
224
225
225
@staticmethod
226
- def execute_graphql_request (schema , execute , data , query , variables , operation_name , only_allow_query = False ):
227
- if not query :
226
+ def execute_graphql_request (schema , execute , data , params , only_allow_query = False ):
227
+ if not params . query :
228
228
raise HttpQueryError (400 , 'Must provide query string.' )
229
229
230
230
try :
231
- source = Source (query , name = 'GraphQL request' )
231
+ source = Source (params . query , name = 'GraphQL request' )
232
232
ast = parse (source )
233
233
validation_errors = validate (schema , ast )
234
234
if validation_errors :
@@ -240,7 +240,7 @@ def execute_graphql_request(schema, execute, data, query, variables, operation_n
240
240
return ExecutionResult (errors = [e ], invalid = True )
241
241
242
242
if only_allow_query :
243
- operation_ast = get_operation_ast (ast , operation_name )
243
+ operation_ast = get_operation_ast (ast , params . operation_name )
244
244
if operation_ast and operation_ast .operation != 'query' :
245
245
raise HttpQueryError (
246
246
405 ,
@@ -254,8 +254,8 @@ def execute_graphql_request(schema, execute, data, query, variables, operation_n
254
254
return execute (
255
255
schema ,
256
256
ast ,
257
- operation_name = operation_name ,
258
- variable_values = variables ,
257
+ operation_name = params . operation_name ,
258
+ variable_values = params . variables ,
259
259
)
260
260
except Exception as e :
261
261
return ExecutionResult (errors = [e ], invalid = True )
@@ -285,20 +285,22 @@ def request_wants_html(self):
285
285
request .accept_mimetypes ['application/json' ]
286
286
287
287
@staticmethod
288
- def get_graphql_params (data ):
289
- query = data .get ('query' )
290
- variables = data .get ('variables' )
291
- id = data .get ('id' )
292
-
288
+ def get_variables (variables ):
293
289
if variables and isinstance (variables , six .text_type ):
294
290
try :
295
- variables = json .loads (variables )
291
+ return json .loads (variables )
296
292
except :
297
293
raise HttpQueryError (400 , 'Variables are invalid JSON.' )
294
+ return variables
298
295
296
+ @classmethod
297
+ def get_graphql_params (cls , data ):
298
+ query = data .get ('query' )
299
+ variables = cls .get_variables (data .get ('variables' ))
300
+ id = data .get ('id' )
299
301
operation_name = data .get ('operationName' )
300
302
301
- return query , variables , operation_name , id
303
+ return GraphQLParams ( query , variables , operation_name , id )
302
304
303
305
@staticmethod
304
306
def format_error (error ):
0 commit comments