Improved GraphQL batch view errors.

This commit is contained in:
Syrus Akbary 2017-02-20 01:08:42 -08:00
parent 0ec8d2c828
commit f217731066
2 changed files with 20 additions and 2 deletions

View File

@ -183,6 +183,15 @@ def test_batch_allows_post_with_json_encoding(client):
}]
def test_batch_fails_if_is_empty(client):
response = client.post(batch_url_string(), j([]), 'application/json')
assert response.status_code == 200
assert response_json(response) == {
'errors': [{'message': 'Received an empty list in the batch request.'}]
}
def test_allows_sending_a_mutation_via_post(client):
response = client.post(url_string(), j(query='mutation TestMutation { writeTest { test } }'), 'application/json')

View File

@ -193,10 +193,19 @@ class GraphQLView(View):
try:
request_json = json.loads(request.body.decode('utf-8'))
if self.batch:
assert isinstance(request_json, list)
assert isinstance(request_json, list), (
'Batch requests should receive a list, but received {}.'
).format(repr(request_json))
assert len(request_json) > 0, (
'Received an empty list in the batch request.'
)
else:
assert isinstance(request_json, dict)
assert isinstance(request_json, dict), (
'The received data is not a valid JSON query.'
)
return request_json
except AssertionError as e:
raise HttpError(HttpResponseBadRequest(str(e)))
except:
raise HttpError(HttpResponseBadRequest('POST body sent invalid JSON.'))