Skip to content

Commit cd096c6

Browse files
committed
Require operation_ast to be found by view handler
1 parent 168ef1f commit cd096c6

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Diff for: graphene_django/views.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from graphql import ExecutionResult, OperationType, execute, get_operation_ast, parse
1313
from graphql.error import GraphQLError
1414
from graphql.execution.middleware import MiddlewareManager
15+
from graphql.language import OperationDefinitionNode
1516
from graphql.validation import validate
1617

1718
from graphene import Schema
@@ -300,13 +301,23 @@ def execute_graphql_request(
300301

301302
operation_ast = get_operation_ast(document, operation_name)
302303

303-
op_error = None
304304
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."
305+
ops_count = len(
306+
[
307+
x
308+
for x in document.definitions
309+
if isinstance(x, OperationDefinitionNode)
310+
]
311+
)
312+
if ops_count > 1:
313+
op_error = (
314+
"Must provide operation name if query contains multiple operations."
315+
)
316+
elif operation_name:
317+
op_error = f"Unknown operation named '{operation_name}'."
318+
else:
319+
op_error = "Must provide a valid operation."
308320

309-
if op_error:
310321
return ExecutionResult(errors=[GraphQLError(op_error)])
311322

312323
if (
@@ -347,8 +358,7 @@ def execute_graphql_request(
347358
if (
348359
graphene_settings.ATOMIC_MUTATIONS is True
349360
or connection.settings_dict.get("ATOMIC_MUTATIONS", False) is True
350-
and operation_ast.operation == OperationType.MUTATION
351-
):
361+
) and operation_ast.operation == OperationType.MUTATION:
352362
with transaction.atomic():
353363
result = execute(*execute_args, **execute_options)
354364
if getattr(request, MUTATION_ERRORS_FLAG, False) is True:

0 commit comments

Comments
 (0)