11import json
22from promise import Promise
3+ from collections import namedtuple
34
45import six
56from flask import Response , request
1516from .render_graphiql import render_graphiql
1617
1718
19+ GraphQLParams = namedtuple ('GraphQLParams' , 'query,variables,operation_name,id' )
20+
1821class HttpQueryError (Exception ):
1922 def __init__ (self , status_code , message = None , is_graphql_error = False , headers = None ):
2023 self .status_code = status_code
@@ -61,11 +64,12 @@ def get_middleware(self, request):
6164 def get_executor (self , request ):
6265 return self .executor
6366
64- def render_graphiql (self , ** kwargs ):
67+ def render_graphiql (self , params , result ):
6568 return render_graphiql (
69+ params = params ,
70+ result = result ,
6671 graphiql_version = self .graphiql_version ,
6772 graphiql_template = self .graphiql_template ,
68- ** kwargs
6973 )
7074
7175 def dispatch_request (self ):
@@ -115,11 +119,9 @@ def dispatch_request(self):
115119 result = self .json_encode (response , pretty )
116120
117121 if show_graphiql :
118- query , variables , operation_name , id = self .get_graphql_params (data [0 ])
122+ params = self .get_graphql_params (data [0 ])
119123 return self .render_graphiql (
120- query = query ,
121- variables = variables ,
122- operation_name = operation_name ,
124+ params = params ,
123125 result = result
124126 )
125127
@@ -140,23 +142,21 @@ def dispatch_request(self):
140142 )
141143
142144 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 )
144146 try :
145147 execution_result = self .execute_graphql_request (
146148 self .schema ,
147149 execute ,
148150 data ,
149- query ,
150- variables ,
151- operation_name ,
151+ params ,
152152 only_allow_query ,
153153 )
154154 except HttpQueryError :
155155 if show_graphiql :
156156 execution_result = None
157157 else :
158158 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 )
160160
161161 @staticmethod
162162 def format_execution_result (execution_result , id , format_error ):
@@ -223,12 +223,12 @@ def execute(self, schema, *args, **kwargs):
223223 )
224224
225225 @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 :
228228 raise HttpQueryError (400 , 'Must provide query string.' )
229229
230230 try :
231- source = Source (query , name = 'GraphQL request' )
231+ source = Source (params . query , name = 'GraphQL request' )
232232 ast = parse (source )
233233 validation_errors = validate (schema , ast )
234234 if validation_errors :
@@ -240,7 +240,7 @@ def execute_graphql_request(schema, execute, data, query, variables, operation_n
240240 return ExecutionResult (errors = [e ], invalid = True )
241241
242242 if only_allow_query :
243- operation_ast = get_operation_ast (ast , operation_name )
243+ operation_ast = get_operation_ast (ast , params . operation_name )
244244 if operation_ast and operation_ast .operation != 'query' :
245245 raise HttpQueryError (
246246 405 ,
@@ -254,8 +254,8 @@ def execute_graphql_request(schema, execute, data, query, variables, operation_n
254254 return execute (
255255 schema ,
256256 ast ,
257- operation_name = operation_name ,
258- variable_values = variables ,
257+ operation_name = params . operation_name ,
258+ variable_values = params . variables ,
259259 )
260260 except Exception as e :
261261 return ExecutionResult (errors = [e ], invalid = True )
@@ -285,20 +285,22 @@ def request_wants_html(self):
285285 request .accept_mimetypes ['application/json' ]
286286
287287 @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 ):
293289 if variables and isinstance (variables , six .text_type ):
294290 try :
295- variables = json .loads (variables )
291+ return json .loads (variables )
296292 except :
297293 raise HttpQueryError (400 , 'Variables are invalid JSON.' )
294+ return variables
298295
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' )
299301 operation_name = data .get ('operationName' )
300302
301- return query , variables , operation_name , id
303+ return GraphQLParams ( query , variables , operation_name , id )
302304
303305 @staticmethod
304306 def format_error (error ):
0 commit comments