|
| 1 | +from collections import defaultdict |
| 2 | +from operator import attrgetter |
| 3 | +from typing import Any, Callable, Collection, Dict, List, TypeVar |
| 4 | + |
| 5 | +from ...error import GraphQLError |
| 6 | +from ...language import ( |
| 7 | + DirectiveDefinitionNode, |
| 8 | + FieldDefinitionNode, |
| 9 | + InputValueDefinitionNode, |
| 10 | + InterfaceTypeDefinitionNode, |
| 11 | + InterfaceTypeExtensionNode, |
| 12 | + NameNode, |
| 13 | + ObjectTypeDefinitionNode, |
| 14 | + ObjectTypeExtensionNode, |
| 15 | + VisitorAction, |
| 16 | + SKIP, |
| 17 | +) |
| 18 | +from . import SDLValidationRule |
| 19 | + |
| 20 | +__all__ = ["UniqueArgumentDefinitionNamesRule"] |
| 21 | + |
| 22 | + |
| 23 | +class UniqueArgumentDefinitionNamesRule(SDLValidationRule): |
| 24 | + """Unique argument definition names |
| 25 | +
|
| 26 | + A GraphQL Object or Interface type is only valid if all its fields have uniquely |
| 27 | + named arguments. |
| 28 | + A GraphQL Directive is only valid if all its arguments are uniquely named. |
| 29 | +
|
| 30 | + See https://spec.graphql.org/draft/#sec-Argument-Uniqueness |
| 31 | + """ |
| 32 | + |
| 33 | + def enter_directive_definition( |
| 34 | + self, node: DirectiveDefinitionNode, *_args: Any |
| 35 | + ) -> VisitorAction: |
| 36 | + return self.check_arg_uniqueness(f"@{node.name.value}", node.arguments) |
| 37 | + |
| 38 | + def enter_interface_type_definition( |
| 39 | + self, node: InterfaceTypeDefinitionNode, *_args: Any |
| 40 | + ) -> VisitorAction: |
| 41 | + return self.check_arg_uniqueness_per_field(node.name, node.fields) |
| 42 | + |
| 43 | + def enter_interface_type_extension( |
| 44 | + self, node: InterfaceTypeExtensionNode, *_args: Any |
| 45 | + ) -> VisitorAction: |
| 46 | + return self.check_arg_uniqueness_per_field(node.name, node.fields) |
| 47 | + |
| 48 | + def enter_object_type_definition( |
| 49 | + self, node: ObjectTypeDefinitionNode, *_args: Any |
| 50 | + ) -> VisitorAction: |
| 51 | + return self.check_arg_uniqueness_per_field(node.name, node.fields) |
| 52 | + |
| 53 | + def enter_object_type_extension( |
| 54 | + self, node: ObjectTypeExtensionNode, *_args: Any |
| 55 | + ) -> VisitorAction: |
| 56 | + return self.check_arg_uniqueness_per_field(node.name, node.fields) |
| 57 | + |
| 58 | + def check_arg_uniqueness_per_field( |
| 59 | + self, |
| 60 | + name: NameNode, |
| 61 | + fields: Collection[FieldDefinitionNode], |
| 62 | + ) -> VisitorAction: |
| 63 | + type_name = name.value |
| 64 | + for field_def in fields: |
| 65 | + field_name = field_def.name.value |
| 66 | + argument_nodes = field_def.arguments or () |
| 67 | + self.check_arg_uniqueness(f"{type_name}.{field_name}", argument_nodes) |
| 68 | + return SKIP |
| 69 | + |
| 70 | + def check_arg_uniqueness( |
| 71 | + self, parent_name: str, argument_nodes: Collection[InputValueDefinitionNode] |
| 72 | + ) -> VisitorAction: |
| 73 | + seen_args = group_by(argument_nodes, attrgetter("name.value")) |
| 74 | + for arg_name, arg_nodes in seen_args.items(): |
| 75 | + if len(arg_nodes) > 1: |
| 76 | + self.report_error( |
| 77 | + GraphQLError( |
| 78 | + f"Argument '{parent_name}({arg_name}:)'" |
| 79 | + " can only be defined once.", |
| 80 | + [node.name for node in arg_nodes], |
| 81 | + ) |
| 82 | + ) |
| 83 | + return SKIP |
| 84 | + |
| 85 | + |
| 86 | +K = TypeVar("K") |
| 87 | +T = TypeVar("T") |
| 88 | + |
| 89 | + |
| 90 | +def group_by(items: Collection[T], key_fn: Callable[[T], K]) -> Dict[K, List[T]]: |
| 91 | + """Group an unsorted collection of items by a key derived via a function.""" |
| 92 | + result: Dict[K, List[T]] = defaultdict(list) |
| 93 | + for item in items: |
| 94 | + key = key_fn(item) |
| 95 | + result[key].append(item) |
| 96 | + return result |
0 commit comments