Skip to content

Commit 0df0a32

Browse files
committed
introspection: Add missing __Directive.args(includeDeprecated)
Backport from 3.2. Replicates graphql/graphql-js@eb0fed0
1 parent 3daa16d commit 0df0a32

File tree

4 files changed

+66
-9
lines changed

4 files changed

+66
-9
lines changed

src/graphql/type/introspection.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,61 @@
8787
# Note: The fields onOperation, onFragment and onField are deprecated
8888
"name": GraphQLField(
8989
GraphQLNonNull(GraphQLString),
90-
resolve=lambda directive, _info: directive.name,
90+
resolve=DirectiveResolvers.name,
9191
),
9292
"description": GraphQLField(
93-
GraphQLString, resolve=lambda directive, _info: directive.description
93+
GraphQLString,
94+
resolve=DirectiveResolvers.description,
9495
),
9596
"isRepeatable": GraphQLField(
9697
GraphQLNonNull(GraphQLBoolean),
97-
resolve=lambda directive, _info: directive.is_repeatable,
98+
resolve=DirectiveResolvers.is_repeatable,
9899
),
99100
"locations": GraphQLField(
100101
GraphQLNonNull(GraphQLList(GraphQLNonNull(__DirectiveLocation))),
101-
resolve=lambda directive, _info: directive.locations,
102+
resolve=DirectiveResolvers.locations,
102103
),
103104
"args": GraphQLField(
104105
GraphQLNonNull(GraphQLList(GraphQLNonNull(__InputValue))),
105-
resolve=lambda directive, _info: directive.args.items(),
106+
args={
107+
"includeDeprecated": GraphQLArgument(
108+
GraphQLBoolean, default_value=False
109+
)
110+
},
111+
resolve=DirectiveResolvers.args,
106112
),
107113
},
108114
)
109115

110116

117+
class DirectiveResolvers:
118+
@staticmethod
119+
def name(directive, _info):
120+
return directive.name
121+
122+
@staticmethod
123+
def description(directive, _info):
124+
return directive.description
125+
126+
@staticmethod
127+
def is_repeatable(directive, _info):
128+
return directive.is_repeatable
129+
130+
@staticmethod
131+
def locations(directive, _info):
132+
return directive.locations
133+
134+
# noinspection PyPep8Naming
135+
@staticmethod
136+
def args(directive, _info, includeDeprecated=False):
137+
items = directive.args.items()
138+
return (
139+
list(items)
140+
if includeDeprecated
141+
else [item for item in items if item[1].deprecation_reason is None]
142+
)
143+
144+
111145
__DirectiveLocation: GraphQLEnumType = GraphQLEnumType(
112146
name="__DirectiveLocation",
113147
description="A Directive can be adjacent to many parts of the GraphQL"

tests/type/test_introspection.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,17 @@ def executes_an_introspection_query():
755755
},
756756
{
757757
"name": "args",
758-
"args": [],
758+
"args": [
759+
{
760+
"name": "includeDeprecated",
761+
"type": {
762+
"kind": "SCALAR",
763+
"name": "Boolean",
764+
"ofType": None,
765+
},
766+
"defaultValue": "false",
767+
}
768+
],
759769
"type": {
760770
"kind": "NON_NULL",
761771
"name": None,

tests/utilities/test_get_introspection_query.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@
22

33
from typing import Pattern
44

5-
from graphql.utilities import get_introspection_query
5+
from graphql.language import parse
6+
from graphql.utilities import build_schema, get_introspection_query
7+
from graphql.validation import validate
8+
9+
dummy_schema = build_schema(
10+
"""
11+
type Query {
12+
dummy: String
13+
}
14+
"""
15+
)
616

717

818
class ExcpectIntrospectionQuery:
919
def __init__(self, **options):
10-
self.query = get_introspection_query(**options)
20+
query = get_introspection_query(**options)
21+
validation_errors = validate(dummy_schema, parse(query))
22+
assert validation_errors == []
23+
self.query = query
1124

1225
def to_match(self, name: str, times: int = 1) -> None:
1326
pattern = self.to_reg_exp(name)

tests/utilities/test_print_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def prints_introspection_schema():
769769
description: String
770770
isRepeatable: Boolean!
771771
locations: [__DirectiveLocation!]!
772-
args: [__InputValue!]!
772+
args(includeDeprecated: Boolean = false): [__InputValue!]!
773773
}
774774
775775
"""

0 commit comments

Comments
 (0)