Skip to content

Commit f217731

Browse files
committed
Improved GraphQL batch view errors.
1 parent 0ec8d2c commit f217731

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

graphene_django/tests/test_views.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ def test_batch_allows_post_with_json_encoding(client):
183183
}]
184184

185185

186+
def test_batch_fails_if_is_empty(client):
187+
response = client.post(batch_url_string(), j([]), 'application/json')
188+
189+
assert response.status_code == 200
190+
assert response_json(response) == {
191+
'errors': [{'message': 'Received an empty list in the batch request.'}]
192+
}
193+
194+
186195
def test_allows_sending_a_mutation_via_post(client):
187196
response = client.post(url_string(), j(query='mutation TestMutation { writeTest { test } }'), 'application/json')
188197

graphene_django/views.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,19 @@ def parse_body(self, request):
193193
try:
194194
request_json = json.loads(request.body.decode('utf-8'))
195195
if self.batch:
196-
assert isinstance(request_json, list)
196+
assert isinstance(request_json, list), (
197+
'Batch requests should receive a list, but received {}.'
198+
).format(repr(request_json))
199+
assert len(request_json) > 0, (
200+
'Received an empty list in the batch request.'
201+
)
197202
else:
198-
assert isinstance(request_json, dict)
203+
assert isinstance(request_json, dict), (
204+
'The received data is not a valid JSON query.'
205+
)
199206
return request_json
207+
except AssertionError as e:
208+
raise HttpError(HttpResponseBadRequest(str(e)))
200209
except:
201210
raise HttpError(HttpResponseBadRequest('POST body sent invalid JSON.'))
202211

0 commit comments

Comments
 (0)