Skip to content

Commit 51a933f

Browse files
committed
Add 'extensions' to all Type System objects
Replicates graphql/graphql-js@493a9b6
1 parent 42154e4 commit 51a933f

9 files changed

+459
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The current version 3.0.0a2 of GraphQL-core is up-to-date
1616
with GraphQL.js version 14.4.2.
1717

1818
All parts of the API are covered by an extensive test suite
19-
of currently 1933 unit tests.
19+
of currently 1965 unit tests.
2020

2121

2222
## Documentation

src/graphql/type/definition.py

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,15 @@ class GraphQLNamedType(GraphQLType):
183183

184184
name: str
185185
description: Optional[str]
186+
extensions: Optional[Dict[str, Any]]
186187
ast_node: Optional[TypeDefinitionNode]
187188
extension_ast_nodes: Optional[FrozenList[TypeExtensionNode]]
188189

189190
def __init__(
190191
self,
191192
name: str,
192193
description: str = None,
194+
extensions: Dict[str, Any] = None,
193195
ast_node: TypeDefinitionNode = None,
194196
extension_ast_nodes: Sequence[TypeExtensionNode] = None,
195197
) -> None:
@@ -199,6 +201,11 @@ def __init__(
199201
raise TypeError("The name must be a string.")
200202
if description is not None and not isinstance(description, str):
201203
raise TypeError("The description must be a string.")
204+
if extensions is not None and (
205+
not isinstance(extensions, dict)
206+
or not all(isinstance(key, str) for key in extensions)
207+
):
208+
raise TypeError(f"{name} extensions must be a dictionary with string keys.")
202209
if ast_node and not isinstance(ast_node, TypeDefinitionNode):
203210
raise TypeError(f"{name} AST node must be a TypeDefinitionNode.")
204211
if extension_ast_nodes:
@@ -215,6 +222,7 @@ def __init__(
215222
extension_ast_nodes = None
216223
self.name = name
217224
self.description = description
225+
self.extensions = extensions
218226
self.ast_node = ast_node
219227
self.extension_ast_nodes = extension_ast_nodes
220228

@@ -228,6 +236,7 @@ def to_kwargs(self) -> Dict[str, Any]:
228236
return dict(
229237
name=self.name,
230238
description=self.description,
239+
extensions=self.extensions,
231240
ast_node=self.ast_node,
232241
extension_ast_nodes=self.extension_ast_nodes or FrozenList(),
233242
)
@@ -306,15 +315,17 @@ def __init__(
306315
self,
307316
name: str,
308317
serialize: GraphQLScalarSerializer = None,
309-
description: str = None,
310318
parse_value: GraphQLScalarValueParser = None,
311319
parse_literal: GraphQLScalarLiteralParser = None,
320+
description: str = None,
321+
extensions: Dict[str, Any] = None,
312322
ast_node: ScalarTypeDefinitionNode = None,
313323
extension_ast_nodes: Sequence[ScalarTypeExtensionNode] = None,
314324
) -> None:
315325
super().__init__(
316326
name=name,
317327
description=description,
328+
extensions=extensions,
318329
ast_node=ast_node,
319330
extension_ast_nodes=extension_ast_nodes,
320331
)
@@ -415,12 +426,13 @@ def assert_scalar_type(type_: Any) -> GraphQLScalarType:
415426
class GraphQLField:
416427
"""Definition of a GraphQL field"""
417428

418-
description: Optional[str]
419429
type: "GraphQLOutputType"
420430
args: GraphQLArgumentMap
421431
resolve: Optional["GraphQLFieldResolver"]
422432
subscribe: Optional["GraphQLFieldResolver"]
433+
description: Optional[str]
423434
deprecation_reason: Optional[str]
435+
extensions: Optional[Dict[str, Any]]
424436
ast_node: Optional[FieldDefinitionNode]
425437

426438
def __init__(
@@ -431,6 +443,7 @@ def __init__(
431443
subscribe: "GraphQLFieldResolver" = None,
432444
description: str = None,
433445
deprecation_reason: str = None,
446+
extensions: Dict[str, Any] = None,
434447
ast_node: FieldDefinitionNode = None,
435448
) -> None:
436449
if not is_output_type(type_):
@@ -462,14 +475,20 @@ def __init__(
462475
raise TypeError("The description must be a string.")
463476
if deprecation_reason is not None and not isinstance(deprecation_reason, str):
464477
raise TypeError("The deprecation reason must be a string.")
478+
if extensions is not None and (
479+
not isinstance(extensions, dict)
480+
or not all(isinstance(key, str) for key in extensions)
481+
):
482+
raise TypeError("Field extensions must be a dictionary with string keys.")
465483
if ast_node and not isinstance(ast_node, FieldDefinitionNode):
466484
raise TypeError("Field AST node must be a FieldDefinitionNode.")
467485
self.type = type_
468486
self.args = args or {}
469487
self.resolve = resolve
470488
self.subscribe = subscribe
471-
self.deprecation_reason = deprecation_reason
472489
self.description = description
490+
self.deprecation_reason = deprecation_reason
491+
self.extensions = extensions
473492
self.ast_node = ast_node
474493

475494
def __repr__(self):
@@ -486,16 +505,18 @@ def __eq__(self, other):
486505
and self.resolve == other.resolve
487506
and self.description == other.description
488507
and self.deprecation_reason == other.deprecation_reason
508+
and self.extensions == other.extensions
489509
)
490510

491511
def to_kwargs(self) -> Dict[str, Any]:
492512
return dict(
493-
description=self.description,
494513
type_=self.type,
495514
args=self.args.copy() if self.args else None,
496515
resolve=self.resolve,
497516
subscribe=self.subscribe,
498517
deprecation_reason=self.deprecation_reason,
518+
description=self.description,
519+
extensions=self.extensions,
499520
ast_node=self.ast_node,
500521
)
501522

@@ -550,10 +571,11 @@ class GraphQLResolveInfo(NamedTuple):
550571
class GraphQLArgument:
551572
"""Definition of a GraphQL argument"""
552573

553-
description: Optional[str]
554574
type: "GraphQLInputType"
555575
default_value: Any
576+
description: Optional[str]
556577
out_name: Optional[str] # for transforming names (extension of GraphQL.js)
578+
extensions: Optional[Dict[str, Any]]
557579
ast_node: Optional[InputValueDefinitionNode]
558580

559581
def __init__(
@@ -562,6 +584,7 @@ def __init__(
562584
default_value: Any = INVALID,
563585
description: str = None,
564586
out_name: str = None,
587+
extensions: Dict[str, Any] = None,
565588
ast_node: InputValueDefinitionNode = None,
566589
) -> None:
567590
if not is_input_type(type_):
@@ -570,12 +593,20 @@ def __init__(
570593
raise TypeError("Argument description must be a string.")
571594
if out_name is not None and not isinstance(out_name, str):
572595
raise TypeError("Argument out name must be a string.")
596+
if extensions is not None and (
597+
not isinstance(extensions, dict)
598+
or not all(isinstance(key, str) for key in extensions)
599+
):
600+
raise TypeError(
601+
"Argument extensions must be a dictionary with string keys."
602+
)
573603
if ast_node and not isinstance(ast_node, InputValueDefinitionNode):
574604
raise TypeError("Argument AST node must be an InputValueDefinitionNode.")
575605
self.type = type_
576606
self.default_value = default_value
577607
self.description = description
578608
self.out_name = out_name
609+
self.extensions = extensions
579610
self.ast_node = ast_node
580611

581612
def __eq__(self, other):
@@ -585,14 +616,16 @@ def __eq__(self, other):
585616
and self.default_value == other.default_value
586617
and self.description == other.description
587618
and self.out_name == other.out_name
619+
and self.extensions == other.extensions
588620
)
589621

590622
def to_kwargs(self) -> Dict[str, Any]:
591623
return dict(
592-
description=self.description,
593624
type_=self.type,
594625
default_value=self.default_value,
626+
description=self.description,
595627
out_name=self.out_name,
628+
extensions=self.extensions,
596629
ast_node=self.ast_node,
597630
)
598631

@@ -645,13 +678,15 @@ def __init__(
645678
fields: Thunk[GraphQLFieldMap],
646679
interfaces: Thunk[Sequence["GraphQLInterfaceType"]] = None,
647680
is_type_of: GraphQLIsTypeOfFn = None,
681+
extensions: Dict[str, Any] = None,
648682
description: str = None,
649683
ast_node: ObjectTypeDefinitionNode = None,
650684
extension_ast_nodes: Sequence[ObjectTypeExtensionNode] = None,
651685
) -> None:
652686
super().__init__(
653687
name=name,
654688
description=description,
689+
extensions=extensions,
655690
ast_node=ast_node,
656691
extension_ast_nodes=extension_ast_nodes,
657692
)
@@ -763,12 +798,14 @@ def __init__(
763798
fields: Thunk[GraphQLFieldMap] = None,
764799
resolve_type: GraphQLTypeResolver = None,
765800
description: str = None,
801+
extensions: Dict[str, Any] = None,
766802
ast_node: InterfaceTypeDefinitionNode = None,
767803
extension_ast_nodes: Sequence[InterfaceTypeExtensionNode] = None,
768804
) -> None:
769805
super().__init__(
770806
name=name,
771807
description=description,
808+
extensions=extensions,
772809
ast_node=ast_node,
773810
extension_ast_nodes=extension_ast_nodes,
774811
)
@@ -788,7 +825,6 @@ def __init__(
788825
)
789826
self._fields = fields
790827
self.resolve_type = resolve_type
791-
self.description = description
792828

793829
def to_kwargs(self) -> Dict[str, Any]:
794830
return dict(
@@ -864,12 +900,14 @@ def __init__(
864900
types: Thunk[Sequence[GraphQLObjectType]],
865901
resolve_type: GraphQLTypeResolver = None,
866902
description: str = None,
903+
extensions: Dict[str, Any] = None,
867904
ast_node: UnionTypeDefinitionNode = None,
868905
extension_ast_nodes: Sequence[UnionTypeExtensionNode] = None,
869906
) -> None:
870907
super().__init__(
871908
name=name,
872909
description=description,
910+
extensions=extensions,
873911
ast_node=ast_node,
874912
extension_ast_nodes=extension_ast_nodes,
875913
)
@@ -967,12 +1005,14 @@ def __init__(
9671005
name: str,
9681006
values: Union[GraphQLEnumValueMap, Dict[str, Any], Type[Enum]],
9691007
description: str = None,
1008+
extensions: Dict[str, Any] = None,
9701009
ast_node: EnumTypeDefinitionNode = None,
9711010
extension_ast_nodes: Sequence[EnumTypeExtensionNode] = None,
9721011
) -> None:
9731012
super().__init__(
9741013
name=name,
9751014
description=description,
1015+
extensions=extensions,
9761016
ast_node=ast_node,
9771017
extension_ast_nodes=extension_ast_nodes,
9781018
)
@@ -1080,13 +1120,15 @@ class GraphQLEnumValue:
10801120
value: Any
10811121
description: Optional[str]
10821122
deprecation_reason: Optional[str]
1123+
extensions: Optional[Dict[str, Any]]
10831124
ast_node: Optional[EnumValueDefinitionNode]
10841125

10851126
def __init__(
10861127
self,
10871128
value: Any = None,
10881129
description: str = None,
10891130
deprecation_reason: str = None,
1131+
extensions: Dict[str, Any] = None,
10901132
ast_node: EnumValueDefinitionNode = None,
10911133
) -> None:
10921134
if description is not None and not isinstance(description, str):
@@ -1095,11 +1137,19 @@ def __init__(
10951137
raise TypeError(
10961138
"The deprecation reason for the enum value must be a string."
10971139
)
1140+
if extensions is not None and (
1141+
not isinstance(extensions, dict)
1142+
or not all(isinstance(key, str) for key in extensions)
1143+
):
1144+
raise TypeError(
1145+
"Enum value extensions must be a dictionary with string keys."
1146+
)
10981147
if ast_node and not isinstance(ast_node, EnumValueDefinitionNode):
10991148
raise TypeError("AST node must be an EnumValueDefinitionNode.")
11001149
self.value = value
11011150
self.description = description
11021151
self.deprecation_reason = deprecation_reason
1152+
self.extensions = extensions
11031153
self.ast_node = ast_node
11041154

11051155
def __eq__(self, other):
@@ -1108,13 +1158,15 @@ def __eq__(self, other):
11081158
and self.value == other.value
11091159
and self.description == other.description
11101160
and self.deprecation_reason == other.deprecation_reason
1161+
and self.extensions == other.extensions
11111162
)
11121163

11131164
def to_kwargs(self) -> Dict[str, Any]:
11141165
return dict(
11151166
value=self.value,
11161167
description=self.description,
11171168
deprecation_reason=self.deprecation_reason,
1169+
extensions=self.extensions,
11181170
ast_node=self.ast_node,
11191171
)
11201172

@@ -1161,12 +1213,14 @@ def __init__(
11611213
fields: Thunk[GraphQLInputFieldMap],
11621214
description: str = None,
11631215
out_type: GraphQLInputFieldOutType = None,
1216+
extensions: Dict[str, Any] = None,
11641217
ast_node: InputObjectTypeDefinitionNode = None,
11651218
extension_ast_nodes: Sequence[InputObjectTypeExtensionNode] = None,
11661219
) -> None:
11671220
super().__init__(
11681221
name=name,
11691222
description=description,
1223+
extensions=extensions,
11701224
ast_node=ast_node,
11711225
extension_ast_nodes=extension_ast_nodes,
11721226
)
@@ -1252,6 +1306,7 @@ class GraphQLInputField:
12521306
default_value: Any
12531307
description: Optional[str]
12541308
out_name: Optional[str] # for transforming names (extension of GraphQL.js)
1309+
extensions: Optional[Dict[str, Any]]
12551310
ast_node: Optional[InputValueDefinitionNode]
12561311

12571312
def __init__(
@@ -1260,6 +1315,7 @@ def __init__(
12601315
default_value: Any = INVALID,
12611316
description: str = None,
12621317
out_name: str = None,
1318+
extensions: Dict[str, Any] = None,
12631319
ast_node: InputValueDefinitionNode = None,
12641320
) -> None:
12651321
if not is_input_type(type_):
@@ -1268,12 +1324,20 @@ def __init__(
12681324
raise TypeError("Input field description must be a string.")
12691325
if out_name is not None and not isinstance(out_name, str):
12701326
raise TypeError("Input field out name must be a string.")
1327+
if extensions is not None and (
1328+
not isinstance(extensions, dict)
1329+
or not all(isinstance(key, str) for key in extensions)
1330+
):
1331+
raise TypeError(
1332+
"Input field extensions must be a dictionary with string keys."
1333+
)
12711334
if ast_node and not isinstance(ast_node, InputValueDefinitionNode):
12721335
raise TypeError("Input field AST node must be an InputValueDefinitionNode.")
12731336
self.type = type_
12741337
self.default_value = default_value
12751338
self.description = description
12761339
self.out_name = out_name
1340+
self.extensions = extensions
12771341
self.ast_node = ast_node
12781342

12791343
def __eq__(self, other):
@@ -1282,15 +1346,17 @@ def __eq__(self, other):
12821346
and self.type == other.type
12831347
and self.default_value == other.default_value
12841348
and self.description == other.description
1349+
and self.extensions == other.extensions
12851350
and self.out_name == other.out_name
12861351
)
12871352

12881353
def to_kwargs(self) -> Dict[str, Any]:
12891354
return dict(
12901355
type_=self.type,
1291-
description=self.description,
12921356
default_value=self.default_value,
1357+
description=self.description,
12931358
out_name=self.out_name,
1359+
extensions=self.extensions,
12941360
ast_node=self.ast_node,
12951361
)
12961362

0 commit comments

Comments
 (0)