|
9 | 9 | from django.utils.decorators import method_decorator
|
10 | 10 | from django.views.decorators.csrf import ensure_csrf_cookie
|
11 | 11 | from django.views.generic import View
|
12 |
| -from graphql import OperationType, get_operation_ast, parse |
| 12 | +from graphql import ExecutionResult, OperationType, execute, get_operation_ast, parse |
13 | 13 | from graphql.error import GraphQLError
|
14 |
| -from graphql.execution import ExecutionResult |
15 |
| - |
16 |
| -from graphene import Schema |
17 | 14 | from graphql.execution.middleware import MiddlewareManager
|
| 15 | +from graphql.validation import validate |
18 | 16 |
|
| 17 | +from graphene import Schema |
19 | 18 | from graphene_django.constants import MUTATION_ERRORS_FLAG
|
20 | 19 | from graphene_django.utils.utils import set_rollback
|
21 | 20 |
|
@@ -299,51 +298,64 @@ def execute_graphql_request(
|
299 | 298 | except Exception as e:
|
300 | 299 | return ExecutionResult(errors=[e])
|
301 | 300 |
|
302 |
| - if request.method.lower() == "get": |
303 |
| - operation_ast = get_operation_ast(document, operation_name) |
304 |
| - if operation_ast and operation_ast.operation != OperationType.QUERY: |
305 |
| - if show_graphiql: |
306 |
| - return None |
| 301 | + operation_ast = get_operation_ast(document, operation_name) |
307 | 302 |
|
308 |
| - raise HttpError( |
309 |
| - HttpResponseNotAllowed( |
310 |
| - ["POST"], |
311 |
| - "Can only perform a {} operation from a POST request.".format( |
312 |
| - operation_ast.operation.value |
313 |
| - ), |
314 |
| - ) |
| 303 | + op_error = None |
| 304 | + if not operation_ast: |
| 305 | + op_error = "Must provide a valid operation." |
| 306 | + elif operation_ast.operation == OperationType.SUBSCRIPTION: |
| 307 | + op_error = "The 'subscription' operation is not supported." |
| 308 | + |
| 309 | + if op_error: |
| 310 | + return ExecutionResult(errors=[GraphQLError(op_error)]) |
| 311 | + |
| 312 | + if ( |
| 313 | + request.method.lower() == "get" |
| 314 | + and operation_ast.operation != OperationType.QUERY |
| 315 | + ): |
| 316 | + if show_graphiql: |
| 317 | + return None |
| 318 | + |
| 319 | + raise HttpError( |
| 320 | + HttpResponseNotAllowed( |
| 321 | + ["POST"], |
| 322 | + "Can only perform a {} operation from a POST request.".format( |
| 323 | + operation_ast.operation.value |
| 324 | + ), |
315 | 325 | )
|
316 |
| - try: |
317 |
| - extra_options = {} |
318 |
| - if self.execution_context_class: |
319 |
| - extra_options["execution_context_class"] = self.execution_context_class |
| 326 | + ) |
| 327 | + |
| 328 | + execute_args = (self.schema.graphql_schema, document) |
| 329 | + validation_errors = validate(*execute_args) |
| 330 | + |
| 331 | + if validation_errors: |
| 332 | + return ExecutionResult(data=None, errors=validation_errors) |
320 | 333 |
|
321 |
| - options = { |
322 |
| - "source": query, |
| 334 | + try: |
| 335 | + execute_options = { |
323 | 336 | "root_value": self.get_root_value(request),
|
| 337 | + "context_value": self.get_context(request), |
324 | 338 | "variable_values": variables,
|
325 | 339 | "operation_name": operation_name,
|
326 |
| - "context_value": self.get_context(request), |
327 | 340 | "middleware": self.get_middleware(request),
|
328 | 341 | }
|
329 |
| - options.update(extra_options) |
| 342 | + if self.execution_context_class: |
| 343 | + execute_options[ |
| 344 | + "execution_context_class" |
| 345 | + ] = self.execution_context_class |
330 | 346 |
|
331 |
| - operation_ast = get_operation_ast(document, operation_name) |
332 | 347 | if (
|
333 |
| - operation_ast |
| 348 | + graphene_settings.ATOMIC_MUTATIONS is True |
| 349 | + or connection.settings_dict.get("ATOMIC_MUTATIONS", False) is True |
334 | 350 | and operation_ast.operation == OperationType.MUTATION
|
335 |
| - and ( |
336 |
| - graphene_settings.ATOMIC_MUTATIONS is True |
337 |
| - or connection.settings_dict.get("ATOMIC_MUTATIONS", False) is True |
338 |
| - ) |
339 | 351 | ):
|
340 | 352 | with transaction.atomic():
|
341 |
| - result = self.schema.execute(**options) |
| 353 | + result = execute(*execute_args, **execute_options) |
342 | 354 | if getattr(request, MUTATION_ERRORS_FLAG, False) is True:
|
343 | 355 | transaction.set_rollback(True)
|
344 | 356 | return result
|
345 | 357 |
|
346 |
| - return self.schema.execute(**options) |
| 358 | + return execute(*execute_args, **execute_options) |
347 | 359 | except Exception as e:
|
348 | 360 | return ExecutionResult(errors=[e])
|
349 | 361 |
|
|
0 commit comments