Skip to content

Commit 83589a2

Browse files
committed
Merge branch '0.4.0' of github.com:graphql-python/graphene into 0.4.0
2 parents d8ca3c0 + 5be22df commit 83589a2

File tree

5 files changed

+21
-139
lines changed

5 files changed

+21
-139
lines changed

graphene/contrib/django/tests/test_views.py

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,6 @@ def format_response(response):
55
return json.loads(response.content.decode())
66

77

8-
def test_client_get_no_query(settings, client):
9-
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
10-
response = client.get('/graphql')
11-
json_response = format_response(response)
12-
assert json_response == {'errors': [
13-
{'message': 'Must provide query string.'}]}
14-
15-
16-
def test_client_post_no_query(settings, client):
17-
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
18-
response = client.post('/graphql', {})
19-
json_response = format_response(response)
20-
assert json_response == {'errors': [
21-
{'message': 'Must provide query string.'}]}
22-
23-
24-
def test_client_post_malformed_json(settings, client):
25-
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
26-
response = client.post('/graphql', 'MALFORMED', 'application/json')
27-
json_response = format_response(response)
28-
assert json_response == {'errors': [
29-
{'message': 'Malformed json body in the post data'}]}
30-
31-
32-
def test_client_post_empty_query_json(settings, client):
33-
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
34-
response = client.post(
35-
'/graphql', json.dumps({'query': ''}), 'application/json')
36-
json_response = format_response(response)
37-
assert json_response == {'errors': [
38-
{'message': 'Must provide query string.'}]}
39-
40-
41-
def test_client_post_empty_query_graphql(settings, client):
42-
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
43-
response = client.post(
44-
'/graphql', '', 'application/graphql')
45-
json_response = format_response(response)
46-
assert json_response == {'errors': [
47-
{'message': 'Must provide query string.'}]}
48-
49-
50-
def test_client_post_bad_query_json(settings, client):
51-
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
52-
response = client.post(
53-
'/graphql', json.dumps({'query': '{ MALFORMED'}), 'application/json')
54-
json_response = format_response(response)
55-
assert 'errors' in json_response
56-
assert len(json_response['errors']) == 1
57-
assert 'Syntax Error GraphQL' in json_response['errors'][0]['message']
58-
59-
60-
def test_client_post_bad_query_graphql(settings, client):
61-
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
62-
response = client.post(
63-
'/graphql', '{ MALFORMED', 'application/graphql')
64-
json_response = format_response(response)
65-
assert 'errors' in json_response
66-
assert len(json_response['errors']) == 1
67-
assert 'Syntax Error GraphQL' in json_response['errors'][0]['message']
68-
69-
708
def test_client_get_good_query(settings, client):
719
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
7210
response = client.get('/graphql', {'query': '{ headline }'})
@@ -83,8 +21,7 @@ def test_client_get_good_query_with_raise(settings, client):
8321
settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
8422
response = client.get('/graphql', {'query': '{ raises }'})
8523
json_response = format_response(response)
86-
assert json_response['errors'][0][
87-
'message'] == 'This field should raise exception'
24+
assert json_response['errors'][0]['message'] == 'This field should raise exception'
8825
assert json_response['data']['raises'] is None
8926

9027

@@ -112,10 +49,3 @@ def test_client_post_good_query_graphql(settings, client):
11249
}
11350
}
11451
assert json_response == expected_json
115-
116-
117-
# def test_client_get_bad_query(settings, client):
118-
# settings.ROOT_URLCONF = 'graphene.contrib.django.tests.test_urls'
119-
# response = client.get('/graphql')
120-
# json_response = format_response(response)
121-
# assert json_response == {'errors': [{'message': 'Must provide query string.'}]}

graphene/contrib/django/views.py

Lines changed: 12 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,16 @@
1-
import json
1+
from graphql_django_view import GraphQLView as BaseGraphQLView
22

3-
from django.conf import settings
4-
from django.http import HttpResponse
5-
from django.views.generic import View
63

