Skip to content

Commit eb165da

Browse files
committed
Simplify find_deprecated_usages since deprecation_reason is always present
Replicates graphql/graphql-js@62532a5
1 parent 8bda661 commit eb165da

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

src/graphql/utilities/find_deprecated_usages.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,25 @@ def __init__(self, type_info: TypeInfo):
3232
self.errors = []
3333

3434
def enter_field(self, node, *_args):
35+
parent_type = self.type_info.get_parent_type()
3536
field_def = self.type_info.get_field_def()
36-
if field_def and field_def.is_deprecated:
37-
parent_type = self.type_info.get_parent_type()
38-
if parent_type:
39-
field_name = node.name.value
40-
reason = field_def.deprecation_reason
41-
self.errors.append(
42-
GraphQLError(
43-
f"The field '{parent_type.name}.{field_name}'"
44-
" is deprecated." + ("" if reason is None else f" {reason}"),
45-
node,
46-
)
37+
if parent_type and field_def.deprecation_reason is not None:
38+
self.errors.append(
39+
GraphQLError(
40+
f"The field '{parent_type.name}.{node.name.value}'"
41+
" is deprecated. " + field_def.deprecation_reason,
42+
node,
4743
)
44+
)
4845

4946
def enter_enum_value(self, node, *_args):
47+
type_ = get_named_type(self.type_info.get_input_type())
5048
enum_val = self.type_info.get_enum_value()
51-
if enum_val and enum_val.is_deprecated:
52-
type_ = get_named_type(self.type_info.get_input_type())
53-
if type_:
54-
enum_val_name = node.value
55-
reason = enum_val.deprecation_reason
56-
self.errors.append(
57-
GraphQLError(
58-
f"The enum value '{type_.name}.{enum_val_name}'"
59-
" is deprecated." + (f" {reason}" if reason else ""),
60-
node,
61-
)
49+
if type_ and enum_val and enum_val.deprecation_reason is not None:
50+
self.errors.append(
51+
GraphQLError(
52+
f"The enum value '{type_.name}.{node.value}'"
53+
" is deprecated. " + enum_val.deprecation_reason,
54+
node,
6255
)
56+
)

tests/utilities/test_find_deprecated_usages.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def describe_find_deprecated_usages():
77
schema = build_schema(
88
"""
99
enum EnumType {
10-
ONE
11-
TWO @deprecated(reason: "Some enum reason.")
10+
NORMAL_VALUE
11+
DEPRECATED_VALUE @deprecated(reason: "Some enum reason.")
1212
}
1313
1414
type Query {
@@ -19,7 +19,9 @@ def describe_find_deprecated_usages():
1919
)
2020

2121
def should_report_empty_set_for_no_deprecated_usages():
22-
errors = find_deprecated_usages(schema, parse("{ normalField(enumArg: ONE) }"))
22+
errors = find_deprecated_usages(
23+
schema, parse("{ normalField(enumArg: [NORMAL_VALUE]) }")
24+
)
2325

2426
assert errors == []
2527

@@ -35,10 +37,20 @@ def should_report_usage_of_deprecated_fields():
3537
]
3638

3739
def should_report_usage_of_deprecated_enums():
38-
errors = find_deprecated_usages(schema, parse("{ normalField(enumArg: TWO) }"))
40+
errors = find_deprecated_usages(
41+
schema,
42+
parse(
43+
"""
44+
{
45+
normalField(enumArg: [NORMAL_VALUE, DEPRECATED_VALUE])
46+
}
47+
"""
48+
),
49+
)
3950

4051
error_messages = [err.message for err in errors]
4152

4253
assert error_messages == [
43-
"The enum value 'EnumType.TWO' is deprecated. Some enum reason."
54+
"The enum value 'EnumType.DEPRECATED_VALUE' is deprecated."
55+
" Some enum reason."
4456
]

0 commit comments

Comments
 (0)