@@ -73,14 +73,21 @@ def dispatch_request(self):
73
73
raise HttpError (MethodNotAllowed (['GET' , 'POST' ], 'GraphQL only supports GET and POST requests.' ))
74
74
75
75
data = self .parse_body (request )
76
+
76
77
show_graphiql = self .graphiql and self .can_display_graphiql (data )
77
78
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
+
79
83
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 )
82
86
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 )
84
91
85
92
if show_graphiql :
86
93
query , variables , operation_name , id = self .get_graphql_params (request , data )
@@ -99,7 +106,7 @@ def dispatch_request(self):
99
106
100
107
except HttpError as e :
101
108
return Response (
102
- self .json_encode (request , {
109
+ self .json_encode ({
103
110
'errors' : [self .format_error (e )]
104
111
}),
105
112
status = e .response .code ,
@@ -132,20 +139,15 @@ def get_response(self, request, data, show_graphiql=False):
132
139
response ['data' ] = execution_result .data
133
140
134
141
if self .batch :
135
- response = {
136
- 'id' : id ,
137
- 'payload' : response ,
138
- 'status' : status_code ,
139
- }
142
+ response ['id' ] = id
140
143
141
- result = self .json_encode (request , response , show_graphiql )
142
144
else :
143
- result = None
145
+ response = None
144
146
145
- return result , status_code
147
+ return response , status_code
146
148
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 ):
149
151
if not pretty :
150
152
return json .dumps (d , separators = (',' , ':' ))
151
153
@@ -160,12 +162,7 @@ def parse_body(self, request):
160
162
161
163
elif content_type == 'application/json' :
162
164
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' ))
169
166
except :
170
167
raise HttpError (BadRequest ('POST body sent invalid JSON.' ))
171
168
0 commit comments