| 1 |
from typing import Any, Union, cast |
| 2 |
|
| 3 |
from ...error import GraphQLError |
| 4 |
from ...language import ( |
| 5 |
DirectiveDefinitionNode, |
| 6 |
DocumentNode, |
| 7 |
ExecutableDefinitionNode, |
| 8 |
SchemaDefinitionNode, |
| 9 |
SchemaExtensionNode, |
| 10 |
TypeDefinitionNode, |
| 11 |
VisitorAction, |
| 12 |
SKIP, |
| 13 |
) |
| 14 |
from . import ASTValidationRule |
| 15 |
|
| 16 |
__all__ = ["ExecutableDefinitionsRule"] |
| 17 |
|
| 18 |
|
| 19 |
class ExecutableDefinitionsRule(ASTValidationRule): |
| 20 |
"""Executable definitions |
| 21 |
|
| 22 |
A GraphQL document is only valid for execution if all definitions are either |
| 23 |
operation or fragment definitions. |
| 24 |
|
| 25 |
See https://spec.graphql.org/draft/#sec-Executable-Definitions |
| 26 |
""" |
| 27 |
|
| 28 |
def enter_document(self, node: DocumentNode, *_args: Any) -> VisitorAction: |
| 29 |
for definition in node.definitions: |
| 30 |
if not isinstance(definition, ExecutableDefinitionNode): |
| 31 |
def_name = ( |
| 32 |
"schema" |
| 33 |
if isinstance( |
| 34 |
definition, (SchemaDefinitionNode, SchemaExtensionNode) |
| 35 |
) |
| 36 |
else "'{}'".format( |
| 37 |
cast( |
| 38 |
Union[DirectiveDefinitionNode, TypeDefinitionNode], |
| 39 |
definition, |
| 40 |
).name.value |
| 41 |
) |
| 42 |
) |
| 43 |
self.report_error( |
| 44 |
GraphQLError( |
| 45 |
f"The {def_name} definition is not executable.", |
| 46 |
definition, |
| 47 |
) |
| 48 |
) |
| 49 |
return SKIP |