mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 12:44:15 +03:00
Merge branch 'refs/heads/master' into 0.4.0
Conflicts: graphene/contrib/django/tests/test_views.py graphene/contrib/django/views.py
This commit is contained in:
commit
9548d6932c
|
@ -29,7 +29,7 @@ def test_client_post_malformed_json(settings, client):
|
||||||
{'message': 'Malformed json body in the post data'}]}
|
{'message': 'Malformed json body in the post data'}]}
|
||||||
|
|
||||||
|
|
||||||
def test_client_post_empty_query(settings, client):
|
def test_client_post_empty_query_json(settings, client):
|
||||||
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/graphql', json.dumps({'query': ''}), 'application/json')
|
'/graphql', json.dumps({'query': ''}), 'application/json')
|
||||||
|
@ -38,7 +38,16 @@ def test_client_post_empty_query(settings, client):
|
||||||
{'message': 'Must provide query string.'}]}
|
{'message': 'Must provide query string.'}]}
|
||||||
|
|
||||||
|
|
||||||
def test_client_post_bad_query(settings, client):
|
def test_client_post_empty_query_graphql(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
||||||
|
response = client.post(
|
||||||
|
'/graphql', '', 'application/graphql')
|
||||||
|
json_response = format_response(response)
|
||||||
|
assert json_response == {'errors': [
|
||||||
|
{'message': 'Must provide query string.'}]}
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_post_bad_query_json(settings, client):
|
||||||
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/graphql', json.dumps({'query': '{ MALFORMED'}), 'application/json')
|
'/graphql', json.dumps({'query': '{ MALFORMED'}), 'application/json')
|
||||||
|
@ -48,6 +57,16 @@ def test_client_post_bad_query(settings, client):
|
||||||
assert 'Syntax Error GraphQL' in json_response['errors'][0]['message']
|
assert 'Syntax Error GraphQL' in json_response['errors'][0]['message']
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_post_bad_query_graphql(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
||||||
|
response = client.post(
|
||||||
|
'/graphql', '{ MALFORMED', 'application/graphql')
|
||||||
|
json_response = format_response(response)
|
||||||
|
assert 'errors' in json_response
|
||||||
|
assert len(json_response['errors']) == 1
|
||||||
|
assert 'Syntax Error GraphQL' in json_response['errors'][0]['message']
|
||||||
|
|
||||||
|
|
||||||
def test_client_get_good_query(settings, client):
|
def test_client_get_good_query(settings, client):
|
||||||
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
||||||
response = client.get('/graphql', {'query': '{ headline }'})
|
response = client.get('/graphql', {'query': '{ headline }'})
|
||||||
|
@ -69,7 +88,7 @@ def test_client_get_good_query_with_raise(settings, client):
|
||||||
assert json_response['data']['raises'] is None
|
assert json_response['data']['raises'] is None
|
||||||
|
|
||||||
|
|
||||||
def test_client_post_good_query(settings, client):
|
def test_client_post_good_query_json(settings, client):
|
||||||
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
||||||
response = client.post(
|
response = client.post(
|
||||||
'/graphql', json.dumps({'query': '{ headline }'}), 'application/json')
|
'/graphql', json.dumps({'query': '{ headline }'}), 'application/json')
|
||||||
|
@ -82,8 +101,21 @@ def test_client_post_good_query(settings, client):
|
||||||
assert json_response == expected_json
|
assert json_response == expected_json
|
||||||
|
|
||||||
|
|
||||||
|
def test_client_post_good_query_graphql(settings, client):
|
||||||
|
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
||||||
|
response = client.post(
|
||||||
|
'/graphql', '{ headline }', 'application/graphql')
|
||||||
|
json_response = format_response(response)
|
||||||
|
expected_json = {
|
||||||
|
'data': {
|
||||||
|
'headline': None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert json_response == expected_json
|
||||||
|
|
||||||
|
|
||||||
# def test_client_get_bad_query(settings, client):
|
# def test_client_get_bad_query(settings, client):
|
||||||
# settings.ROOT_URLCONF = 'tests.contrib_django.test_urls'
|
# settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
||||||
# response = client.get('/graphql')
|
# response = client.get('/graphql')
|
||||||
# json_response = format_response(response)
|
# json_response = format_response(response)
|
||||||
# assert json_response == {'errors': [{'message': 'Must provide query string.'}]}
|
# assert json_response == {'errors': [{'message': 'Must provide query string.'}]}
|
||||||
|
|
|
@ -28,14 +28,11 @@ class GraphQLView(View):
|
||||||
errors = [{
|
errors = [{
|
||||||
"message": str(e)
|
"message": str(e)
|
||||||
} for e in errors]
|
} for e in errors]
|
||||||
return HttpResponse(
|
return HttpResponse(json.dumps({'errors': errors}), content_type='application/json')
|
||||||
json.dumps({'errors': errors}),
|
|
||||||
content_type='application/json')
|
|
||||||
|
|
||||||
def execute_query(self, request, query, *args, **kwargs):
|
def execute_query(self, request, query, *args, **kwargs):
|
||||||
if not query:
|
if not query:
|
||||||
return self.response_errors(
|
return self.response_errors(Exception("Must provide query string."))
|
||||||
Exception("Must provide query string."))
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
result = self.schema.execute(query, *args, **kwargs)
|
result = self.schema.execute(query, *args, **kwargs)
|
||||||
|
@ -62,8 +59,9 @@ class GraphQLView(View):
|
||||||
received_json_data = json.loads(request.body.decode())
|
received_json_data = json.loads(request.body.decode())
|
||||||
query = received_json_data.get('query')
|
query = received_json_data.get('query')
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return self.response_errors(ValueError(
|
return self.response_errors(ValueError("Malformed json body in the post data"))
|
||||||
"Malformed json body in the post data"))
|
elif content_type == 'application/graphql':
|
||||||
|
query = request.body.decode()
|
||||||
else:
|
else:
|
||||||
query = request.POST.get('query') or request.GET.get('query')
|
query = request.POST.get('query') or request.GET.get('query')
|
||||||
return self.execute_query(request, query or '')
|
return self.execute_query(request, query or '')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user