| 1 |
from operator import attrgetter |
| 2 |
from typing import Any, Collection |
| 3 |
|
| 4 |
from ...error import GraphQLError |
| 5 |
from ...language import ( |
| 6 |
DirectiveDefinitionNode, |
| 7 |
FieldDefinitionNode, |
| 8 |
InputValueDefinitionNode, |
| 9 |
InterfaceTypeDefinitionNode, |
| 10 |
InterfaceTypeExtensionNode, |
| 11 |
NameNode, |
| 12 |
ObjectTypeDefinitionNode, |
| 13 |
ObjectTypeExtensionNode, |
| 14 |
VisitorAction, |
| 15 |
SKIP, |
| 16 |
) |
| 17 |
from ...pyutils import group_by |
| 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 |