Skip to content

Commit 9e67f37

Browse files
committed
Add named input and output types
Backport from 3.2. Unfortunately, we cannot use these for overloading since they overlap. Replicates graphql/graphql-js@c55ac60
1 parent 810d712 commit 9e67f37

File tree

3 files changed

+56
-34
lines changed

3 files changed

+56
-34
lines changed

src/graphql/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@
142142
GraphQLWrappingType,
143143
GraphQLNullableType,
144144
GraphQLNamedType,
145+
GraphQLNamedInputType,
146+
GraphQLNamedOutputType,
145147
Thunk,
146148
GraphQLArgument,
147149
GraphQLArgumentMap,
@@ -509,6 +511,8 @@
509511
"GraphQLWrappingType",
510512
"GraphQLNullableType",
511513
"GraphQLNamedType",
514+
"GraphQLNamedInputType",
515+
"GraphQLNamedOutputType",
512516
"Thunk",
513517
"GraphQLArgument",
514518
"GraphQLArgumentMap",

src/graphql/type/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
GraphQLWrappingType,
7777
GraphQLNullableType,
7878
GraphQLNamedType,
79+
GraphQLNamedInputType,
80+
GraphQLNamedOutputType,
7981
Thunk,
8082
GraphQLArgument,
8183
GraphQLArgumentMap,
@@ -203,6 +205,8 @@
203205
"GraphQLWrappingType",
204206
"GraphQLNullableType",
205207
"GraphQLNamedType",
208+
"GraphQLNamedInputType",
209+
"GraphQLNamedOutputType",
206210
"Thunk",
207211
"GraphQLArgument",
208212
"GraphQLArgumentMap",

src/graphql/type/definition.py

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@
117117
"GraphQLLeafType",
118118
"GraphQLList",
119119
"GraphQLNamedType",
120+
"GraphQLNamedInputType",
121+
"GraphQLNamedOutputType",
120122
"GraphQLNullableType",
121123
"GraphQLNonNull",
122124
"GraphQLResolveInfo",
@@ -186,9 +188,6 @@ def assert_wrapping_type(type_: Any) -> GraphQLWrappingType:
186188
return cast(GraphQLWrappingType, type_)
187189

188190

189-
# These named types do not include modifiers like List or NonNull.
190-
191-
192191
class GraphQLNamedType(GraphQLType):
193192
"""Base class for all GraphQL named types"""
194193

@@ -256,37 +255,6 @@ def __copy__(self) -> "GraphQLNamedType": # pragma: no cover
256255
return self.__class__(**self.to_kwargs())
257256

258257

259-
def is_named_type(type_: Any) -> bool:
260-
return isinstance(type_, GraphQLNamedType)
261-
262-
263-
def assert_named_type(type_: Any) -> GraphQLNamedType:
264-
if not is_named_type(type_):
265-
raise TypeError(f"Expected {type_} to be a GraphQL named type.")
266-
return cast(GraphQLNamedType, type_)
267-
268-
269-
@overload
270-
def get_named_type(type_: None) -> None:
271-
...
272-
273-
274-
@overload
275-
def get_named_type(type_: GraphQLType) -> GraphQLNamedType:
276-
...
277-
278-
279-
def get_named_type(type_: Optional[GraphQLType]) -> Optional[GraphQLNamedType]:
280-
"""Unwrap possible wrapping type"""
281-
if type_:
282-
unwrapped_type = type_
283-
while is_wrapping_type(unwrapped_type):
284-
unwrapped_type = cast(GraphQLWrappingType, unwrapped_type)
285-
unwrapped_type = unwrapped_type.of_type
286-
return cast(GraphQLNamedType, unwrapped_type)
287-
return None
288-
289-
290258
def resolve_thunk(thunk: Any) -> Any:
291259
"""Resolve the given thunk.
292260
@@ -1672,6 +1640,52 @@ def assert_output_type(type_: Any) -> GraphQLOutputType:
16721640
return cast(GraphQLOutputType, type_)
16731641

16741642

1643+
# These named types do not include modifiers like List or NonNull.
1644+
1645+
GraphQLNamedInputType = Union[
1646+
GraphQLScalarType, GraphQLEnumType, GraphQLInputObjectType
1647+
]
1648+
1649+
GraphQLNamedOutputType = Union[
1650+
GraphQLScalarType,
1651+
GraphQLObjectType,
1652+
GraphQLInterfaceType,
1653+
GraphQLUnionType,
1654+
GraphQLEnumType,
1655+
]
1656+
1657+
1658+
def is_named_type(type_: Any) -> bool:
1659+
return isinstance(type_, GraphQLNamedType)
1660+
1661+
1662+
def assert_named_type(type_: Any) -> GraphQLNamedType:
1663+
if not is_named_type(type_):
1664+
raise TypeError(f"Expected {type_} to be a GraphQL named type.")
1665+
return cast(GraphQLNamedType, type_)
1666+
1667+
1668+
@overload
1669+
def get_named_type(type_: None) -> None:
1670+
...
1671+
1672+
1673+
@overload
1674+
def get_named_type(type_: GraphQLType) -> GraphQLNamedType:
1675+
...
1676+
1677+
1678+
def get_named_type(type_: Optional[GraphQLType]) -> Optional[GraphQLNamedType]:
1679+
"""Unwrap possible wrapping type"""
1680+
if type_:
1681+
unwrapped_type = type_
1682+
while is_wrapping_type(unwrapped_type):
1683+
unwrapped_type = cast(GraphQLWrappingType, unwrapped_type)
1684+
unwrapped_type = unwrapped_type.of_type
1685+
return cast(GraphQLNamedType, unwrapped_type)
1686+
return None
1687+
1688+
16751689
# These types may describe types which may be leaf values.
16761690

16771691
graphql_leaf_types = (GraphQLScalarType, GraphQLEnumType)

0 commit comments

Comments
 (0)