| 1 |
from collections import defaultdict |
| 2 |
from functools import partial |
| 3 |
from typing import ( |
| 4 |
Any, |
| 5 |
Collection, |
| 6 |
DefaultDict, |
| 7 |
Dict, |
| 8 |
List, |
| 9 |
Mapping, |
| 10 |
Optional, |
| 11 |
Tuple, |
| 12 |
Union, |
| 13 |
cast, |
| 14 |
) |
| 15 |
|
| 16 |
from ..language import ( |
| 17 |
DirectiveDefinitionNode, |
| 18 |
DirectiveExtensionNode, |
| 19 |
DirectiveLocation, |
| 20 |
DocumentNode, |
| 21 |
EnumTypeDefinitionNode, |
| 22 |
EnumTypeExtensionNode, |
| 23 |
EnumValueDefinitionNode, |
| 24 |
FieldDefinitionNode, |
| 25 |
InputObjectTypeDefinitionNode, |
| 26 |
InputObjectTypeExtensionNode, |
| 27 |
InputValueDefinitionNode, |
| 28 |
InterfaceTypeDefinitionNode, |
| 29 |
InterfaceTypeExtensionNode, |
| 30 |
ListTypeNode, |
| 31 |
NamedTypeNode, |
| 32 |
NonNullTypeNode, |
| 33 |
ObjectTypeDefinitionNode, |
| 34 |
ObjectTypeExtensionNode, |
| 35 |
OperationType, |
| 36 |
ScalarTypeDefinitionNode, |
| 37 |
ScalarTypeExtensionNode, |
| 38 |
SchemaExtensionNode, |
| 39 |
SchemaDefinitionNode, |
| 40 |
TypeDefinitionNode, |
| 41 |
TypeExtensionNode, |
| 42 |
TypeNode, |
| 43 |
UnionTypeDefinitionNode, |
| 44 |
UnionTypeExtensionNode, |
| 45 |
) |
| 46 |
from ..pyutils import inspect, merge_kwargs |
| 47 |
from ..type import ( |
| 48 |
GraphQLArgument, |
| 49 |
GraphQLArgumentMap, |
| 50 |
GraphQLDeprecatedDirective, |
| 51 |
GraphQLDirective, |
| 52 |
GraphQLEnumType, |
| 53 |
GraphQLEnumValue, |
| 54 |
GraphQLEnumValueMap, |
| 55 |
GraphQLField, |
| 56 |
GraphQLFieldMap, |
| 57 |
GraphQLInputField, |
| 58 |
GraphQLInputObjectType, |
| 59 |
GraphQLInputObjectTypeKwargs, |
| 60 |
GraphQLInputType, |
| 61 |
GraphQLInputFieldMap, |
| 62 |
GraphQLInterfaceType, |
| 63 |
GraphQLInterfaceTypeKwargs, |
| 64 |
GraphQLList, |
| 65 |
GraphQLNamedType, |
| 66 |
GraphQLNonNull, |
| 67 |
GraphQLNullableType, |
| 68 |
GraphQLObjectType, |
| 69 |
GraphQLObjectTypeKwargs, |
| 70 |
GraphQLOutputType, |
| 71 |
GraphQLScalarType, |
| 72 |
GraphQLSchema, |
| 73 |
GraphQLSchemaKwargs, |
| 74 |
GraphQLSpecifiedByDirective, |
| 75 |
GraphQLOneOfDirective, |
| 76 |
GraphQLType, |
| 77 |
GraphQLUnionType, |
| 78 |
GraphQLUnionTypeKwargs, |
| 79 |
assert_schema, |
| 80 |
is_enum_type, |
| 81 |
is_input_object_type, |
| 82 |
is_interface_type, |
| 83 |
is_list_type, |
| 84 |
is_non_null_type, |
| 85 |
is_object_type, |
| 86 |
is_scalar_type, |
| 87 |
is_specified_directive, |
| 88 |
is_union_type, |
| 89 |
is_introspection_type, |
| 90 |
is_specified_scalar_type, |
| 91 |
introspection_types, |
| 92 |
specified_scalar_types, |
| 93 |
) |
| 94 |
from .value_from_ast import value_from_ast |
| 95 |
|
| 96 |
__all__ = [ |
| 97 |
"extend_schema", |
| 98 |
"ExtendSchemaImpl", |
| 99 |
] |
| 100 |
|
| 101 |
|
| 102 |
def extend_schema( |
| 103 |
schema: GraphQLSchema, |
| 104 |
document_ast: DocumentNode, |
| 105 |
assume_valid: bool = False, |
| 106 |
assume_valid_sdl: bool = False, |
| 107 |
) -> GraphQLSchema: |
| 108 |
"""Extend the schema with extensions from a given document. |
| 109 |
|
| 110 |
Produces a new schema given an existing schema and a document which may contain |
| 111 |
GraphQL type extensions and definitions. The original schema will remain unaltered. |
| 112 |
|
| 113 |
Because a schema represents a graph of references, a schema cannot be extended |
| 114 |
without effectively making an entire copy. We do not know until it's too late if |
| 115 |
subgraphs remain unchanged. |
| 116 |
|
| 117 |
This algorithm copies the provided schema, applying extensions while producing the |
| 118 |
copy. The original schema remains unaltered. |
| 119 |
|
| 120 |
When extending a schema with a known valid extension, it might be safe to assume the |
| 121 |
schema is valid. Set ``assume_valid`` to ``True`` to assume the produced schema is |
| 122 |
valid. Set ``assume_valid_sdl`` to ``True`` to assume it is already a valid SDL |
| 123 |
document. |
| 124 |
""" |
| 125 |
assert_schema(schema) |
| 126 |
|
| 127 |
if not isinstance(document_ast, DocumentNode): |
| 128 |
raise TypeError("Must provide valid Document AST.") |
| 129 |
|
| 130 |
if not (assume_valid or assume_valid_sdl): |
| 131 |
from ..validation.validate import assert_valid_sdl_extension |
| 132 |
|
| 133 |
assert_valid_sdl_extension(document_ast, schema) |
| 134 |
|
| 135 |
schema_kwargs = schema.to_kwargs() |
| 136 |
extended_kwargs = ExtendSchemaImpl.extend_schema_args( |
| 137 |
schema_kwargs, document_ast, assume_valid |
| 138 |
) |
| 139 |
return ( |
| 140 |
schema if schema_kwargs is extended_kwargs else GraphQLSchema(**extended_kwargs) |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
class ExtendSchemaImpl: |
| 145 |
"""Helper class implementing the methods to extend a schema. |
| 146 |
|
| 147 |
Note: We use a class instead of an implementation with local functions |
| 148 |
and lambda functions so that the extended schema can be pickled. |
| 149 |
|
| 150 |
For internal use only. |
| 151 |
""" |
| 152 |
|
| 153 |
type_map: Dict[str, GraphQLNamedType] |
| 154 |
type_extensions_map: Dict[str, Any] |
| 155 |
directive_extensions_map: Dict[str, List[DirectiveExtensionNode]] |
| 156 |
|
| 157 |
def __init__( |
| 158 |
self, |
| 159 |
type_extensions_map: Dict[str, Any], |
| 160 |
directive_extensions_map: Dict[str, List[DirectiveExtensionNode]], |
| 161 |
): |
| 162 |
self.type_map = {} |
| 163 |
self.type_extensions_map = type_extensions_map |
| 164 |
self.directive_extensions_map = directive_extensions_map |
| 165 |
|
| 166 |
@classmethod |
| 167 |
def extend_schema_args( |
| 168 |
cls, |
| 169 |
schema_kwargs: GraphQLSchemaKwargs, |
| 170 |
document_ast: DocumentNode, |
| 171 |
assume_valid: bool = False, |
| 172 |
) -> GraphQLSchemaKwargs: |
| 173 |
"""Extend the given schema arguments with extensions from a given document. |
| 174 |
|
| 175 |
For internal use only. |
| 176 |
""" |
| 177 |
# Collect the type definitions and extensions found in the document. |
| 178 |
type_defs: List[TypeDefinitionNode] = [] |
| 179 |
type_extensions_map: DefaultDict[str, Any] = defaultdict(list) |
| 180 |
directive_extensions_map: DefaultDict[str, List[DirectiveExtensionNode]] = ( |
| 181 |
defaultdict(list) |
| 182 |
) |
| 183 |
|
| 184 |
# New directives and types are separate because a directives and types can have |
| 185 |
# the same name. For example, a type named "skip". |
| 186 |
directive_defs: List[DirectiveDefinitionNode] = [] |
| 187 |
|
| 188 |
schema_def: Optional[SchemaDefinitionNode] = None |
| 189 |
# Schema extensions are collected which may add additional operation types. |
| 190 |
schema_extensions: List[SchemaExtensionNode] = [] |
| 191 |
|
| 192 |
for def_ in document_ast.definitions: |
| 193 |
if isinstance(def_, SchemaDefinitionNode): |
| 194 |
schema_def = def_ |
| 195 |
elif isinstance(def_, SchemaExtensionNode): |
| 196 |
schema_extensions.append(def_) |
| 197 |
elif isinstance(def_, TypeDefinitionNode): |
| 198 |
type_defs.append(def_) |
| 199 |
elif isinstance(def_, TypeExtensionNode): |
| 200 |
extended_type_name = def_.name.value |
| 201 |
type_extensions_map[extended_type_name].append(def_) |
| 202 |
elif isinstance(def_, DirectiveDefinitionNode): |
| 203 |
directive_defs.append(def_) |
| 204 |
elif isinstance(def_, DirectiveExtensionNode): |
| 205 |
extended_directive_name = def_.name.value |
| 206 |
directive_extensions_map[extended_directive_name].append(def_) |
| 207 |
|
| 208 |
# If this document contains no new types, extensions, or directives then return |
| 209 |
# the same unmodified GraphQLSchema instance. |
| 210 |
if ( |
| 211 |
not type_extensions_map |
| 212 |
and not directive_extensions_map |
| 213 |
and not type_defs |
| 214 |
and not directive_defs |
| 215 |
and not schema_extensions |
| 216 |
and not schema_def |
| 217 |
): |
| 218 |
return schema_kwargs |
| 219 |
|
| 220 |
self = cls(type_extensions_map, directive_extensions_map) |
| 221 |
for existing_type in schema_kwargs["types"] or (): |
| 222 |
self.type_map[existing_type.name] = self.extend_named_type(existing_type) |
| 223 |
for type_node in type_defs: |
| 224 |
name = type_node.name.value |
| 225 |
self.type_map[name] = std_type_map.get(name) or self.build_type(type_node) |
| 226 |
|
| 227 |
directive_map: Dict[str, GraphQLDirective] = { |
| 228 |
existing_directive.name: self.extend_directive(existing_directive) |
| 229 |
for existing_directive in schema_kwargs["directives"] |
| 230 |
} |
| 231 |
|
| 232 |
# Get the extended root operation types. |
| 233 |
operation_types: Dict[OperationType, GraphQLNamedType] = {} |
| 234 |
for operation_type in OperationType: |
| 235 |
original_type = schema_kwargs[operation_type.value] |
| 236 |
if original_type: |
| 237 |
operation_types[operation_type] = self.replace_named_type(original_type) |
| 238 |
# Then, incorporate schema definition and all schema extensions. |
| 239 |
if schema_def: |
| 240 |
operation_types.update(self.get_operation_types([schema_def])) |
| 241 |
if schema_extensions: |
| 242 |
operation_types.update(self.get_operation_types(schema_extensions)) |
| 243 |
|
| 244 |
# Then produce and return the kwargs for a Schema with these types. |
| 245 |
get_operation = operation_types.get |
| 246 |
description = ( |
| 247 |
schema_def.description.value |
| 248 |
if schema_def and schema_def.description |
| 249 |
else None |
| 250 |
) |
| 251 |
if description is None: |
| 252 |
description = schema_kwargs["description"] |
| 253 |
return GraphQLSchemaKwargs( |
| 254 |
query=get_operation(OperationType.QUERY), # type: ignore |
| 255 |
mutation=get_operation(OperationType.MUTATION), # type: ignore |
| 256 |
subscription=get_operation(OperationType.SUBSCRIPTION), # type: ignore |
| 257 |
types=tuple(self.type_map.values()), |
| 258 |
directives=tuple( |
| 259 |
self.replace_directive(directive) |
| 260 |
for directive in directive_map.values() |
| 261 |
) |
| 262 |
+ tuple(self.build_directive(directive) for directive in directive_defs), |
| 263 |
description=description, |
| 264 |
extensions=schema_kwargs["extensions"], |
| 265 |
ast_node=schema_def or schema_kwargs["ast_node"], |
| 266 |
extension_ast_nodes=schema_kwargs["extension_ast_nodes"] |
| 267 |
+ tuple(schema_extensions), |
| 268 |
assume_valid=assume_valid, |
| 269 |
) |
| 270 |
|
| 271 |
# noinspection PyTypeChecker,PyUnresolvedReferences |
| 272 |
def replace_type(self, type_: GraphQLType) -> GraphQLType: |
| 273 |
if is_list_type(type_): |
| 274 |
return GraphQLList(self.replace_type(type_.of_type)) # type: ignore |
| 275 |
if is_non_null_type(type_): |
| 276 |
return GraphQLNonNull(self.replace_type(type_.of_type)) # type: ignore |
| 277 |
return self.replace_named_type(type_) # type: ignore |
| 278 |
|
| 279 |
def replace_named_type(self, type_: GraphQLNamedType) -> GraphQLNamedType: |
| 280 |
# Note: While this could make early assertions to get the correctly |
| 281 |
# typed values below, that would throw immediately while type system |
| 282 |
# validation with validate_schema() will produce more actionable results. |
| 283 |
return self.type_map[type_.name] |
| 284 |
|
| 285 |
# noinspection PyShadowingNames |
| 286 |
def replace_directive(self, directive: GraphQLDirective) -> GraphQLDirective: |
| 287 |
if is_specified_directive(directive): |
| 288 |
# Builtin directives are not extended. |
| 289 |
return directive |
| 290 |
kwargs = directive.to_kwargs() |
| 291 |
return GraphQLDirective( |
| 292 |
**merge_kwargs( |
| 293 |
kwargs, |
| 294 |
args={ |
| 295 |
name: self.extend_arg(arg) for name, arg in kwargs["args"].items() |
| 296 |
}, |
| 297 |
) |
| 298 |
) |
| 299 |
|
| 300 |
def extend_directive(self, directive: GraphQLDirective) -> GraphQLDirective: |
| 301 |
kwargs = directive.to_kwargs() |
| 302 |
extensions = tuple(self.directive_extensions_map[kwargs["name"]]) |
| 303 |
deprecation_reason = kwargs["deprecation_reason"] |
| 304 |
if deprecation_reason is None: |
| 305 |
deprecation_reason = next( |
| 306 |
( |
| 307 |
reason |
| 308 |
for reason in (get_deprecation_reason(ext) for ext in extensions) |
| 309 |
if reason is not None |
| 310 |
), |
| 311 |
None, |
| 312 |
) |
| 313 |
return GraphQLDirective( |
| 314 |
**merge_kwargs( |
| 315 |
kwargs, |
| 316 |
deprecation_reason=deprecation_reason, |
| 317 |
extension_ast_nodes=kwargs["extension_ast_nodes"] + extensions, |
| 318 |
) |
| 319 |
) |
| 320 |
|
| 321 |
def extend_named_type(self, type_: GraphQLNamedType) -> GraphQLNamedType: |
| 322 |
if is_introspection_type(type_) or is_specified_scalar_type(type_): |
| 323 |
# Builtin types are not extended. |
| 324 |
return type_ |
| 325 |
if is_scalar_type(type_): |
| 326 |
type_ = cast(GraphQLScalarType, type_) |
| 327 |
return self.extend_scalar_type(type_) |
| 328 |
if is_object_type(type_): |
| 329 |
type_ = cast(GraphQLObjectType, type_) |
| 330 |
return self.extend_object_type(type_) |
| 331 |
if is_interface_type(type_): |
| 332 |
type_ = cast(GraphQLInterfaceType, type_) |
| 333 |
return self.extend_interface_type(type_) |
| 334 |
if is_union_type(type_): |
| 335 |
type_ = cast(GraphQLUnionType, type_) |
| 336 |
return self.extend_union_type(type_) |
| 337 |
if is_enum_type(type_): |
| 338 |
type_ = cast(GraphQLEnumType, type_) |
| 339 |
return self.extend_enum_type(type_) |
| 340 |
if is_input_object_type(type_): |
| 341 |
type_ = cast(GraphQLInputObjectType, type_) |
| 342 |
return self.extend_input_object_type(type_) |
| 343 |
|
| 344 |
# Not reachable. All possible types have been considered. |
| 345 |
raise TypeError(f"Unexpected type: {inspect(type_)}.") # pragma: no cover |
| 346 |
|
| 347 |
def extend_input_object_type_fields( |
| 348 |
self, kwargs: GraphQLInputObjectTypeKwargs, extensions: Tuple[Any, ...] |
| 349 |
) -> GraphQLInputFieldMap: |
| 350 |
return { |
| 351 |
**{ |
| 352 |
name: GraphQLInputField( |
| 353 |
**merge_kwargs( |
| 354 |
field.to_kwargs(), |
| 355 |
type_=self.replace_type(field.type), |
| 356 |
) |
| 357 |
) |
| 358 |
for name, field in kwargs["fields"].items() |
| 359 |
}, |
| 360 |
**self.build_input_field_map(extensions), |
| 361 |
} |
| 362 |
|
| 363 |
# noinspection PyShadowingNames |
| 364 |
def extend_input_object_type( |
| 365 |
self, |
| 366 |
type_: GraphQLInputObjectType, |
| 367 |
) -> GraphQLInputObjectType: |
| 368 |
kwargs = type_.to_kwargs() |
| 369 |
extensions = tuple(self.type_extensions_map[kwargs["name"]]) |
| 370 |
|
| 371 |
return GraphQLInputObjectType( |
| 372 |
**merge_kwargs( |
| 373 |
kwargs, |
| 374 |
fields=partial( |
| 375 |
self.extend_input_object_type_fields, kwargs, extensions |
| 376 |
), |
| 377 |
extension_ast_nodes=kwargs["extension_ast_nodes"] + extensions, |
| 378 |
) |
| 379 |
) |
| 380 |
|
| 381 |
def extend_enum_type(self, type_: GraphQLEnumType) -> GraphQLEnumType: |
| 382 |
kwargs = type_.to_kwargs() |
| 383 |
extensions = tuple(self.type_extensions_map[kwargs["name"]]) |
| 384 |
|
| 385 |
return GraphQLEnumType( |
| 386 |
**merge_kwargs( |
| 387 |
kwargs, |
| 388 |
values={**kwargs["values"], **self.build_enum_value_map(extensions)}, |
| 389 |
extension_ast_nodes=kwargs["extension_ast_nodes"] + extensions, |
| 390 |
) |
| 391 |
) |
| 392 |
|
| 393 |
def extend_scalar_type(self, type_: GraphQLScalarType) -> GraphQLScalarType: |
| 394 |
kwargs = type_.to_kwargs() |
| 395 |
extensions = tuple(self.type_extensions_map[kwargs["name"]]) |
| 396 |
|
| 397 |
specified_by_url = kwargs["specified_by_url"] |
| 398 |
for extension_node in extensions: |
| 399 |
specified_by_url = get_specified_by_url(extension_node) or specified_by_url |
| 400 |
|
| 401 |
return GraphQLScalarType( |
| 402 |
**merge_kwargs( |
| 403 |
kwargs, |
| 404 |
specified_by_url=specified_by_url, |
| 405 |
extension_ast_nodes=kwargs["extension_ast_nodes"] + extensions, |
| 406 |
) |
| 407 |
) |
| 408 |
|
| 409 |
def extend_object_type_interfaces( |
| 410 |
self, kwargs: GraphQLObjectTypeKwargs, extensions: Tuple[Any, ...] |
| 411 |
) -> List[GraphQLInterfaceType]: |
| 412 |
return [ |
| 413 |
cast(GraphQLInterfaceType, self.replace_named_type(interface)) |
| 414 |
for interface in kwargs["interfaces"] |
| 415 |
] + self.build_interfaces(extensions) |
| 416 |
|
| 417 |
def extend_object_type_fields( |
| 418 |
self, kwargs: GraphQLObjectTypeKwargs, extensions: Tuple[Any, ...] |
| 419 |
) -> GraphQLFieldMap: |
| 420 |
return { |
| 421 |
**{ |
| 422 |
name: self.extend_field(field) |
| 423 |
for name, field in kwargs["fields"].items() |
| 424 |
}, |
| 425 |
**self.build_field_map(extensions), |
| 426 |
} |
| 427 |
|
| 428 |
# noinspection PyShadowingNames |
| 429 |
def extend_object_type(self, type_: GraphQLObjectType) -> GraphQLObjectType: |
| 430 |
kwargs = type_.to_kwargs() |
| 431 |
extensions = tuple(self.type_extensions_map[kwargs["name"]]) |
| 432 |
|
| 433 |
return GraphQLObjectType( |
| 434 |
**merge_kwargs( |
| 435 |
kwargs, |
| 436 |
interfaces=partial( |
| 437 |
self.extend_object_type_interfaces, kwargs, extensions |
| 438 |
), |
| 439 |
fields=partial(self.extend_object_type_fields, kwargs, extensions), |
| 440 |
extension_ast_nodes=kwargs["extension_ast_nodes"] + extensions, |
| 441 |
) |
| 442 |
) |
| 443 |
|
| 444 |
def extend_interface_type_interfaces( |
| 445 |
self, kwargs: GraphQLInterfaceTypeKwargs, extensions: Tuple[Any, ...] |
| 446 |
) -> List[GraphQLInterfaceType]: |
| 447 |
return [ |
| 448 |
cast(GraphQLInterfaceType, self.replace_named_type(interface)) |
| 449 |
for interface in kwargs["interfaces"] |
| 450 |
] + self.build_interfaces(extensions) |
| 451 |
|
| 452 |
def extend_interface_type_fields( |
| 453 |
self, kwargs: GraphQLInterfaceTypeKwargs, extensions: Tuple[Any, ...] |
| 454 |
) -> GraphQLFieldMap: |
| 455 |
return { |
| 456 |
**{ |
| 457 |
name: self.extend_field(field) |
| 458 |
for name, field in kwargs["fields"].items() |
| 459 |
}, |
| 460 |
**self.build_field_map(extensions), |
| 461 |
} |
| 462 |
|
| 463 |
# noinspection PyShadowingNames |
| 464 |
def extend_interface_type( |
| 465 |
self, type_: GraphQLInterfaceType |
| 466 |
) -> GraphQLInterfaceType: |
| 467 |
kwargs = type_.to_kwargs() |
| 468 |
extensions = tuple(self.type_extensions_map[kwargs["name"]]) |
| 469 |
|
| 470 |
return GraphQLInterfaceType( |
| 471 |
**merge_kwargs( |
| 472 |
kwargs, |
| 473 |
interfaces=partial( |
| 474 |
self.extend_interface_type_interfaces, kwargs, extensions |
| 475 |
), |
| 476 |
fields=partial(self.extend_interface_type_fields, kwargs, extensions), |
| 477 |
extension_ast_nodes=kwargs["extension_ast_nodes"] + extensions, |
| 478 |
) |
| 479 |
) |
| 480 |
|
| 481 |
def extend_union_type_types( |
| 482 |
self, kwargs: GraphQLUnionTypeKwargs, extensions: Tuple[Any, ...] |
| 483 |
) -> List[GraphQLObjectType]: |
| 484 |
return [ |
| 485 |
cast(GraphQLObjectType, self.replace_named_type(member_type)) |
| 486 |
for member_type in kwargs["types"] |
| 487 |
] + self.build_union_types(extensions) |
| 488 |
|
| 489 |
def extend_union_type(self, type_: GraphQLUnionType) -> GraphQLUnionType: |
| 490 |
kwargs = type_.to_kwargs() |
| 491 |
extensions = tuple(self.type_extensions_map[kwargs["name"]]) |
| 492 |
|
| 493 |
return GraphQLUnionType( |
| 494 |
**merge_kwargs( |
| 495 |
kwargs, |
| 496 |
types=partial(self.extend_union_type_types, kwargs, extensions), |
| 497 |
extension_ast_nodes=kwargs["extension_ast_nodes"] + extensions, |
| 498 |
), |
| 499 |
) |
| 500 |
|
| 501 |
# noinspection PyShadowingNames |
| 502 |
def extend_field(self, field: GraphQLField) -> GraphQLField: |
| 503 |
return GraphQLField( |
| 504 |
**merge_kwargs( |
| 505 |
field.to_kwargs(), |
| 506 |
type_=self.replace_type(field.type), |
| 507 |
args={name: self.extend_arg(arg) for name, arg in field.args.items()}, |
| 508 |
) |
| 509 |
) |
| 510 |
|
| 511 |
def extend_arg(self, arg: GraphQLArgument) -> GraphQLArgument: |
| 512 |
return GraphQLArgument( |
| 513 |
**merge_kwargs( |
| 514 |
arg.to_kwargs(), |
| 515 |
type_=self.replace_type(arg.type), |
| 516 |
) |
| 517 |
) |
| 518 |
|
| 519 |
# noinspection PyShadowingNames |
| 520 |
def get_operation_types( |
| 521 |
self, nodes: Collection[Union[SchemaDefinitionNode, SchemaExtensionNode]] |
| 522 |
) -> Dict[OperationType, GraphQLNamedType]: |
| 523 |
# Note: While this could make early assertions to get the correctly |
| 524 |
# typed values below, that would throw immediately while type system |
| 525 |
# validation with validate_schema() will produce more actionable results. |
| 526 |
return { |
| 527 |
operation_type.operation: self.get_named_type(operation_type.type) |
| 528 |
for node in nodes |
| 529 |
for operation_type in node.operation_types or [] |
| 530 |
} |
| 531 |
|
| 532 |
# noinspection PyShadowingNames |
| 533 |
def get_named_type(self, node: NamedTypeNode) -> GraphQLNamedType: |
| 534 |
name = node.name.value |
| 535 |
type_ = std_type_map.get(name) or self.type_map.get(name) |
| 536 |
|
| 537 |
if not type_: |
| 538 |
raise TypeError(f"Unknown type: '{name}'.") |
| 539 |
return type_ |
| 540 |
|
| 541 |
def get_wrapped_type(self, node: TypeNode) -> GraphQLType: |
| 542 |
if isinstance(node, ListTypeNode): |
| 543 |
return GraphQLList(self.get_wrapped_type(node.type)) |
| 544 |
if isinstance(node, NonNullTypeNode): |
| 545 |
return GraphQLNonNull( |
| 546 |
cast(GraphQLNullableType, self.get_wrapped_type(node.type)) |
| 547 |
) |
| 548 |
return self.get_named_type(cast(NamedTypeNode, node)) |
| 549 |
|
| 550 |
def build_directive(self, node: DirectiveDefinitionNode) -> GraphQLDirective: |
| 551 |
locations = [DirectiveLocation[node.value] for node in node.locations] |
| 552 |
extensions = tuple(self.directive_extensions_map[node.name.value]) |
| 553 |
deprecation_reason = get_deprecation_reason(node) |
| 554 |
if deprecation_reason is None: |
| 555 |
deprecation_reason = next( |
| 556 |
( |
| 557 |
reason |
| 558 |
for reason in (get_deprecation_reason(ext) for ext in extensions) |
| 559 |
if reason is not None |
| 560 |
), |
| 561 |
None, |
| 562 |
) |
| 563 |
|
| 564 |
return GraphQLDirective( |
| 565 |
name=node.name.value, |
| 566 |
description=node.description.value if node.description else None, |
| 567 |
locations=locations, |
| 568 |
is_repeatable=node.repeatable, |
| 569 |
args=self.build_argument_map(node.arguments), |
| 570 |
deprecation_reason=deprecation_reason, |
| 571 |
ast_node=node, |
| 572 |
extension_ast_nodes=extensions, |
| 573 |
) |
| 574 |
|
| 575 |
def build_field_map( |
| 576 |
self, |
| 577 |
nodes: Collection[ |
| 578 |
Union[ |
| 579 |
InterfaceTypeDefinitionNode, |
| 580 |
InterfaceTypeExtensionNode, |
| 581 |
ObjectTypeDefinitionNode, |
| 582 |
ObjectTypeExtensionNode, |
| 583 |
] |
| 584 |
], |
| 585 |
) -> GraphQLFieldMap: |
| 586 |
field_map: GraphQLFieldMap = {} |
| 587 |
for node in nodes: |
| 588 |
for field in node.fields or []: |
| 589 |
# Note: While this could make assertions to get the correctly typed |
| 590 |
# value, that would throw immediately while type system validation |
| 591 |
# with validate_schema() will produce more actionable results. |
| 592 |
field_map[field.name.value] = GraphQLField( |
| 593 |
type_=cast(GraphQLOutputType, self.get_wrapped_type(field.type)), |
| 594 |
description=field.description.value if field.description else None, |
| 595 |
args=self.build_argument_map(field.arguments), |
| 596 |
deprecation_reason=get_deprecation_reason(field), |
| 597 |
ast_node=field, |
| 598 |
) |
| 599 |
return field_map |
| 600 |
|
| 601 |
def build_argument_map( |
| 602 |
self, |
| 603 |
args: Optional[Collection[InputValueDefinitionNode]], |
| 604 |
) -> GraphQLArgumentMap: |
| 605 |
arg_map: GraphQLArgumentMap = {} |
| 606 |
for arg in args or []: |
| 607 |
# Note: While this could make assertions to get the correctly typed |
| 608 |
# value, that would throw immediately while type system validation |
| 609 |
# with validate_schema() will produce more actionable results. |
| 610 |
type_ = cast(GraphQLInputType, self.get_wrapped_type(arg.type)) |
| 611 |
arg_map[arg.name.value] = GraphQLArgument( |
| 612 |
type_=type_, |
| 613 |
description=arg.description.value if arg.description else None, |
| 614 |
default_value=value_from_ast(arg.default_value, type_), |
| 615 |
deprecation_reason=get_deprecation_reason(arg), |
| 616 |
ast_node=arg, |
| 617 |
) |
| 618 |
return arg_map |
| 619 |
|
| 620 |
def build_input_field_map( |
| 621 |
self, |
| 622 |
nodes: Collection[ |
| 623 |
Union[InputObjectTypeDefinitionNode, InputObjectTypeExtensionNode] |
| 624 |
], |
| 625 |
) -> GraphQLInputFieldMap: |
| 626 |
input_field_map: GraphQLInputFieldMap = {} |
| 627 |
for node in nodes: |
| 628 |
for field in node.fields or []: |
| 629 |
# Note: While this could make assertions to get the correctly typed |
| 630 |
# value, that would throw immediately while type system validation |
| 631 |
# with validate_schema() will produce more actionable results. |
| 632 |
type_ = cast(GraphQLInputType, self.get_wrapped_type(field.type)) |
| 633 |
input_field_map[field.name.value] = GraphQLInputField( |
| 634 |
type_=type_, |
| 635 |
description=field.description.value if field.description else None, |
| 636 |
default_value=value_from_ast(field.default_value, type_), |
| 637 |
deprecation_reason=get_deprecation_reason(field), |
| 638 |
ast_node=field, |
| 639 |
) |
| 640 |
return input_field_map |
| 641 |
|
| 642 |
@staticmethod |
| 643 |
def build_enum_value_map( |
| 644 |
nodes: Collection[Union[EnumTypeDefinitionNode, EnumTypeExtensionNode]], |
| 645 |
) -> GraphQLEnumValueMap: |
| 646 |
enum_value_map: GraphQLEnumValueMap = {} |
| 647 |
for node in nodes: |
| 648 |
for value in node.values or []: |
| 649 |
# Note: While this could make assertions to get the correctly typed |
| 650 |
# value, that would throw immediately while type system validation |
| 651 |
# with validate_schema() will produce more actionable results. |
| 652 |
value_name = value.name.value |
| 653 |
enum_value_map[value_name] = GraphQLEnumValue( |
| 654 |
value=value_name, |
| 655 |
description=value.description.value if value.description else None, |
| 656 |
deprecation_reason=get_deprecation_reason(value), |
| 657 |
ast_node=value, |
| 658 |
) |
| 659 |
return enum_value_map |
| 660 |
|
| 661 |
def build_interfaces( |
| 662 |
self, |
| 663 |
nodes: Collection[ |
| 664 |
Union[ |
| 665 |
InterfaceTypeDefinitionNode, |
| 666 |
InterfaceTypeExtensionNode, |
| 667 |
ObjectTypeDefinitionNode, |
| 668 |
ObjectTypeExtensionNode, |
| 669 |
] |
| 670 |
], |
| 671 |
) -> List[GraphQLInterfaceType]: |
| 672 |
# Note: While this could make assertions to get the correctly typed |
| 673 |
# value, that would throw immediately while type system validation |
| 674 |
# with validate_schema() will produce more actionable results. |
| 675 |
return [ |
| 676 |
cast(GraphQLInterfaceType, self.get_named_type(type_)) |
| 677 |
for node in nodes |
| 678 |
for type_ in node.interfaces or [] |
| 679 |
] |
| 680 |
|
| 681 |
def build_union_types( |
| 682 |
self, |
| 683 |
nodes: Collection[Union[UnionTypeDefinitionNode, UnionTypeExtensionNode]], |
| 684 |
) -> List[GraphQLObjectType]: |
| 685 |
# Note: While this could make assertions to get the correctly typed |
| 686 |
# value, that would throw immediately while type system validation |
| 687 |
# with validate_schema() will produce more actionable results. |
| 688 |
return [ |
| 689 |
cast(GraphQLObjectType, self.get_named_type(type_)) |
| 690 |
for node in nodes |
| 691 |
for type_ in node.types or [] |
| 692 |
] |
| 693 |
|
| 694 |
def build_object_type( |
| 695 |
self, ast_node: ObjectTypeDefinitionNode |
| 696 |
) -> GraphQLObjectType: |
| 697 |
extension_nodes = self.type_extensions_map[ast_node.name.value] |
| 698 |
all_nodes: List[Union[ObjectTypeDefinitionNode, ObjectTypeExtensionNode]] = [ |
| 699 |
ast_node, |
| 700 |
*extension_nodes, |
| 701 |
] |
| 702 |
return GraphQLObjectType( |
| 703 |
name=ast_node.name.value, |
| 704 |
description=ast_node.description.value if ast_node.description else None, |
| 705 |
interfaces=partial(self.build_interfaces, all_nodes), |
| 706 |
fields=partial(self.build_field_map, all_nodes), |
| 707 |
ast_node=ast_node, |
| 708 |
extension_ast_nodes=extension_nodes, |
| 709 |
) |
| 710 |
|
| 711 |
def build_interface_type( |
| 712 |
self, |
| 713 |
ast_node: InterfaceTypeDefinitionNode, |
| 714 |
) -> GraphQLInterfaceType: |
| 715 |
extension_nodes = self.type_extensions_map[ast_node.name.value] |
| 716 |
all_nodes: List[ |
| 717 |
Union[InterfaceTypeDefinitionNode, InterfaceTypeExtensionNode] |
| 718 |
] = [ast_node, *extension_nodes] |
| 719 |
return GraphQLInterfaceType( |
| 720 |
name=ast_node.name.value, |
| 721 |
description=ast_node.description.value if ast_node.description else None, |
| 722 |
interfaces=partial(self.build_interfaces, all_nodes), |
| 723 |
fields=partial(self.build_field_map, all_nodes), |
| 724 |
ast_node=ast_node, |
| 725 |
extension_ast_nodes=extension_nodes, |
| 726 |
) |
| 727 |
|
| 728 |
def build_enum_type(self, ast_node: EnumTypeDefinitionNode) -> GraphQLEnumType: |
| 729 |
extension_nodes = self.type_extensions_map[ast_node.name.value] |
| 730 |
all_nodes: List[Union[EnumTypeDefinitionNode, EnumTypeExtensionNode]] = [ |
| 731 |
ast_node, |
| 732 |
*extension_nodes, |
| 733 |
] |
| 734 |
return GraphQLEnumType( |
| 735 |
name=ast_node.name.value, |
| 736 |
description=ast_node.description.value if ast_node.description else None, |
| 737 |
values=self.build_enum_value_map(all_nodes), |
| 738 |
ast_node=ast_node, |
| 739 |
extension_ast_nodes=extension_nodes, |
| 740 |
) |
| 741 |
|
| 742 |
def build_union_type(self, ast_node: UnionTypeDefinitionNode) -> GraphQLUnionType: |
| 743 |
extension_nodes = self.type_extensions_map[ast_node.name.value] |
| 744 |
all_nodes: List[Union[UnionTypeDefinitionNode, UnionTypeExtensionNode]] = [ |
| 745 |
ast_node, |
| 746 |
*extension_nodes, |
| 747 |
] |
| 748 |
return GraphQLUnionType( |
| 749 |
name=ast_node.name.value, |
| 750 |
description=ast_node.description.value if ast_node.description else None, |
| 751 |
types=partial(self.build_union_types, all_nodes), |
| 752 |
ast_node=ast_node, |
| 753 |
extension_ast_nodes=extension_nodes, |
| 754 |
) |
| 755 |
|
| 756 |
def build_scalar_type( |
| 757 |
self, ast_node: ScalarTypeDefinitionNode |
| 758 |
) -> GraphQLScalarType: |
| 759 |
extension_nodes = self.type_extensions_map[ast_node.name.value] |
| 760 |
return GraphQLScalarType( |
| 761 |
name=ast_node.name.value, |
| 762 |
description=ast_node.description.value if ast_node.description else None, |
| 763 |
specified_by_url=get_specified_by_url(ast_node), |
| 764 |
ast_node=ast_node, |
| 765 |
extension_ast_nodes=extension_nodes, |
| 766 |
) |
| 767 |
|
| 768 |
def build_input_object_type( |
| 769 |
self, |
| 770 |
ast_node: InputObjectTypeDefinitionNode, |
| 771 |
) -> GraphQLInputObjectType: |
| 772 |
extension_nodes = self.type_extensions_map[ast_node.name.value] |
| 773 |
all_nodes: List[ |
| 774 |
Union[InputObjectTypeDefinitionNode, InputObjectTypeExtensionNode] |
| 775 |
] = [ast_node, *extension_nodes] |
| 776 |
return GraphQLInputObjectType( |
| 777 |
name=ast_node.name.value, |
| 778 |
description=ast_node.description.value if ast_node.description else None, |
| 779 |
fields=partial(self.build_input_field_map, all_nodes), |
| 780 |
ast_node=ast_node, |
| 781 |
extension_ast_nodes=extension_nodes, |
| 782 |
is_one_of=is_one_of(ast_node), |
| 783 |
) |
| 784 |
|
| 785 |
def build_type(self, ast_node: TypeDefinitionNode) -> GraphQLNamedType: |
| 786 |
kind = ast_node.kind |
| 787 |
try: |
| 788 |
kind = kind.removesuffix("_definition") |
| 789 |
except AttributeError: # pragma: no cover (Python < 3.9) |
| 790 |
if kind.endswith("_definition"): |
| 791 |
kind = kind[:-11] |
| 792 |
try: |
| 793 |
build = getattr(self, f"build_{kind}") |
| 794 |
except AttributeError: # pragma: no cover |
| 795 |
# Not reachable. All possible type definition nodes have been considered. |
| 796 |
raise TypeError( # pragma: no cover |
| 797 |
f"Unexpected type definition node: {inspect(ast_node)}." |
| 798 |
) |
| 799 |
return build(ast_node) |
| 800 |
|
| 801 |
|
| 802 |
std_type_map: Mapping[str, Union[GraphQLNamedType, GraphQLObjectType]] = { |
| 803 |
**specified_scalar_types, |
| 804 |
**introspection_types, |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
def get_deprecation_reason( |
| 809 |
node: Union[ |
| 810 |
EnumValueDefinitionNode, |
| 811 |
FieldDefinitionNode, |
| 812 |
InputValueDefinitionNode, |
| 813 |
DirectiveDefinitionNode, |
| 814 |
DirectiveExtensionNode, |
| 815 |
], |
| 816 |
) -> Optional[str]: |
| 817 |
"""Given a field or enum value node, get deprecation reason as string.""" |
| 818 |
from ..execution import get_directive_values |
| 819 |
|
| 820 |
deprecated = get_directive_values(GraphQLDeprecatedDirective, node) |
| 821 |
return deprecated["reason"] if deprecated else None |
| 822 |
|
| 823 |
|
| 824 |
def get_specified_by_url( |
| 825 |
node: Union[ScalarTypeDefinitionNode, ScalarTypeExtensionNode], |
| 826 |
) -> Optional[str]: |
| 827 |
"""Given a scalar node, return the string value for the specifiedByURL.""" |
| 828 |
from ..execution import get_directive_values |
| 829 |
|
| 830 |
specified_by_url = get_directive_values(GraphQLSpecifiedByDirective, node) |
| 831 |
return specified_by_url["url"] if specified_by_url else None |
| 832 |
|
| 833 |
|
| 834 |
def is_one_of(node: InputObjectTypeDefinitionNode) -> bool: |
| 835 |
"""Given an input object node, returns if the node should be OneOf.""" |
| 836 |
from ..execution import get_directive_values |
| 837 |
|
| 838 |
return get_directive_values(GraphQLOneOfDirective, node) is not None |