Skip to content

Commit 490ed92

Browse files
authored
Merge pull request #449 from graphql-python/graphqlview-pluggable-backend
Graphqlview pluggable backend
2 parents a480a39 + a65f786 commit 490ed92

File tree

5 files changed

+35
-28
lines changed

5 files changed

+35
-28
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
graphene
22
graphene-django
3-
graphql-core
3+
graphql-core>=2.1rc1
44
django==1.9

examples/cookbook/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
graphene
22
graphene-django
3-
graphql-core
3+
graphql-core>=2.1rc1
44
django==1.9
55
django-filter==0.11.0

graphene_django/tests/test_views.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,11 @@ def test_handles_field_errors_caught_by_graphql(client):
448448
assert response.status_code == 200
449449
assert response_json(response) == {
450450
'data': None,
451-
'errors': [{'locations': [{'column': 2, 'line': 1}], 'message': 'Throws!'}]
451+
'errors': [{
452+
'locations': [{'column': 2, 'line': 1}],
453+
'path': ['thrower'],
454+
'message': 'Throws!',
455+
}]
452456
}
453457

454458

@@ -457,7 +461,7 @@ def test_handles_syntax_errors_caught_by_graphql(client):
457461
assert response.status_code == 400
458462
assert response_json(response) == {
459463
'errors': [{'locations': [{'column': 1, 'line': 1}],
460-
'message': 'Syntax Error GraphQL request (1:1) '
464+
'message': 'Syntax Error GraphQL (1:1) '
461465
'Unexpected Name "syntaxerror"\n\n1: syntaxerror\n ^\n'}]
462466
}
463467

graphene_django/views.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@
1010
from django.views.generic import View
1111
from django.views.decorators.csrf import ensure_csrf_cookie
1212

13-
from graphql import Source, execute, parse, validate
13+
from graphql import get_default_backend
1414
from graphql.error import format_error as format_graphql_error
1515
from graphql.error import GraphQLError
1616
from graphql.execution import ExecutionResult
1717
from graphql.type.schema import GraphQLSchema
18-
from graphql.utils.get_operation_ast import get_operation_ast
1918

2019
from .settings import graphene_settings
2120

@@ -59,16 +58,20 @@ class GraphQLView(View):
5958
schema = None
6059
graphiql = False
6160
executor = None
61+
backend = None
6262
middleware = None
6363
root_value = None
6464
pretty = False
6565
batch = False
6666

6767
def __init__(self, schema=None, executor=None, middleware=None, root_value=None, graphiql=False, pretty=False,
68-
batch=False):
68+
batch=False, backend=None):
6969
if not schema:
7070
schema = graphene_settings.SCHEMA
7171

72+
if backend is None:
73+
backend = get_default_backend()
74+
7275
if middleware is None:
7376
middleware = graphene_settings.MIDDLEWARE
7477

@@ -80,6 +83,7 @@ def __init__(self, schema=None, executor=None, middleware=None, root_value=None,
8083
self.pretty = self.pretty or pretty
8184
self.graphiql = self.graphiql or graphiql
8285
self.batch = self.batch or batch
86+
self.backend = backend
8387

8488
assert isinstance(
8589
self.schema, GraphQLSchema), 'A Schema is required to be provided to GraphQLView.'
@@ -96,6 +100,9 @@ def get_middleware(self, request):
96100
def get_context(self, request):
97101
return request
98102

103+
def get_backend(self, request):
104+
return self.backend
105+
99106
@method_decorator(ensure_csrf_cookie)
100107
def dispatch(self, request, *args, **kwargs):
101108
try:
@@ -225,49 +232,44 @@ def parse_body(self, request):
225232

226233
return {}
227234

228-
def execute(self, *args, **kwargs):
229-
return execute(self.schema, *args, **kwargs)
230-
231235
def execute_graphql_request(self, request, data, query, variables, operation_name, show_graphiql=False):
232236
if not query:
233237
if show_graphiql:
234238
return None
235239
raise HttpError(HttpResponseBadRequest(
236240
'Must provide query string.'))
237241

238-
source = Source(query, name='GraphQL request')
239-
240242
try:
241-
document_ast = parse(source)
242-
validation_errors = validate(self.schema, document_ast)
243-
if validation_errors:
244-
return ExecutionResult(
245-
errors=validation_errors,
246-
invalid=True,
247-
)
243+
backend = self.get_backend(request)
244+
document = backend.document_from_string(self.schema, query)
248245
except Exception as e:
249246
return ExecutionResult(errors=[e], invalid=True)
250247

251248
if request.method.lower() == 'get':
252-
operation_ast = get_operation_ast(document_ast, operation_name)
253-
if operation_ast and operation_ast.operation != 'query':
249+
operation_type = document.get_operation_type(operation_name)
250+
if operation_type and operation_type != 'query':
254251
if show_graphiql:
255252
return None
256253

257254
raise HttpError(HttpResponseNotAllowed(
258255
['POST'], 'Can only perform a {} operation from a POST request.'.format(
259-
operation_ast.operation)
256+
operation_type)
260257
))
261258

262259
try:
263-
return self.execute(
264-
document_ast,
265-
root_value=self.get_root_value(request),
266-
variable_values=variables,
260+
extra_options = {}
261+
if self.executor:
262+
# We only include it optionally since
263+
# executor is not a valid argument in all backends
264+
extra_options['executor'] = self.executor
265+
266+
return document.execute(
267+
root=self.get_root_value(request),
268+
variables=variables,
267269
operation_name=operation_name,
268-
context_value=self.get_context(request),
270+
context=self.get_context(request),
269271
middleware=self.get_middleware(request),
270-
executor=self.executor,
272+
**extra_options
271273
)
272274
except Exception as e:
273275
return ExecutionResult(errors=[e], invalid=True)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
install_requires=[
6060
'six>=1.10.0',
6161
'graphene>=2.0.1,<3',
62+
'graphql-core>=2.1rc1',
6263
django_version,
6364
'iso8601',
6465
'singledispatch>=3.4.0.3',

0 commit comments

Comments
 (0)