| 1 |
from typing import cast, Any, Dict, List, Union |
| 2 |
|
| 3 |
from ...error import GraphQLError |
| 4 |
from ...language import ( |
| 5 |
ArgumentNode, |
| 6 |
DirectiveDefinitionNode, |
| 7 |
DirectiveNode, |
| 8 |
SKIP, |
| 9 |
VisitorAction, |
| 10 |
) |
| 11 |
from ...pyutils import did_you_mean, suggestion_list |
| 12 |
from ...type import specified_directives |
| 13 |
from . import ASTValidationRule, SDLValidationContext, ValidationContext |
| 14 |
|
| 15 |
__all__ = ["KnownArgumentNamesRule", "KnownArgumentNamesOnDirectivesRule"] |
| 16 |
|
| 17 |
|
| 18 |
class KnownArgumentNamesOnDirectivesRule(ASTValidationRule): |
| 19 |
"""Known argument names on directives |
| 20 |
|
| 21 |
A GraphQL directive is only valid if all supplied arguments are defined. |
| 22 |
|
| 23 |
For internal use only. |
| 24 |
""" |
| 25 |
|
| 26 |
context: Union[ValidationContext, SDLValidationContext] |
| 27 |
|
| 28 |
def __init__(self, context: Union[ValidationContext, SDLValidationContext]): |
| 29 |
super().__init__(context) |
| 30 |
directive_args: Dict[str, List[str]] = {} |
| 31 |
|
| 32 |
schema = context.schema |
| 33 |
defined_directives = schema.directives if schema else specified_directives |
| 34 |
for directive in cast(List, defined_directives): |
| 35 |
directive_args[directive.name] = list(directive.args) |
| 36 |
|
| 37 |
ast_definitions = context.document.definitions |
| 38 |
for def_ in ast_definitions: |
| 39 |
if isinstance(def_, DirectiveDefinitionNode): |
| 40 |
directive_args[def_.name.value] = [ |
| 41 |
arg.name.value for arg in def_.arguments or [] |
| 42 |
] |
| 43 |
|
| 44 |
self.directive_args = directive_args |
| 45 |
|
| 46 |
def enter_directive( |
| 47 |
self, directive_node: DirectiveNode, *_args: Any |
| 48 |
) -> VisitorAction: |
| 49 |
directive_name = directive_node.name.value |
| 50 |
known_args = self.directive_args.get(directive_name) |
| 51 |
if directive_node.arguments and known_args is not None: |
| 52 |
for arg_node in directive_node.arguments: |
| 53 |
arg_name = arg_node.name.value |
| 54 |
if arg_name not in known_args: |
| 55 |
suggestions = suggestion_list(arg_name, known_args) |
| 56 |
self.report_error( |
| 57 |
GraphQLError( |
| 58 |
f"Unknown argument '{arg_name}'" |
| 59 |
f" on directive '@{directive_name}'." |
| 60 |
+ did_you_mean(suggestions), |
| 61 |
arg_node, |
| 62 |
) |
| 63 |
) |
| 64 |
return SKIP |
| 65 |
|
| 66 |
|
| 67 |
class KnownArgumentNamesRule(KnownArgumentNamesOnDirectivesRule): |
| 68 |
"""Known argument names |
| 69 |
|
| 70 |
A GraphQL field is only valid if all supplied arguments are defined by that field. |
| 71 |
|
| 72 |
See https://spec.graphql.org/draft/#sec-Argument-Names |
| 73 |
See https://spec.graphql.org/draft/#sec-Directives-Are-In-Valid-Locations |
| 74 |
""" |
| 75 |
|
| 76 |
context: ValidationContext |
| 77 |
|
| 78 |
def __init__(self, context: ValidationContext): |
| 79 |
super().__init__(context) |
| 80 |
|
| 81 |
def enter_argument(self, arg_node: ArgumentNode, *args: Any) -> None: |
| 82 |
context = self.context |
| 83 |
arg_def = context.get_argument() |
| 84 |
field_def = context.get_field_def() |
| 85 |
parent_type = context.get_parent_type() |
| 86 |
if not arg_def and field_def and parent_type: |
| 87 |
arg_name = arg_node.name.value |
| 88 |
field_name = args[3][-1].name.value |
| 89 |
known_args_names = list(field_def.args) |
| 90 |
suggestions = suggestion_list(arg_name, known_args_names) |
| 91 |
context.report_error( |
| 92 |
GraphQLError( |
| 93 |
f"Unknown argument '{arg_name}'" |
| 94 |
f" on field '{parent_type.name}.{field_name}'." |
| 95 |
+ did_you_mean(suggestions), |
| 96 |
arg_node, |
| 97 |
) |
| 98 |
) |