Debian Codegraph @

src/graphql/validation/rules/custom/no_deprecated.py

raw
1 from typing import Any, cast
2
3 from ....error import GraphQLError
4 from ....language import ArgumentNode, EnumValueNode, FieldNode, ObjectFieldNode
5 from ....type import GraphQLInputObjectType, get_named_type, is_input_object_type
6 from .. import ValidationRule
7
8 __all__ = ["NoDeprecatedCustomRule"]
9
10
11 class NoDeprecatedCustomRule(ValidationRule):
12 """No deprecated
13
14 A GraphQL document is only valid if all selected fields and all used enum values
15 have not been deprecated.
16
17 Note: This rule is optional and is not part of the Validation section of the GraphQL
18 Specification. The main purpose of this rule is detection of deprecated usages and
19 not necessarily to forbid their use when querying a service.
20 """
21
22 def enter_field(self, node: FieldNode, *_args: Any) -> None:
23 context = self.context
24 field_def = context.get_field_def()
25 if field_def:
26 deprecation_reason = field_def.deprecation_reason
27 if deprecation_reason is not None:
28 parent_type = context.get_parent_type()
29 parent_name = parent_type.name # type: ignore
30 self.report_error(
31 GraphQLError(
32 f"The field {parent_name}.{node.name.value}"
33 f" is deprecated. {deprecation_reason}",
34 node,
35 )
36 )
37
38 def enter_argument(self, node: ArgumentNode, *_args: Any) -> None:
39 context = self.context
40 arg_def = context.get_argument()
41 if arg_def:
42 deprecation_reason = arg_def.deprecation_reason
43 if deprecation_reason is not None:
44 directive_def = context.get_directive()
45 arg_name = node.name.value
46 if directive_def is None:
47 parent_type = context.get_parent_type()
48 parent_name = parent_type.name # type: ignore
49 field_def = context.get_field_def()
50 field_name = field_def.ast_node.name.value # type: ignore
51 self.report_error(
52 GraphQLError(
53 f"Field '{parent_name}.{field_name}' argument"
54 f" '{arg_name}' is deprecated. {deprecation_reason}",
55 node,
56 )
57 )
58 else:
59 self.report_error(
60 GraphQLError(
61 f"Directive '@{directive_def.name}' argument"
62 f" '{arg_name}' is deprecated. {deprecation_reason}",
63 node,
64 )
65 )
66
67 def enter_object_field(self, node: ObjectFieldNode, *_args: Any) -> None:
68 context = self.context
69 input_object_def = get_named_type(context.get_parent_input_type())
70 if is_input_object_type(input_object_def):
71 input_field_def = cast(GraphQLInputObjectType, input_object_def).fields.get(
72 node.name.value
73 )
74 if input_field_def:
75 deprecation_reason = input_field_def.deprecation_reason
76 if deprecation_reason is not None:
77 field_name = node.name.value
78 input_object_name = input_object_def.name # type: ignore
79 self.report_error(
80 GraphQLError(
81 f"The input field {input_object_name}.{field_name}"
82 f" is deprecated. {deprecation_reason}",
83 node,
84 )
85 )
86
87 def enter_enum_value(self, node: EnumValueNode, *_args: Any) -> None:
88 context = self.context
89 enum_value_def = context.get_enum_value()
90 if enum_value_def:
91 deprecation_reason = enum_value_def.deprecation_reason
92 if deprecation_reason is not None: # pragma: no cover else
93 enum_type_def = get_named_type(context.get_input_type())
94 enum_type_name = enum_type_def.name # type: ignore
95 self.report_error(
96 GraphQLError(
97 f"The enum value '{enum_type_name}.{node.value}'"
98 f" is deprecated. {deprecation_reason}",
99 node,
100 )
101 )