forked from graphql-python/graphql-core
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_introspection_query.py
309 lines (244 loc) · 8.1 KB
/
get_introspection_query.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""Get introspection query"""
from __future__ import annotations
from textwrap import dedent
from typing import TYPE_CHECKING, Any, Dict, Union
if TYPE_CHECKING:
from ..language import DirectiveLocation
try:
from typing import Literal, TypedDict
except ImportError: # Python < 3.8
from typing_extensions import Literal, TypedDict # type: ignore
try:
from typing import TypeAlias
except ImportError: # Python < 3.10
from typing_extensions import TypeAlias
__all__ = [
"IntrospectionDirective",
"IntrospectionEnumType",
"IntrospectionField",
"IntrospectionInputObjectType",
"IntrospectionInputValue",
"IntrospectionInterfaceType",
"IntrospectionListType",
"IntrospectionNonNullType",
"IntrospectionObjectType",
"IntrospectionQuery",
"IntrospectionScalarType",
"IntrospectionSchema",
"IntrospectionType",
"IntrospectionTypeRef",
"IntrospectionUnionType",
"get_introspection_query",
]
def get_introspection_query(
descriptions: bool = True,
specified_by_url: bool = False,
directive_is_repeatable: bool = False,
schema_description: bool = False,
input_value_deprecation: bool = False,
) -> str:
"""Get a query for introspection.
Optionally, you can exclude descriptions, include specification URLs,
include repeatability of directives, and specify whether to include
the schema description as well.
"""
maybe_description = "description" if descriptions else ""
maybe_specified_by_url = "specifiedByURL" if specified_by_url else ""
maybe_directive_is_repeatable = "isRepeatable" if directive_is_repeatable else ""
maybe_schema_description = maybe_description if schema_description else ""
def input_deprecation(string: str) -> str | None:
return string if input_value_deprecation else ""
return dedent(
f"""
query IntrospectionQuery {{
__schema {{
{maybe_schema_description}
queryType {{ name }}
mutationType {{ name }}
subscriptionType {{ name }}
types {{
...FullType
}}
directives {{
name
{maybe_description}
{maybe_directive_is_repeatable}
locations
args{input_deprecation("(includeDeprecated: true)")} {{
...InputValue
}}
}}
}}
}}
fragment FullType on __Type {{
kind
name
{maybe_description}
{maybe_specified_by_url}
fields(includeDeprecated: true) {{
name
{maybe_description}
args{input_deprecation("(includeDeprecated: true)")} {{
...InputValue
}}
type {{
...TypeRef
}}
isDeprecated
deprecationReason
}}
inputFields{input_deprecation("(includeDeprecated: true)")} {{
...InputValue
}}
interfaces {{
...TypeRef
}}
enumValues(includeDeprecated: true) {{
name
{maybe_description}
isDeprecated
deprecationReason
}}
possibleTypes {{
...TypeRef
}}
}}
fragment InputValue on __InputValue {{
name
{maybe_description}
type {{ ...TypeRef }}
defaultValue
{input_deprecation("isDeprecated")}
{input_deprecation("deprecationReason")}
}}
fragment TypeRef on __Type {{
kind
name
ofType {{
kind
name
ofType {{
kind
name
ofType {{
kind
name
ofType {{
kind
name
ofType {{
kind
name
ofType {{
kind
name
ofType {{
kind
name
ofType {{
kind
name
ofType {{
kind
name
}}
}}
}}
}}
}}
}}
}}
}}
}}
}}
"""
)
# Unfortunately, the following type definitions are a bit simplistic
# because of current restrictions in the typing system (mypy):
# - no recursion, see https://github.com/python/mypy/issues/731
# - no generic typed dicts, see https://github.com/python/mypy/issues/3863
# simplified IntrospectionNamedType to avoids cycles
SimpleIntrospectionType: TypeAlias = Dict[str, Any]
class MaybeWithDescription(TypedDict, total=False):
description: str | None
class WithName(MaybeWithDescription):
name: str
class MaybeWithSpecifiedByUrl(TypedDict, total=False):
specifiedByURL: str | None
class WithDeprecated(TypedDict):
isDeprecated: bool
deprecationReason: str | None
class MaybeWithDeprecated(TypedDict, total=False):
isDeprecated: bool
deprecationReason: str | None
class IntrospectionInputValue(WithName, MaybeWithDeprecated):
type: SimpleIntrospectionType # should be IntrospectionInputType
defaultValue: str | None
class IntrospectionField(WithName, WithDeprecated):
args: list[IntrospectionInputValue]
type: SimpleIntrospectionType # should be IntrospectionOutputType
class IntrospectionEnumValue(WithName, WithDeprecated):
pass
class MaybeWithIsRepeatable(TypedDict, total=False):
isRepeatable: bool
class IntrospectionDirective(WithName, MaybeWithIsRepeatable):
locations: list[DirectiveLocation]
args: list[IntrospectionInputValue]
class IntrospectionScalarType(WithName, MaybeWithSpecifiedByUrl):
kind: Literal["scalar"]
class IntrospectionInterfaceType(WithName):
kind: Literal["interface"]
fields: list[IntrospectionField]
interfaces: list[SimpleIntrospectionType] # should be InterfaceType
possibleTypes: list[SimpleIntrospectionType] # should be NamedType
class IntrospectionObjectType(WithName):
kind: Literal["object"]
fields: list[IntrospectionField]
interfaces: list[SimpleIntrospectionType] # should be InterfaceType
class IntrospectionUnionType(WithName):
kind: Literal["union"]
possibleTypes: list[SimpleIntrospectionType] # should be NamedType
class IntrospectionEnumType(WithName):
kind: Literal["enum"]
enumValues: list[IntrospectionEnumValue]
class IntrospectionInputObjectType(WithName):
kind: Literal["input_object"]
inputFields: list[IntrospectionInputValue]
IntrospectionType: TypeAlias = Union[
IntrospectionScalarType,
IntrospectionObjectType,
IntrospectionInterfaceType,
IntrospectionUnionType,
IntrospectionEnumType,
IntrospectionInputObjectType,
]
IntrospectionOutputType: TypeAlias = Union[
IntrospectionScalarType,
IntrospectionObjectType,
IntrospectionInterfaceType,
IntrospectionUnionType,
IntrospectionEnumType,
]
IntrospectionInputType: TypeAlias = Union[
IntrospectionScalarType, IntrospectionEnumType, IntrospectionInputObjectType
]
class IntrospectionListType(TypedDict):
kind: Literal["list"]
ofType: SimpleIntrospectionType # should be IntrospectionType
class IntrospectionNonNullType(TypedDict):
kind: Literal["non_null"]
ofType: SimpleIntrospectionType # should be IntrospectionType
IntrospectionTypeRef: TypeAlias = Union[
IntrospectionType, IntrospectionListType, IntrospectionNonNullType
]
class IntrospectionSchema(MaybeWithDescription):
queryType: IntrospectionObjectType
mutationType: IntrospectionObjectType | None
subscriptionType: IntrospectionObjectType | None
types: list[IntrospectionType]
directives: list[IntrospectionDirective]
# The root typed dictionary for schema introspections.
IntrospectionQuery = TypedDict( # noqa: UP013
"IntrospectionQuery",
{"__schema": IntrospectionSchema},
)