@@ -81,8 +81,10 @@ def __init__(self, schema=None, executor=None, middleware=None, root_value=None,
81
81
self .graphiql = graphiql
82
82
self .batch = batch
83
83
84
- assert isinstance (self .schema , GraphQLSchema ), 'A Schema is required to be provided to GraphQLView.'
85
- assert not all ((graphiql , batch )), 'Use either graphiql or batch processing'
84
+ assert isinstance (
85
+ self .schema , GraphQLSchema ), 'A Schema is required to be provided to GraphQLView.'
86
+ assert not all ((graphiql , batch )
87
+ ), 'Use either graphiql or batch processing'
86
88
87
89
# noinspection PyUnusedLocal
88
90
def get_root_value (self , request ):
@@ -98,20 +100,27 @@ def get_context(self, request):
98
100
def dispatch (self , request , * args , ** kwargs ):
99
101
try :
100
102
if request .method .lower () not in ('get' , 'post' ):
101
- raise HttpError (HttpResponseNotAllowed (['GET' , 'POST' ], 'GraphQL only supports GET and POST requests.' ))
103
+ raise HttpError (HttpResponseNotAllowed (
104
+ ['GET' , 'POST' ], 'GraphQL only supports GET and POST requests.' ))
102
105
103
106
data = self .parse_body (request )
104
- show_graphiql = self .graphiql and self .can_display_graphiql (request , data )
107
+ show_graphiql = self .graphiql and self .can_display_graphiql (
108
+ request , data )
105
109
106
110
if self .batch :
107
- responses = [self .get_response (request , entry ) for entry in data ]
108
- result = '[{}]' .format (',' .join ([response [0 ] for response in responses ]))
109
- status_code = max (responses , key = lambda response : response [1 ])[1 ]
111
+ responses = [self .get_response (
112
+ request , entry ) for entry in data ]
113
+ result = '[{}]' .format (
114
+ ',' .join ([response [0 ] for response in responses ]))
115
+ status_code = max (
116
+ responses , key = lambda response : response [1 ])[1 ]
110
117
else :
111
- result , status_code = self .get_response (request , data , show_graphiql )
118
+ result , status_code = self .get_response (
119
+ request , data , show_graphiql )
112
120
113
121
if show_graphiql :
114
- query , variables , operation_name , id = self .get_graphql_params (request , data )
122
+ query , variables , operation_name , id = self .get_graphql_params (
123
+ request , data )
115
124
return self .render_graphiql (
116
125
request ,
117
126
graphiql_version = self .graphiql_version ,
@@ -136,7 +145,8 @@ def dispatch(self, request, *args, **kwargs):
136
145
return response
137
146
138
147
def get_response (self , request , data , show_graphiql = False ):
139
- query , variables , operation_name , id = self .get_graphql_params (request , data )
148
+ query , variables , operation_name , id = self .get_graphql_params (
149
+ request , data )
140
150
141
151
execution_result = self .execute_graphql_request (
142
152
request ,
@@ -152,7 +162,8 @@ def get_response(self, request, data, show_graphiql=False):
152
162
response = {}
153
163
154
164
if execution_result .errors :
155
- response ['errors' ] = [self .format_error (e ) for e in execution_result .errors ]
165
+ response ['errors' ] = [self .format_error (
166
+ e ) for e in execution_result .errors ]
156
167
157
168
if execution_result .invalid :
158
169
status_code = 400
@@ -209,7 +220,8 @@ def parse_body(self, request):
209
220
except AssertionError as e :
210
221
raise HttpError (HttpResponseBadRequest (str (e )))
211
222
except (TypeError , ValueError ):
212
- raise HttpError (HttpResponseBadRequest ('POST body sent invalid JSON.' ))
223
+ raise HttpError (HttpResponseBadRequest (
224
+ 'POST body sent invalid JSON.' ))
213
225
214
226
elif content_type in ['application/x-www-form-urlencoded' , 'multipart/form-data' ]:
215
227
return request .POST
@@ -223,7 +235,8 @@ def execute_graphql_request(self, request, data, query, variables, operation_nam
223
235
if not query :
224
236
if show_graphiql :
225
237
return None
226
- raise HttpError (HttpResponseBadRequest ('Must provide query string.' ))
238
+ raise HttpError (HttpResponseBadRequest (
239
+ 'Must provide query string.' ))
227
240
228
241
source = Source (query , name = 'GraphQL request' )
229
242
@@ -245,7 +258,8 @@ def execute_graphql_request(self, request, data, query, variables, operation_nam
245
258
return None
246
259
247
260
raise HttpError (HttpResponseNotAllowed (
248
- ['POST' ], 'Can only perform a {} operation from a POST request.' .format (operation_ast .operation )
261
+ ['POST' ], 'Can only perform a {} operation from a POST request.' .format (
262
+ operation_ast .operation )
249
263
))
250
264
251
265
try :
@@ -283,10 +297,12 @@ def get_graphql_params(request, data):
283
297
if variables and isinstance (variables , six .text_type ):
284
298
try :
285
299
variables = json .loads (variables )
286
- except :
287
- raise HttpError (HttpResponseBadRequest ('Variables are invalid JSON.' ))
300
+ except Exception :
301
+ raise HttpError (HttpResponseBadRequest (
302
+ 'Variables are invalid JSON.' ))
288
303
289
- operation_name = request .GET .get ('operationName' ) or data .get ('operationName' )
304
+ operation_name = request .GET .get (
305
+ 'operationName' ) or data .get ('operationName' )
290
306
if operation_name == "null" :
291
307
operation_name = None
292
308
@@ -302,5 +318,6 @@ def format_error(error):
302
318
@staticmethod
303
319
def get_content_type (request ):
304
320
meta = request .META
305
- content_type = meta .get ('CONTENT_TYPE' , meta .get ('HTTP_CONTENT_TYPE' , '' ))
321
+ content_type = meta .get (
322
+ 'CONTENT_TYPE' , meta .get ('HTTP_CONTENT_TYPE' , '' ))
306
323
return content_type .split (';' , 1 )[0 ].lower ()
0 commit comments