Skip to content

Commit 226bc47

Browse files
committed
introspection: Add missing support for deprecated input values
Replicates graphql/graphql-js@3f129b5
1 parent 102ba85 commit 226bc47

File tree

6 files changed

+83
-8
lines changed

6 files changed

+83
-8
lines changed

src/graphql/utilities/build_client_schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ def build_argument(argument_introspection: Dict) -> GraphQLArgument:
267267
type_,
268268
default_value=default_value,
269269
description=argument_introspection.get("description"),
270+
deprecation_reason=argument_introspection.get("deprecationReason"),
270271
)
271272

272273
def build_input_value_def_map(
@@ -298,6 +299,7 @@ def build_input_value(input_value_introspection: Dict) -> GraphQLInputField:
298299
type_,
299300
default_value=default_value,
300301
description=input_value_introspection.get("description"),
302+
deprecation_reason=input_value_introspection.get("deprecationReason"),
301303
)
302304

303305
def build_directive(directive_introspection: Dict) -> GraphQLDirective:

src/graphql/utilities/get_introspection_query.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from textwrap import dedent
2+
from typing import Optional
23

34
__all__ = ["get_introspection_query"]
45

@@ -8,6 +9,7 @@ def get_introspection_query(
89
specified_by_url: bool = False,
910
directive_is_repeatable: bool = False,
1011
schema_description: bool = False,
12+
input_value_deprecation: bool = False,
1113
) -> str:
1214
"""Get a query for introspection.
1315
@@ -19,6 +21,10 @@ def get_introspection_query(
1921
maybe_specified_by_url = "specifiedByUrl" if specified_by_url else ""
2022
maybe_directive_is_repeatable = "isRepeatable" if directive_is_repeatable else ""
2123
maybe_schema_description = maybe_description if schema_description else ""
24+
25+
def input_deprecation(string: str) -> Optional[str]:
26+
return string if input_value_deprecation else ""
27+
2228
return dedent(
2329
f"""
2430
query IntrospectionQuery {{
@@ -35,7 +41,7 @@ def get_introspection_query(
3541
{maybe_description}
3642
{maybe_directive_is_repeatable}
3743
locations
38-
args {{
44+
args{input_deprecation("(includeDeprecated: true)")} {{
3945
...InputValue
4046
}}
4147
}}
@@ -50,7 +56,7 @@ def get_introspection_query(
5056
fields(includeDeprecated: true) {{
5157
name
5258
{maybe_description}
53-
args {{
59+
args{input_deprecation("(includeDeprecated: true)")} {{
5460
...InputValue
5561
}}
5662
type {{
@@ -59,7 +65,7 @@ def get_introspection_query(
5965
isDeprecated
6066
deprecationReason
6167
}}
62-
inputFields {{
68+
inputFields{input_deprecation("(includeDeprecated: true)")} {{
6369
...InputValue
6470
}}
6571
interfaces {{
@@ -81,6 +87,8 @@ def get_introspection_query(
8187
{maybe_description}
8288
type {{ ...TypeRef }}
8389
defaultValue
90+
{input_deprecation("isDeprecated")}
91+
{input_deprecation("deprecationReason")}
8492
}}
8593
8694
fragment TypeRef on __Type {{

src/graphql/utilities/introspection_from_schema.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def introspection_from_schema(
1717
specified_by_url: bool = True,
1818
directive_is_repeatable: bool = True,
1919
schema_description: bool = True,
20+
input_value_deprecation: bool = True,
2021
) -> IntrospectionSchema:
2122
"""Build an IntrospectionQuery from a GraphQLSchema
2223
@@ -28,7 +29,11 @@ def introspection_from_schema(
2829
"""
2930
document = parse(
3031
get_introspection_query(
31-
descriptions, specified_by_url, directive_is_repeatable, schema_description
32+
descriptions,
33+
specified_by_url,
34+
directive_is_repeatable,
35+
schema_description,
36+
input_value_deprecation,
3237
)
3338
)
3439

tests/utilities/test_build_client_schema.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,14 @@ def builds_a_schema_without_directives():
494494
def builds_a_schema_aware_of_deprecation():
495495
sdl = dedent(
496496
'''
497+
directive @someDirective(
498+
"""This is a shiny new argument"""
499+
shinyArg: SomeInputObject
500+
501+
"""This was our design mistake :("""
502+
oldArg: String @deprecated(reason: "Use shinyArg")
503+
) on QUERY
504+
497505
enum Color {
498506
"""So rosy"""
499507
RED
@@ -508,30 +516,55 @@ def builds_a_schema_aware_of_deprecation():
508516
MAUVE @deprecated(reason: "No longer in fashion")
509517
}
510518
519+
input SomeInputObject {
520+
"""Nothing special about it, just deprecated for some unknown reason"""
521+
oldField: String @deprecated(reason: "Don't use it, use newField instead!")
522+
523+
"""Same field but with a new name"""
524+
newField: String
525+
}
526+
511527
type Query {
512528
"""This is a shiny string field"""
513529
shinyString: String
514530
515531
"""This is a deprecated string field"""
516532
deprecatedString: String @deprecated(reason: "Use shinyString")
533+
534+
"""Color of a week"""
517535
color: Color
536+
537+
"""Some random field"""
538+
someField(
539+
"""This is a shiny new argument"""
540+
shinyArg: SomeInputObject
541+
542+
"""This was our design mistake :("""
543+
oldArg: String @deprecated(reason: "Use shinyArg")
544+
): String
518545
}
519-
'''
546+
''' # noqa: E501
520547
)
521548

522549
assert cycle_introspection(sdl) == sdl
523550

524551
def builds_a_schema_with_empty_deprecation_reasons():
525552
sdl = dedent(
526553
"""
554+
directive @someDirective(someArg: SomeInputObject @deprecated(reason: "")) on QUERY
555+
527556
type Query {
528-
someField: String @deprecated(reason: "")
557+
someField(someArg: SomeInputObject @deprecated(reason: "")): SomeEnum @deprecated(reason: "")
558+
}
559+
560+
input SomeInputObject {
561+
someInputField: String @deprecated(reason: "")
529562
}
530563
531564
enum SomeEnum {
532565
SOME_VALUE @deprecated(reason: "")
533566
}
534-
"""
567+
""" # noqa: E501
535568
)
536569

537570
assert cycle_introspection(sdl) == sdl

tests/utilities/test_get_introspection_query.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,30 @@ def includes_specified_by_url_field():
4747
ExcpectIntrospectionQuery().to_not_match("specifiedByUrl")
4848
ExcpectIntrospectionQuery(specified_by_url=True).to_match("specifiedByUrl")
4949
ExcpectIntrospectionQuery(specified_by_url=False).to_not_match("specifiedByUrl")
50+
51+
def includes_is_deprecated_field_on_input_values():
52+
ExcpectIntrospectionQuery().to_match("isDeprecated", 2)
53+
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match(
54+
"isDeprecated", 3
55+
)
56+
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match(
57+
"isDeprecated", 2
58+
)
59+
60+
def includes_deprecation_reason_field_on_input_values():
61+
ExcpectIntrospectionQuery().to_match("deprecationReason", 2)
62+
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match(
63+
"deprecationReason", 3
64+
)
65+
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match(
66+
"deprecationReason", 2
67+
)
68+
69+
def includes_deprecated_input_field_and_args():
70+
ExcpectIntrospectionQuery().to_match("includeDeprecated: true", 2)
71+
ExcpectIntrospectionQuery(input_value_deprecation=True).to_match(
72+
"includeDeprecated: true", 5
73+
)
74+
ExcpectIntrospectionQuery(input_value_deprecation=False).to_match(
75+
"includeDeprecated: true", 2
76+
)

tests/utilities/test_introspection_from_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def describe_introspection_from_schema():
2626
},
2727
description="This is a simple type",
2828
),
29-
description='This is a simple schema'
29+
description="This is a simple schema",
3030
)
3131

3232
def converts_a_simple_schema():

0 commit comments

Comments
 (0)