7-
from graphql.core.error import GraphQLError, format_error
4+
class GraphQLView(BaseGraphQLView):
5+
graphene_schema = None
86

7+
def __init__(self, schema, **kwargs):
8+
super(GraphQLView, self).__init__(
9+
graphene_schema=schema,
10+
schema=schema.schema,
11+
executor=schema.executor,
12+
**kwargs
13+
)
914

10-
def form_error(error):
11-
if isinstance(error, GraphQLError):
12-
return format_error(error)
13-
return error
14-
15-
16-
class GraphQLView(View):
17-
schema = None
18-
19-
@staticmethod
20-
def format_result(result):
21-
data = {'data': result.data}
22-
if result.errors:
23-
data['errors'] = list(map(form_error, result.errors))
24-
25-
return data
26-
27-
def response_errors(self, *errors):
28-
errors = [{
29-
"message": str(e)
30-
} for e in errors]
31-
return HttpResponse(json.dumps({'errors': errors}), content_type='application/json')
32-
33-
def execute_query(self, request, query, *args, **kwargs):
34-
if not query:
35-
return self.response_errors(Exception("Must provide query string."))
36-
else:
37-
try:
38-
result = self.schema.execute(query, *args, **kwargs)
39-
data = self.format_result(result)
40-
except Exception as e:
41-
if settings.DEBUG:
42-
raise e
43-
return self.response_errors(e)
44-
return HttpResponse(json.dumps(data), content_type='application/json')
45-
46-
def get(self, request, *args, **kwargs):
47-
query = request.GET.get('query')
48-
return self.execute_query(request, query or '')
49-
50-
@staticmethod
51-
def get_content_type(request):
52-
meta = request.META
53-
return meta.get('CONTENT_TYPE', meta.get('HTTP_CONTENT_TYPE', ''))
54-
55-
def post(self, request, *args, **kwargs):
56-
content_type = self.get_content_type(request)
57-
if content_type == 'application/json':
58-
try:
59-
received_json_data = json.loads(request.body.decode())
60-
query = received_json_data.get('query')
61-
except ValueError:
62-
return self.response_errors(ValueError("Malformed json body in the post data"))
63-
elif content_type == 'application/graphql':
64-
query = request.body.decode()
65-
else:
66-
query = request.POST.get('query') or request.GET.get('query')
67-
return self.execute_query(request, query or '')
15+
def get_root_value(self, request):
16+
return self.graphene_schema.query(super(GraphQLView, self).get_root_value(request))

graphene/core/types/objecttype.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import six
77

88
from graphene import signals
9-
from graphql.core.type import (GraphQLArgument, GraphQLInputObjectType,
10-
GraphQLInterfaceType, GraphQLObjectType)
9+
from graphql.core.type import (GraphQLInputObjectType, GraphQLInterfaceType,
10+
GraphQLObjectType)
1111

1212
from ..exceptions import SkipField
1313
from ..options import Options

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def run_tests(self):
5656
install_requires=[
5757
'six>=1.10.0',
5858
'blinker',
59-
'graphql-core==0.4.7b0',
59+
'graphql-core==0.4.7b2',
6060
'graphql-relay==0.3.3'
6161
],
6262
tests_require=[
@@ -68,6 +68,7 @@ def run_tests(self):
6868
'django': [
6969
'Django>=1.6.0,<1.9',
7070
'singledispatch>=3.4.0.3',
71+
'graphql-django-view>=1.0.0',
7172
],
7273
},
7374

tox.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ deps=
77
pytest>=2.7.2
88
django>=1.8.0,<1.9
99
pytest-django
10-
graphql-core==0.4.7b0
10+
graphql-django-view>=1.0.0
11+
graphql-core==0.4.7b2
1112
graphql-relay==0.3.3
1213
six
1314
blinker
1415
singledispatch
16+
mock
1517
setenv =
1618
PYTHONPATH = .:{envdir}
1719
commands=
18-
py.test tests/ examples/
20+
py.test
1921

2022
[testenv:flake8]
2123
deps = flake8

0 commit comments

Comments
 (0)