| 1 |
from typing import cast, Any, Dict, List, Union |
| 2 |
|
| 3 |
from ...error import GraphQLError |
| 4 |
from ...language import ( |
| 5 |
DirectiveDefinitionNode, |
| 6 |
DirectiveNode, |
| 7 |
FieldNode, |
| 8 |
InputValueDefinitionNode, |
| 9 |
NonNullTypeNode, |
| 10 |
TypeNode, |
| 11 |
VisitorAction, |
| 12 |
SKIP, |
| 13 |
print_ast, |
| 14 |
) |
| 15 |
from ...type import GraphQLArgument, is_required_argument, is_type, specified_directives |
| 16 |
from . import ASTValidationRule, SDLValidationContext, ValidationContext |
| 17 |
|
| 18 |
__all__ = ["ProvidedRequiredArgumentsRule", "ProvidedRequiredArgumentsOnDirectivesRule"] |
| 19 |
|
| 20 |
|
| 21 |
class ProvidedRequiredArgumentsOnDirectivesRule(ASTValidationRule): |
| 22 |
"""Provided required arguments on directives |
| 23 |
|
| 24 |
A directive is only valid if all required (non-null without a default value) |
| 25 |
arguments have been provided. |
| 26 |
|
| 27 |
For internal use only. |
| 28 |
""" |
| 29 |
|
| 30 |
context: Union[ValidationContext, SDLValidationContext] |
| 31 |
|
| 32 |
def __init__(self, context: Union[ValidationContext, SDLValidationContext]): |
| 33 |
super().__init__(context) |
| 34 |
required_args_map: Dict[ |
| 35 |
str, Dict[str, Union[GraphQLArgument, InputValueDefinitionNode]] |
| 36 |
] = {} |
| 37 |
|
| 38 |
schema = context.schema |
| 39 |
defined_directives = schema.directives if schema else specified_directives |
| 40 |
for directive in cast(List, defined_directives): |
| 41 |
required_args_map[directive.name] = { |
| 42 |
name: arg |
| 43 |
for name, arg in directive.args.items() |
| 44 |
if is_required_argument(arg) |
| 45 |
} |
| 46 |
|
| 47 |
ast_definitions = context.document.definitions |
| 48 |
for def_ in ast_definitions: |
| 49 |
if isinstance(def_, DirectiveDefinitionNode): |
| 50 |
required_args_map[def_.name.value] = { |
| 51 |
arg.name.value: arg |
| 52 |
for arg in filter(is_required_argument_node, def_.arguments or ()) |
| 53 |
} |
| 54 |
|
| 55 |
self.required_args_map = required_args_map |
| 56 |
|
| 57 |
def leave_directive(self, directive_node: DirectiveNode, *_args: Any) -> None: |
| 58 |
# Validate on leave to allow for deeper errors to appear first. |
| 59 |
directive_name = directive_node.name.value |
| 60 |
required_args = self.required_args_map.get(directive_name) |
| 61 |
if required_args: |
| 62 |
|
| 63 |
arg_nodes = directive_node.arguments or () |
| 64 |
arg_node_set = {arg.name.value for arg in arg_nodes} |
| 65 |
for arg_name in required_args: |
| 66 |
if arg_name not in arg_node_set: |
| 67 |
arg_type = required_args[arg_name].type |
| 68 |
arg_type_str = ( |
| 69 |
str(arg_type) |
| 70 |
if is_type(arg_type) |
| 71 |
else print_ast(cast(TypeNode, arg_type)) |
| 72 |
) |
| 73 |
self.report_error( |
| 74 |
GraphQLError( |
| 75 |
f"Directive '@{directive_name}' argument '{arg_name}'" |
| 76 |
f" of type '{arg_type_str}' is required," |
| 77 |
" but it was not provided.", |
| 78 |
directive_node, |
| 79 |
) |
| 80 |
) |
| 81 |
|
| 82 |
|
| 83 |
class ProvidedRequiredArgumentsRule(ProvidedRequiredArgumentsOnDirectivesRule): |
| 84 |
"""Provided required arguments |
| 85 |
|
| 86 |
A field or directive is only valid if all required (non-null without a default |
| 87 |
value) field arguments have been provided. |
| 88 |
""" |
| 89 |
|
| 90 |
context: ValidationContext |
| 91 |
|
| 92 |
def __init__(self, context: ValidationContext): |
| 93 |
super().__init__(context) |
| 94 |
|
| 95 |
def leave_field(self, field_node: FieldNode, *_args: Any) -> VisitorAction: |
| 96 |
# Validate on leave to allow for deeper errors to appear first. |
| 97 |
field_def = self.context.get_field_def() |
| 98 |
if not field_def: |
| 99 |
return SKIP |
| 100 |
arg_nodes = field_node.arguments or () |
| 101 |
|
| 102 |
arg_node_map = {arg.name.value: arg for arg in arg_nodes} |
| 103 |
for arg_name, arg_def in field_def.args.items(): |
| 104 |
arg_node = arg_node_map.get(arg_name) |
| 105 |
if not arg_node and is_required_argument(arg_def): |
| 106 |
self.report_error( |
| 107 |
GraphQLError( |
| 108 |
f"Field '{field_node.name.value}' argument '{arg_name}'" |
| 109 |
f" of type '{arg_def.type}' is required," |
| 110 |
" but it was not provided.", |
| 111 |
field_node, |
| 112 |
) |
| 113 |
) |
| 114 |
|
| 115 |
return None |
| 116 |
|
| 117 |
|
| 118 |
def is_required_argument_node(arg: InputValueDefinitionNode) -> bool: |
| 119 |
return isinstance(arg.type, NonNullTypeNode) and arg.default_value is None |