|
12 | 12 | from graphql import ExecutionResult, OperationType, execute, get_operation_ast, parse
|
13 | 13 | from graphql.error import GraphQLError
|
14 | 14 | from graphql.execution.middleware import MiddlewareManager
|
| 15 | +from graphql.language import OperationDefinitionNode |
15 | 16 | from graphql.validation import validate
|
16 | 17 |
|
17 | 18 | from graphene import Schema
|
@@ -300,13 +301,23 @@ def execute_graphql_request(
|
300 | 301 |
|
301 | 302 | operation_ast = get_operation_ast(document, operation_name)
|
302 | 303 |
|
303 |
| - op_error = None |
304 | 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." |
| 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." |
308 | 320 |
|
309 |
| - if op_error: |
310 | 321 | return ExecutionResult(errors=[GraphQLError(op_error)])
|
311 | 322 |
|
312 | 323 | if (
|
@@ -347,8 +358,7 @@ def execute_graphql_request(
|
347 | 358 | if (
|
348 | 359 | graphene_settings.ATOMIC_MUTATIONS is True
|
349 | 360 | 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: |
352 | 362 | with transaction.atomic():
|
353 | 363 | result = execute(*execute_args, **execute_options)
|
354 | 364 | if getattr(request, MUTATION_ERRORS_FLAG, False) is True:
|
|
0 commit comments