mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-11 04:07:16 +03:00
Merge branch '0.4.0' of github.com:graphql-python/graphene into 0.4.0
This commit is contained in:
commit
83589a21b8
|
@ -5,68 +5,6 @@ def format_response(response):
|
||||||
return json.loads(response.content.decode())
|
return json.loads(response.content.decode())
|
||||||
|
|
||||||
|
|
||||||
def test_client_get_no_query(settings, client):
|
|
||||||
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
|
||||||
response = client.get('/graphql')
|
|
||||||
json_response = format_response(response)
|
|
||||||
assert json_response == {'errors': [
|
|
||||||
{'message': 'Must provide query string.'}]}
|
|
||||||
|
|
||||||
|
|
||||||
def test_client_post_no_query(settings, client):
|
|
||||||
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
|
||||||
response = client.post('/graphql', {})
|
|
||||||
json_response = format_response(response)
|
|
||||||
assert json_response == {'errors': [
|
|
||||||
{'message': 'Must provide query string.'}]}
|
|
||||||
|
|
||||||
|
|
||||||
def test_client_post_malformed_json(settings, client):
|
|
||||||
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
|
||||||
response = client.post('/graphql', 'MALFORMED', 'application/json')
|
|
||||||
json_response = format_response(response)
|
|
||||||
assert json_response == {'errors': [
|
|
||||||
{'message': 'Malformed json body in the post data'}]}
|
|
||||||
|
|
||||||
|
|
||||||
def test_client_post_empty_query_json(settings, client):
|
|
||||||
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
|
||||||
response = client.post(
|
|
||||||
'/graphql', json.dumps({'query': ''}), 'application/json')
|
|
||||||
json_response = format_response(response)
|
|
||||||
assert json_response == {'errors': [
|
|
||||||
{'message': 'Must provide query string.'}]}
|
|
||||||
|
|
||||||
|
|
||||||
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'
|
|
||||||
response = client.post(
|
|
||||||
'/graphql', json.dumps({'query': '{ MALFORMED'}), 'application/json')
|
|
||||||
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_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 }'})
|
||||||
|
@ -83,8 +21,7 @@ def test_client_get_good_query_with_raise(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': '{ raises }'})
|
response = client.get('/graphql', {'query': '{ raises }'})
|
||||||
json_response = format_response(response)
|
json_response = format_response(response)
|
||||||
assert json_response['errors'][0][
|
assert json_response['errors'][0]['message'] == 'This field should raise exception'
|
||||||
'message'] == 'This field should raise exception'
|
|
||||||
assert json_response['data']['raises'] is None
|
assert json_response['data']['raises'] is None
|
||||||
|
|
||||||
|
|
||||||
|
@ -112,10 +49,3 @@ def test_client_post_good_query_graphql(settings, client):
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert json_response == expected_json
|
assert json_response == expected_json
|
||||||
|
|
||||||
|
|
||||||
# def test_client_get_bad_query(settings, client):
|
|
||||||
# settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
|
|
||||||
# response = client.get('/graphql')
|
|
||||||
# json_response = format_response(response)
|
|
||||||
# assert json_response == {'errors': [{'message': 'Must provide query string.'}]}
|
|
||||||
|
|
|
@ -1,67 +1,16 @@
|
||||||
import json
|
from graphql_django_view import GraphQLView as BaseGraphQLView
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.http import HttpResponse
|
|
||||||
from django.views.generic import View
|
|
||||||
|
|
||||||
from graphql.core.error import GraphQLError, format_error
|
|
||||||
|
|
||||||
|
|
||||||
def form_error(error):
|
class GraphQLView(BaseGraphQLView):
|
||||||
if isinstance(error, GraphQLError):
|
graphene_schema = None
|
||||||
return format_error(error)
|
|
||||||
return error
|
|
||||||
|
|
||||||
|
def __init__(self, schema, **kwargs):
|
||||||
|
super(GraphQLView, self).__init__(
|
||||||
|
graphene_schema=schema,
|
||||||
|
schema=schema.schema,
|
||||||
|
executor=schema.executor,
|
||||||
|
**kwargs
|
||||||
|
)
|
||||||
|
|
||||||
class GraphQLView(View):
|
def get_root_value(self, request):
|
||||||
schema = None
|
return self.graphene_schema.query(super(GraphQLView, self).get_root_value(request))
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def format_result(result):
|
|
||||||
data = {'data': result.data}
|
|
||||||
if result.errors:
|
|
||||||
data['errors'] = list(map(form_error, result.errors))
|
|
||||||
|
|
||||||
return data
|
|
||||||
|
|
||||||
def response_errors(self, *errors):
|
|
||||||
errors = [{
|
|
||||||
"message": str(e)
|
|
||||||
} for e in errors]
|
|
||||||
return HttpResponse(json.dumps({'errors': errors}), content_type='application/json')
|
|
||||||
|
|
||||||
def execute_query(self, request, query, *args, **kwargs):
|
|
||||||
if not query:
|
|
||||||
return self.response_errors(Exception("Must provide query string."))
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
result = self.schema.execute(query, *args, **kwargs)
|
|
||||||
data = self.format_result(result)
|
|
||||||
except Exception as e:
|
|
||||||
if settings.DEBUG:
|
|
||||||
raise e
|
|
||||||
return self.response_errors(e)
|
|
||||||
return HttpResponse(json.dumps(data), content_type='application/json')
|
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
|
||||||
query = request.GET.get('query')
|
|
||||||
return self.execute_query(request, query or '')
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_content_type(request):
|
|
||||||
meta = request.META
|
|
||||||
return meta.get('CONTENT_TYPE', meta.get('HTTP_CONTENT_TYPE', ''))
|
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
|
||||||
content_type = self.get_content_type(request)
|
|
||||||
if content_type == 'application/json':
|
|
||||||
try:
|
|
||||||
received_json_data = json.loads(request.body.decode())
|
|
||||||
query = received_json_data.get('query')
|
|
||||||
except ValueError:
|
|
||||||
return self.response_errors(ValueError("Malformed json body in the post data"))
|
|
||||||
elif content_type == 'application/graphql':
|
|
||||||
query = request.body.decode()
|
|
||||||
else:
|
|
||||||
query = request.POST.get('query') or request.GET.get('query')
|
|
||||||
return self.execute_query(request, query or '')
|
|
||||||
|
|
|
@ -6,8 +6,8 @@ from functools import partial
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from graphene import signals
|
from graphene import signals
|
||||||
from graphql.core.type import (GraphQLArgument, GraphQLInputObjectType,
|
from graphql.core.type import (GraphQLInputObjectType, GraphQLInterfaceType,
|
||||||
GraphQLInterfaceType, GraphQLObjectType)
|
GraphQLObjectType)
|
||||||
|
|
||||||
from ..exceptions import SkipField
|
from ..exceptions import SkipField
|
||||||
from ..options import Options
|
from ..options import Options
|
||||||
|
|
3
setup.py
3
setup.py
|
@ -56,7 +56,7 @@ setup(
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'six>=1.10.0',
|
'six>=1.10.0',
|
||||||
'blinker',
|
'blinker',
|
||||||
'graphql-core==0.4.7b0',
|
'graphql-core==0.4.7b2',
|
||||||
'graphql-relay==0.3.3'
|
'graphql-relay==0.3.3'
|
||||||
],
|
],
|
||||||
tests_require=[
|
tests_require=[
|
||||||
|
@ -68,6 +68,7 @@ setup(
|
||||||
'django': [
|
'django': [
|
||||||
'Django>=1.6.0,<1.9',
|
'Django>=1.6.0,<1.9',
|
||||||
'singledispatch>=3.4.0.3',
|
'singledispatch>=3.4.0.3',
|
||||||
|
'graphql-django-view>=1.0.0',
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
6
tox.ini
6
tox.ini
|
@ -7,15 +7,17 @@ deps=
|
||||||
pytest>=2.7.2
|
pytest>=2.7.2
|
||||||
django>=1.8.0,<1.9
|
django>=1.8.0,<1.9
|
||||||
pytest-django
|
pytest-django
|
||||||
graphql-core==0.4.7b0
|
graphql-django-view>=1.0.0
|
||||||
|
graphql-core==0.4.7b2
|
||||||
graphql-relay==0.3.3
|
graphql-relay==0.3.3
|
||||||
six
|
six
|
||||||
blinker
|
blinker
|
||||||
singledispatch
|
singledispatch
|
||||||
|
mock
|
||||||
setenv =
|
setenv =
|
||||||
PYTHONPATH = .:{envdir}
|
PYTHONPATH = .:{envdir}
|
||||||
commands=
|
commands=
|
||||||
py.test tests/ examples/
|
py.test
|
||||||
|
|
||||||
[testenv:flake8]
|
[testenv:flake8]
|
||||||
deps = flake8
|
deps = flake8
|
||||||
|
|
Loading…
Reference in New Issue
Block a user