Skip to content

Commit e735f5d

Browse files
danthewildcatfiraskafrikiendang
authored
Optimize views (#1439)
* Optimize execute_graphql_request * Require operation_ast to be found by view handler * Remove unused show_graphiql kwarg * Old style if syntax * Revert "Remove unused show_graphiql kwarg" This reverts commit 33b3426. * Add missing schema validation step * Pass args directly to improve clarity * Remove duplicated operation_ast not None check --------- Co-authored-by: Firas Kafri <3097061+firaskafri@users.noreply.github.com> Co-authored-by: Kien Dang <mail@kien.ai>
1 parent 36cf100 commit e735f5d

File tree

1 file changed

+46
-26
lines changed

1 file changed

+46
-26
lines changed

graphene_django/views.py

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@
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 (
13+
ExecutionResult,
14+
OperationType,
15+
execute,
16+
get_operation_ast,
17+
parse,
18+
validate_schema,
19+
)
1320
from graphql.error import GraphQLError
14-
from graphql.execution import ExecutionResult
1521
from graphql.execution.middleware import MiddlewareManager
22+
from graphql.validation import validate
1623

1724
from graphene import Schema
1825
from graphene_django.constants import MUTATION_ERRORS_FLAG
@@ -295,56 +302,69 @@ def execute_graphql_request(
295302
return None
296303
raise HttpError(HttpResponseBadRequest("Must provide query string."))
297304

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+
298311
try:
299312
document = parse(query)
300313
except Exception as e:
301314
return ExecutionResult(errors=[e])
302315

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

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+
),
316332
)
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)
321339

322-
options = {
323-
"source": query,
340+
try:
341+
execute_options = {
324342
"root_value": self.get_root_value(request),
343+
"context_value": self.get_context(request),
325344
"variable_values": variables,
326345
"operation_name": operation_name,
327-
"context_value": self.get_context(request),
328346
"middleware": self.get_middleware(request),
329347
}
330-
options.update(extra_options)
348+
if self.execution_context_class:
349+
execute_options[
350+
"execution_context_class"
351+
] = self.execution_context_class
331352

332-
operation_ast = get_operation_ast(document, operation_name)
333353
if (
334-
operation_ast
354+
operation_ast is not None
335355
and operation_ast.operation == OperationType.MUTATION
336356
and (
337357
graphene_settings.ATOMIC_MUTATIONS is True
338358
or connection.settings_dict.get("ATOMIC_MUTATIONS", False) is True
339359
)
340360
):
341361
with transaction.atomic():
342-
result = self.schema.execute(**options)
362+
result = execute(schema, document, **execute_options)
343363
if getattr(request, MUTATION_ERRORS_FLAG, False) is True:
344364
transaction.set_rollback(True)
345365
return result
346366

347-
return self.schema.execute(**options)
367+
return execute(schema, document, **execute_options)
348368
except Exception as e:
349369
return ExecutionResult(errors=[e])
350370

0 commit comments

Comments
 (0)