17
17
18
18
19
19
GraphQLParams = namedtuple ('GraphQLParams' , 'query,variables,operation_name,id' )
20
- GraphQLResponse = namedtuple ('GraphQLResponse' , 'result,params, status_code' )
20
+ GraphQLResponse = namedtuple ('GraphQLResponse' , 'result,status_code' )
21
21
22
22
23
23
class HttpQueryError (Exception ):
@@ -51,19 +51,23 @@ def __init__(self, **kwargs):
51
51
52
52
assert isinstance (self .schema , GraphQLSchema ), 'A Schema is required to be provided to GraphQLView.'
53
53
54
+ # Flask
54
55
# noinspection PyUnusedLocal
55
- def get_root_value (self , request ):
56
+ def get_root_value (self ):
56
57
return self .root_value
57
58
58
- def get_context (self , request ):
59
+ # Flask
60
+ def get_context (self ):
59
61
if self .context is not None :
60
62
return self .context
61
63
return request
62
64
63
- def get_middleware (self , request ):
65
+ # Flask
66
+ def get_middleware (self ):
64
67
return self .middleware
65
68
66
- def get_executor (self , request ):
69
+ # Flask
70
+ def get_executor (self ):
67
71
return self .executor
68
72
69
73
def render_graphiql (self , params , result ):
@@ -108,19 +112,20 @@ def dispatch_request(self):
108
112
'Batch GraphQL requests are not enabled.'
109
113
)
110
114
115
+ all_params = [self .get_graphql_params (entry ) for entry in data ]
111
116
112
117
responses = [self .get_response (
113
118
self .schema ,
114
- self . get_graphql_params ( entry ) ,
119
+ params ,
115
120
catch ,
116
121
only_allow_query ,
117
- root_value = self .get_root_value (request ),
118
- context_value = self .get_context (request ),
119
- middleware = self .get_middleware (request ),
120
- executor = self .get_executor (request ),
121
- ) for entry in data ]
122
+ root_value = self .get_root_value (),
123
+ context_value = self .get_context (),
124
+ middleware = self .get_middleware (),
125
+ executor = self .get_executor (),
126
+ ) for params in all_params ]
122
127
123
- response , params , status_codes = zip (* responses )
128
+ response , status_codes = zip (* responses )
124
129
status_code = max (status_codes )
125
130
126
131
if not is_batch :
@@ -131,7 +136,7 @@ def dispatch_request(self):
131
136
132
137
if show_graphiql :
133
138
return self .render_graphiql (
134
- params = params [0 ],
139
+ params = all_params [0 ],
135
140
result = result
136
141
)
137
142
@@ -160,14 +165,9 @@ def get_response(self, schema, params, catch=None, only_allow_query=False, **kwa
160
165
** kwargs
161
166
)
162
167
except catch :
163
- execution_result = None
168
+ return GraphQLResponse ( None , 400 )
164
169
165
- response , status_code = self .format_execution_result (execution_result , params .id , self .format_error )
166
- return GraphQLResponse (
167
- response ,
168
- params ,
169
- status_code
170
- )
170
+ return self .format_execution_result (execution_result , params .id , self .format_error )
171
171
172
172
@staticmethod
173
173
def format_execution_result (execution_result , id , format_error ):
@@ -190,8 +190,9 @@ def format_execution_result(execution_result, id, format_error):
190
190
else :
191
191
response = None
192
192
193
- return response , status_code
193
+ return GraphQLResponse ( response , status_code )
194
194
195
+ # Flask
195
196
# noinspection PyBroadException
196
197
def parse_body (self ):
197
198
# We use mimetype here since we don't need the other
@@ -267,12 +268,14 @@ def json_encode(data, pretty=False):
267
268
separators = (',' , ': ' )
268
269
)
269
270
271
+ # Flask
270
272
def should_display_graphiql (self , data ):
271
273
if not self .graphiql or 'raw' in data :
272
274
return False
273
275
274
276
return self .request_wants_html ()
275
277
278
+ # Flask
276
279
def request_wants_html (self ):
277
280
best = request .accept_mimetypes \
278
281
.best_match (['application/json' , 'text/html' ])
0 commit comments