@@ -73,14 +73,21 @@ def dispatch_request(self):
7373 raise HttpError (MethodNotAllowed (['GET' , 'POST' ], 'GraphQL only supports GET and POST requests.' ))
7474
7575 data = self .parse_body (request )
76+
7677 show_graphiql = self .graphiql and self .can_display_graphiql (data )
7778
78- if self .batch :
79+ if isinstance (data , list ):
80+ if not self .batch or show_graphiql :
81+ raise HttpError (BadRequest ('Batch requests are not allowed.' ))
82+
7983 responses = [self .get_response (request , entry ) for entry in data ]
80- result = '[{}]' . format ( ',' . join ([ response [ 0 ] for response in responses ]) )
81- status_code = max (responses , key = lambda response : response [ 1 ])[ 1 ]
84+ response , status_codes = zip ( * responses )
85+ status_code = max (status_codes )
8286 else :
83- result , status_code = self .get_response (request , data , show_graphiql )
87+ response , status_code = self .get_response (request , data , show_graphiql )
88+
89+ pretty = self .pretty or show_graphiql or request .args .get ('pretty' )
90+ result = self .json_encode (response , pretty )
8491
8592 if show_graphiql :
8693 query , variables , operation_name , id = self .get_graphql_params (request , data )
@@ -99,7 +106,7 @@ def dispatch_request(self):
99106
100107 except HttpError as e :
101108 return Response (
102- self .json_encode (request , {
109+ self .json_encode ({
103110 'errors' : [self .format_error (e )]
104111 }),
105112 status = e .response .code ,
@@ -132,20 +139,15 @@ def get_response(self, request, data, show_graphiql=False):
132139 response ['data' ] = execution_result .data
133140
134141 if self .batch :
135- response = {
136- 'id' : id ,
137- 'payload' : response ,
138- 'status' : status_code ,
139- }
142+ response ['id' ] = id
140143
141- result = self .json_encode (request , response , show_graphiql )
142144 else :
143- result = None
145+ response = None
144146
145- return result , status_code
147+ return response , status_code
146148
147- def json_encode ( self , request , d , show_graphiql = False ):
148- pretty = self . pretty or show_graphiql or request . args . get ( 'pretty' )
149+ @ staticmethod
150+ def json_encode ( d , pretty = False ):
149151 if not pretty :
150152 return json .dumps (d , separators = (',' , ':' ))
151153
@@ -160,12 +162,7 @@ def parse_body(self, request):
160162
161163 elif content_type == 'application/json' :
162164 try :
163- request_json = json .loads (request .data .decode ('utf8' ))
164- if self .batch :
165- assert isinstance (request_json , list )
166- else :
167- assert isinstance (request_json , dict )
168- return request_json
165+ return json .loads (request .data .decode ('utf8' ))
169166 except :
170167 raise HttpError (BadRequest ('POST body sent invalid JSON.' ))
171168
0 commit comments