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
@@ -59,16 +59,20 @@ class GraphQLView(View):
59
59
schema = None
60
60
graphiql = False
61
61
executor = None
62
+ backend = None
62
63
middleware = None
63
64
root_value = None
64
65
pretty = False
65
66
batch = False
66
67
67
68
def __init__ (self , schema = None , executor = None , middleware = None , root_value = None , graphiql = False , pretty = False ,
68
- batch = False ):
69
+ batch = False , backend = None ):
69
70
if not schema :
70
71
schema = graphene_settings .SCHEMA
71
72
73
+ if backend is None :
74
+ backend = get_default_backend ()
75
+
72
76
if middleware is None :
73
77
middleware = graphene_settings .MIDDLEWARE
74
78
@@ -80,6 +84,7 @@ def __init__(self, schema=None, executor=None, middleware=None, root_value=None,
80
84
self .pretty = self .pretty or pretty
81
85
self .graphiql = self .graphiql or graphiql
82
86
self .batch = self .batch or batch
87
+ self .backend = backend
83
88
84
89
assert isinstance (
85
90
self .schema , GraphQLSchema ), 'A Schema is required to be provided to GraphQLView.'
@@ -96,6 +101,9 @@ def get_middleware(self, request):
96
101
def get_context (self , request ):
97
102
return request
98
103
104
+ def get_backend (self , request ):
105
+ return self .backend
106
+
99
107
@method_decorator (ensure_csrf_cookie )
100
108
def dispatch (self , request , * args , ** kwargs ):
101
109
try :
@@ -225,49 +233,44 @@ def parse_body(self, request):
225
233
226
234
return {}
227
235
228
- def execute (self , * args , ** kwargs ):
229
- return execute (self .schema , * args , ** kwargs )
230
-
231
236
def execute_graphql_request (self , request , data , query , variables , operation_name , show_graphiql = False ):
232
237
if not query :
233
238
if show_graphiql :
234
239
return None
235
240
raise HttpError (HttpResponseBadRequest (
236
241
'Must provide query string.' ))
237
242
238
- source = Source (query , name = 'GraphQL request' )
239
-
240
243
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
- )
244
+ backend = self .get_backend (request )
245
+ document = backend .document_from_string (self .schema , query )
248
246
except Exception as e :
249
247
return ExecutionResult (errors = [e ], invalid = True )
250
248
251
249
if request .method .lower () == 'get' :
252
- operation_ast = get_operation_ast ( document_ast , operation_name )
253
- if operation_ast and operation_ast . operation != 'query' :
250
+ operation_type = document . get_operation_type ( operation_name )
251
+ if operation_type and operation_type != 'query' :
254
252
if show_graphiql :
255
253
return None
256
254
257
255
raise HttpError (HttpResponseNotAllowed (
258
256
['POST' ], 'Can only perform a {} operation from a POST request.' .format (
259
- operation_ast . operation )
257
+ operation_type )
260
258
))
261
259
262
260
try :
263
- return self .execute (
264
- document_ast ,
265
- root_value = self .get_root_value (request ),
266
- variable_values = variables ,
261
+ extra_options = {}
262
+ if self .executor :
263
+ # We only include it optionally since
264
+ # executor is not a valid argument in all backends
265
+ extra_options ['executor' ] = self .executor
266
+
267
+ return document .execute (
268
+ root = self .get_root_value (request ),
269
+ variables = variables ,
267
270
operation_name = operation_name ,
268
- context_value = self .get_context (request ),
271
+ context = self .get_context (request ),
269
272
middleware = self .get_middleware (request ),
270
- executor = self . executor ,
273
+ ** extra_options
271
274
)
272
275
except Exception as e :
273
276
return ExecutionResult (errors = [e ], invalid = True )
0 commit comments