9
9
from django .utils .decorators import method_decorator
10
10
from django .views .generic import View
11
11
from django .views .decorators .csrf import ensure_csrf_cookie
12
+ from graphene import Schema
13
+ from graphql import parse , get_operation_ast , OperationType , execute , validate
12
14
13
- from graphql import get_default_backend
14
15
from graphql .error import format_error as format_graphql_error
15
16
from graphql .error import GraphQLError
16
17
from graphql .execution import ExecutionResult
@@ -55,10 +56,8 @@ class GraphQLView(View):
55
56
graphiql_template = "graphene/graphiql.html"
56
57
react_version = "16.8.6"
57
58
58
- schema = None
59
+ schema : Schema = None
59
60
graphiql = False
60
- executor = None
61
- backend = None
62
61
middleware = None
63
62
root_value = None
64
63
pretty = False
@@ -67,35 +66,28 @@ class GraphQLView(View):
67
66
def __init__ (
68
67
self ,
69
68
schema = None ,
70
- executor = None ,
71
69
middleware = None ,
72
70
root_value = None ,
73
71
graphiql = False ,
74
72
pretty = False ,
75
73
batch = False ,
76
- backend = None ,
77
74
):
78
75
if not schema :
79
76
schema = graphene_settings .SCHEMA
80
77
81
- if backend is None :
82
- backend = get_default_backend ()
83
-
84
78
if middleware is None :
85
79
middleware = graphene_settings .MIDDLEWARE
86
80
87
81
self .schema = self .schema or schema
88
82
if middleware is not None :
89
83
self .middleware = list (instantiate_middleware (middleware ))
90
- self .executor = executor
91
84
self .root_value = root_value
92
85
self .pretty = self .pretty or pretty
93
86
self .graphiql = self .graphiql or graphiql
94
87
self .batch = self .batch or batch
95
- self .backend = backend
96
88
97
89
assert isinstance (
98
- self .schema , GraphQLSchema
90
+ self .schema , Schema
99
91
), "A Schema is required to be provided to GraphQLView."
100
92
assert not all ((graphiql , batch )), "Use either graphiql or batch processing"
101
93
@@ -109,9 +101,6 @@ def get_middleware(self, request):
109
101
def get_context (self , request ):
110
102
return request
111
103
112
- def get_backend (self , request ):
113
- return self .backend
114
-
115
104
@method_decorator (ensure_csrf_cookie )
116
105
def dispatch (self , request , * args , ** kwargs ):
117
106
try :
@@ -173,7 +162,9 @@ def get_response(self, request, data, show_graphiql=False):
173
162
self .format_error (e ) for e in execution_result .errors
174
163
]
175
164
176
- if execution_result .invalid :
165
+ if execution_result .errors and any (
166
+ not e .path for e in execution_result .errors
167
+ ):
177
168
status_code = 400
178
169
else :
179
170
response ["data" ] = execution_result .data
@@ -246,43 +237,38 @@ def execute_graphql_request(
246
237
raise HttpError (HttpResponseBadRequest ("Must provide query string." ))
247
238
248
239
try :
249
- backend = self .get_backend (request )
250
- document = backend .document_from_string (self .schema , query )
240
+ document = parse (query )
251
241
except Exception as e :
252
- return ExecutionResult (errors = [e ], invalid = True )
242
+ return ExecutionResult (errors = [e ])
253
243
254
244
if request .method .lower () == "get" :
255
- operation_type = document . get_operation_type ( operation_name )
256
- if operation_type and operation_type != "query" :
245
+ operation_ast = get_operation_ast ( document , operation_name )
246
+ if operation_ast and operation_ast . operation != OperationType . QUERY :
257
247
if show_graphiql :
258
248
return None
259
249
260
250
raise HttpError (
261
251
HttpResponseNotAllowed (
262
252
["POST" ],
263
253
"Can only perform a {} operation from a POST request." .format (
264
- operation_type
254
+ operation_ast . operation . value
265
255
),
266
256
)
267
257
)
268
258
269
- try :
270
- extra_options = {}
271
- if self .executor :
272
- # We only include it optionally since
273
- # executor is not a valid argument in all backends
274
- extra_options ["executor" ] = self .executor
275
-
276
- return document .execute (
277
- root = self .get_root_value (request ),
278
- variables = variables ,
279
- operation_name = operation_name ,
280
- context = self .get_context (request ),
281
- middleware = self .get_middleware (request ),
282
- ** extra_options
283
- )
284
- except Exception as e :
285
- return ExecutionResult (errors = [e ], invalid = True )
259
+ validation_errors = validate (self .schema .graphql_schema , document )
260
+ if validation_errors :
261
+ return ExecutionResult (data = None , errors = validation_errors )
262
+
263
+ return execute (
264
+ schema = self .schema .graphql_schema ,
265
+ document = document ,
266
+ root_value = self .get_root_value (request ),
267
+ variable_values = variables ,
268
+ operation_name = operation_name ,
269
+ context_value = self .get_context (request ),
270
+ middleware = self .get_middleware (request ),
271
+ )
286
272
287
273
@classmethod
288
274
def can_display_graphiql (cls , request , data ):
0 commit comments