Skip to content

Commit 168ef1f

Browse files
committed
Optimize execute_graphql_request
1 parent 5d7a04f commit 168ef1f

File tree

1 file changed

+44
-32
lines changed

1 file changed

+44
-32
lines changed

graphene_django/views.py

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
from django.utils.decorators import method_decorator
1010
from django.views.decorators.csrf import ensure_csrf_cookie
1111
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
1313
from graphql.error import GraphQLError
14-
from graphql.execution import ExecutionResult
15-
16-
from graphene import Schema
1714
from graphql.execution.middleware import MiddlewareManager
15+
from graphql.validation import validate
1816

17+
from graphene import Schema
1918
from graphene_django.constants import MUTATION_ERRORS_FLAG
2019
from graphene_django.utils.utils import set_rollback
2120

@@ -299,51 +298,64 @@ def execute_graphql_request(
299298
except Exception as e:
300299
return ExecutionResult(errors=[e])
301300

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)
307302

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+
),
315325
)
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)
320333

321-
options = {
322-
"source": query,
334+
try:
335+
execute_options = {
323336
"root_value": self.get_root_value(request),
337+
"context_value": self.get_context(request),
324338
"variable_values": variables,
325339
"operation_name": operation_name,
326-
"context_value": self.get_context(request),
327340
"middleware": self.get_middleware(request),
328341
}
329-
options.update(extra_options)
342+
if self.execution_context_class:
343+
execute_options[
344+
"execution_context_class"
345+
] = self.execution_context_class
330346

331-
operation_ast = get_operation_ast(document, operation_name)
332347
if (
333-
operation_ast
348+
graphene_settings.ATOMIC_MUTATIONS is True
349+
or connection.settings_dict.get("ATOMIC_MUTATIONS", False) is True
334350
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-
)
339351
):
340352
with transaction.atomic():
341-
result = self.schema.execute(**options)
353+
result = execute(*execute_args, **execute_options)
342354
if getattr(request, MUTATION_ERRORS_FLAG, False) is True:
343355
transaction.set_rollback(True)
344356
return result
345357

346-
return self.schema.execute(**options)
358+
return execute(*execute_args, **execute_options)
347359
except Exception as e:
348360
return ExecutionResult(errors=[e])
349361

0 commit comments

Comments
 (0)