|
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 ( |
| 13 | + ExecutionResult, |
| 14 | + OperationType, |
| 15 | + execute, |
| 16 | + get_operation_ast, |
| 17 | + parse, |
| 18 | + validate_schema, |
| 19 | +) |
13 | 20 | from graphql.error import GraphQLError |
14 | | -from graphql.execution import ExecutionResult |
15 | 21 | from graphql.execution.middleware import MiddlewareManager |
| 22 | +from graphql.validation import validate |
16 | 23 |
|
17 | 24 | from graphene import Schema |
18 | 25 | from graphene_django.constants import MUTATION_ERRORS_FLAG |
@@ -295,56 +302,69 @@ def execute_graphql_request( |
295 | 302 | return None |
296 | 303 | raise HttpError(HttpResponseBadRequest("Must provide query string.")) |
297 | 304 |
|
| 305 | + schema = self.schema.graphql_schema |
| 306 | + |
| 307 | + schema_validation_errors = validate_schema(schema) |
| 308 | + if schema_validation_errors: |
| 309 | + return ExecutionResult(data=None, errors=schema_validation_errors) |
| 310 | + |
298 | 311 | try: |
299 | 312 | document = parse(query) |
300 | 313 | except Exception as e: |
301 | 314 | return ExecutionResult(errors=[e]) |
302 | 315 |
|
303 | | - if request.method.lower() == "get": |
304 | | - operation_ast = get_operation_ast(document, operation_name) |
305 | | - if operation_ast and operation_ast.operation != OperationType.QUERY: |
306 | | - if show_graphiql: |
307 | | - return None |
| 316 | + operation_ast = get_operation_ast(document, operation_name) |
308 | 317 |
|
309 | | - raise HttpError( |
310 | | - HttpResponseNotAllowed( |
311 | | - ["POST"], |
312 | | - "Can only perform a {} operation from a POST request.".format( |
313 | | - operation_ast.operation.value |
314 | | - ), |
315 | | - ) |
| 318 | + if ( |
| 319 | + request.method.lower() == "get" |
| 320 | + and operation_ast is not None |
| 321 | + and operation_ast.operation != OperationType.QUERY |
| 322 | + ): |
| 323 | + if show_graphiql: |
| 324 | + return None |
| 325 | + |
| 326 | + raise HttpError( |
| 327 | + HttpResponseNotAllowed( |
| 328 | + ["POST"], |
| 329 | + "Can only perform a {} operation from a POST request.".format( |
| 330 | + operation_ast.operation.value |
| 331 | + ), |
316 | 332 | ) |
317 | | - try: |
318 | | - extra_options = {} |
319 | | - if self.execution_context_class: |
320 | | - extra_options["execution_context_class"] = self.execution_context_class |
| 333 | + ) |
| 334 | + |
| 335 | + validation_errors = validate(schema, document) |
| 336 | + |
| 337 | + if validation_errors: |
| 338 | + return ExecutionResult(data=None, errors=validation_errors) |
321 | 339 |
|
322 | | - options = { |
323 | | - "source": query, |
| 340 | + try: |
| 341 | + execute_options = { |
324 | 342 | "root_value": self.get_root_value(request), |
| 343 | + "context_value": self.get_context(request), |
325 | 344 | "variable_values": variables, |
326 | 345 | "operation_name": operation_name, |
327 | | - "context_value": self.get_context(request), |
328 | 346 | "middleware": self.get_middleware(request), |
329 | 347 | } |
330 | | - options.update(extra_options) |
| 348 | + if self.execution_context_class: |
| 349 | + execute_options[ |
| 350 | + "execution_context_class" |
| 351 | + ] = self.execution_context_class |
331 | 352 |
|
332 | | - operation_ast = get_operation_ast(document, operation_name) |
333 | 353 | if ( |
334 | | - operation_ast |
| 354 | + operation_ast is not None |
335 | 355 | and operation_ast.operation == OperationType.MUTATION |
336 | 356 | and ( |
337 | 357 | graphene_settings.ATOMIC_MUTATIONS is True |
338 | 358 | or connection.settings_dict.get("ATOMIC_MUTATIONS", False) is True |
339 | 359 | ) |
340 | 360 | ): |
341 | 361 | with transaction.atomic(): |
342 | | - result = self.schema.execute(**options) |
| 362 | + result = execute(schema, document, **execute_options) |
343 | 363 | if getattr(request, MUTATION_ERRORS_FLAG, False) is True: |
344 | 364 | transaction.set_rollback(True) |
345 | 365 | return result |
346 | 366 |
|
347 | | - return self.schema.execute(**options) |
| 367 | + return execute(schema, document, **execute_options) |
348 | 368 | except Exception as e: |
349 | 369 | return ExecutionResult(errors=[e]) |
350 | 370 |
|
|
0 commit comments