| 1 |
from typing import cast, Any, Dict, List, Optional, Tuple, Union |
| 2 |
|
| 3 |
from ...error import GraphQLError |
| 4 |
from ...language import ( |
| 5 |
DirectiveLocation, |
| 6 |
DirectiveDefinitionNode, |
| 7 |
DirectiveNode, |
| 8 |
Node, |
| 9 |
OperationDefinitionNode, |
| 10 |
) |
| 11 |
from ...type import specified_directives |
| 12 |
from . import ASTValidationRule, SDLValidationContext, ValidationContext |
| 13 |
|
| 14 |
__all__ = ["KnownDirectivesRule"] |
| 15 |
|
| 16 |
|
| 17 |
class KnownDirectivesRule(ASTValidationRule): |
| 18 |
"""Known directives |
| 19 |
|
| 20 |
A GraphQL document is only valid if all ``@directives`` are known by the schema and |
| 21 |
legally positioned. |
| 22 |
|
| 23 |
See https://spec.graphql.org/draft/#sec-Directives-Are-Defined |
| 24 |
""" |
| 25 |
|
| 26 |
context: Union[ValidationContext, SDLValidationContext] |
| 27 |
|
| 28 |
def __init__(self, context: Union[ValidationContext, SDLValidationContext]): |
| 29 |
super().__init__(context) |
| 30 |
locations_map: Dict[str, Tuple[DirectiveLocation, ...]] = {} |
| 31 |
|
| 32 |
schema = context.schema |
| 33 |
defined_directives = ( |
| 34 |
schema.directives if schema else cast(List, specified_directives) |
| 35 |
) |
| 36 |
for directive in defined_directives: |
| 37 |
locations_map[directive.name] = directive.locations |
| 38 |
ast_definitions = context.document.definitions |
| 39 |
for def_ in ast_definitions: |
| 40 |
if isinstance(def_, DirectiveDefinitionNode): |
| 41 |
locations_map[def_.name.value] = tuple( |
| 42 |
DirectiveLocation[name.value] for name in def_.locations |
| 43 |
) |
| 44 |
self.locations_map = locations_map |
| 45 |
|
| 46 |
def enter_directive( |
| 47 |
self, |
| 48 |
node: DirectiveNode, |
| 49 |
_key: Any, |
| 50 |
_parent: Any, |
| 51 |
_path: Any, |
| 52 |
ancestors: List[Node], |
| 53 |
) -> None: |
| 54 |
name = node.name.value |
| 55 |
locations = self.locations_map.get(name) |
| 56 |
if locations: |
| 57 |
candidate_location = get_directive_location_for_ast_path(ancestors) |
| 58 |
if candidate_location and candidate_location not in locations: |
| 59 |
self.report_error( |
| 60 |
GraphQLError( |
| 61 |
f"Directive '@{name}'" |
| 62 |
f" may not be used on {candidate_location.value}.", |
| 63 |
node, |
| 64 |
) |
| 65 |
) |
| 66 |
else: |
| 67 |
self.report_error(GraphQLError(f"Unknown directive '@{name}'.", node)) |
| 68 |
|
| 69 |
|
| 70 |
_operation_location = { |
| 71 |
"query": DirectiveLocation.QUERY, |
| 72 |
"mutation": DirectiveLocation.MUTATION, |
| 73 |
"subscription": DirectiveLocation.SUBSCRIPTION, |
| 74 |
} |
| 75 |
|
| 76 |
_directive_location = { |
| 77 |
"field": DirectiveLocation.FIELD, |
| 78 |
"fragment_spread": DirectiveLocation.FRAGMENT_SPREAD, |
| 79 |
"inline_fragment": DirectiveLocation.INLINE_FRAGMENT, |
| 80 |
"fragment_definition": DirectiveLocation.FRAGMENT_DEFINITION, |
| 81 |
"variable_definition": DirectiveLocation.VARIABLE_DEFINITION, |
| 82 |
"schema_definition": DirectiveLocation.SCHEMA, |
| 83 |
"schema_extension": DirectiveLocation.SCHEMA, |
| 84 |
"scalar_type_definition": DirectiveLocation.SCALAR, |
| 85 |
"scalar_type_extension": DirectiveLocation.SCALAR, |
| 86 |
"object_type_definition": DirectiveLocation.OBJECT, |
| 87 |
"object_type_extension": DirectiveLocation.OBJECT, |
| 88 |
"field_definition": DirectiveLocation.FIELD_DEFINITION, |
| 89 |
"interface_type_definition": DirectiveLocation.INTERFACE, |
| 90 |
"interface_type_extension": DirectiveLocation.INTERFACE, |
| 91 |
"union_type_definition": DirectiveLocation.UNION, |
| 92 |
"union_type_extension": DirectiveLocation.UNION, |
| 93 |
"enum_type_definition": DirectiveLocation.ENUM, |
| 94 |
"enum_type_extension": DirectiveLocation.ENUM, |
| 95 |
"enum_value_definition": DirectiveLocation.ENUM_VALUE, |
| 96 |
"input_object_type_definition": DirectiveLocation.INPUT_OBJECT, |
| 97 |
"input_object_type_extension": DirectiveLocation.INPUT_OBJECT, |
| 98 |
"directive_definition": DirectiveLocation.DIRECTIVE_DEFINITION, |
| 99 |
"directive_extension": DirectiveLocation.DIRECTIVE_DEFINITION, |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
def get_directive_location_for_ast_path( |
| 104 |
ancestors: List[Node], |
| 105 |
) -> Optional[DirectiveLocation]: |
| 106 |
applied_to = ancestors[-1] |
| 107 |
if not isinstance(applied_to, Node): # pragma: no cover |
| 108 |
raise TypeError("Unexpected error in directive.") |
| 109 |
kind = applied_to.kind |
| 110 |
if kind == "operation_definition": |
| 111 |
applied_to = cast(OperationDefinitionNode, applied_to) |
| 112 |
return _operation_location[applied_to.operation.value] |
| 113 |
elif kind == "input_value_definition": |
| 114 |
parent_node = ancestors[-3] |
| 115 |
return ( |
| 116 |
DirectiveLocation.INPUT_FIELD_DEFINITION |
| 117 |
if parent_node.kind == "input_object_type_definition" |
| 118 |
else DirectiveLocation.ARGUMENT_DEFINITION |
| 119 |
) |
| 120 |
else: |
| 121 |
return _directive_location.get(kind) |