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