Skip to content

Commit 5a1e014

Browse files
committed
Use graphql-django-view to handle GraphQLView
1 parent 5c4db65 commit 5a1e014

File tree

5 files changed

+15
-69
lines changed

5 files changed

+15
-69
lines changed

graphene/contrib/django/views.py

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,10 @@
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
8-
9-
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 '')
4+
class GraphQLView(BaseGraphQLView):
5+
def __init__(self, schema, **kwargs):
6+
super(GraphQLView, self).__init__(
7+
schema=schema.schema,
8+
executor=schema.executor,
9+
**kwargs
10+
)

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ 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',
60+
'graphql-django-view>=1.0.0',
6061
'graphql-relay==0.3.3'
6162
],
6263
tests_require=[

tests/contrib_django/test_urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class Human(DjangoNode):
2323
class Meta:
2424
model = Article
2525

26-
def resolve_raises(self, *args):
26+
@staticmethod
27+
def resolve_raises(*args):
2728
raise Exception("This field should raise exception")
2829

2930
def get_node(self, id):

tests/contrib_django/test_views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_client_post_malformed_json(settings, client):
2626
response = client.post('/graphql', 'MALFORMED', 'application/json')
2727
json_response = format_response(response)
2828
assert json_response == {'errors': [
29-
{'message': 'Malformed json body in the post data'}]}
29+
{'message': 'POST body sent invalid JSON.'}]}
3030

3131

3232
def test_client_post_empty_query_json(settings, client):

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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

0 commit comments

Comments
 (0)