10
10
from django .views .generic import View
11
11
from django .views .decorators .csrf import ensure_csrf_cookie
12
12
13
- from graphql import Source , execute , parse , validate
13
+ from graphql import get_default_backend
14
14
from graphql .error import format_error as format_graphql_error
15
15
from graphql .error import GraphQLError
16
16
from graphql .execution import ExecutionResult
17
17
from graphql .type .schema import GraphQLSchema
18
- from graphql .utils .get_operation_ast import get_operation_ast
19
18
20
19
from .settings import graphene_settings
21
20
@@ -59,16 +58,20 @@ class GraphQLView(View):
59
58
schema = None
60
59
graphiql = False
61
60
executor = None
61
+ backend = None
62
62
middleware = None
63
63
root_value = None
64
64
pretty = False
65
65
batch = False
66
66
67
67
def __init__ (self , schema = None , executor = None , middleware = None , root_value = None , graphiql = False , pretty = False ,
68
- batch = False ):
68
+ batch = False , backend = None ):
69
69
if not schema :
70
70
schema = graphene_settings .SCHEMA
71
71
72
+ if backend is None :
73
+ backend = get_default_backend ()
74
+
72
75
if middleware is None :
73
76
middleware = graphene_settings .MIDDLEWARE
74
77
@@ -80,6 +83,7 @@ def __init__(self, schema=None, executor=None, middleware=None, root_value=None,
80
83
self .pretty = self .pretty or pretty
81
84
self .graphiql = self .graphiql or graphiql
82
85
self .batch = self .batch or batch
86
+ self .backend = backend
83
87
84
88
assert isinstance (
85
89
self .schema , GraphQLSchema ), 'A Schema is required to be provided to GraphQLView.'
@@ -96,6 +100,9 @@ def get_middleware(self, request):
96
100
def get_context (self , request ):
97
101
return request
98
102
103
+ def get_backend (self , request ):
104
+ return self .backend
105
+
99
106
@method_decorator (ensure_csrf_cookie )
100
107
def dispatch (self , request , * args , ** kwargs ):
101
108
try :
@@ -225,49 +232,44 @@ def parse_body(self, request):
225
232
226
233
return {}
227
234
228
- def execute (self , * args , ** kwargs ):
229
- return execute (self .schema , * args , ** kwargs )
230
-
231
235
def execute_graphql_request (self , request , data , query , variables , operation_name , show_graphiql = False ):
232
236
if not query :
233
237
if show_graphiql :
234
238
return None
235
239
raise HttpError (HttpResponseBadRequest (
236
240
'Must provide query string.' ))
237
241
238
- source = Source (query , name = 'GraphQL request' )
239
-
240
242
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 )
248
245
except Exception as e :
249
246
return ExecutionResult (errors = [e ], invalid = True )
250
247
251
248
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' :
254
251
if show_graphiql :
255
252
return None
256
253
257
254
raise HttpError (HttpResponseNotAllowed (
258
255
['POST' ], 'Can only perform a {} operation from a POST request.' .format (
259
- operation_ast . operation )
256
+ operation_type )
260
257
))
261
258
262
259
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 ,
267
269
operation_name = operation_name ,
268
- context_value = self .get_context (request ),
270
+ context = self .get_context (request ),
269
271
middleware = self .get_middleware (request ),
270
- executor = self . executor ,
272
+ ** extra_options
271
273
)
272
274
except Exception as e :
273
275
return ExecutionResult (errors = [e ], invalid = True )
0 commit comments