Skip to content

Commit 44eef9f

Browse files
committed
Enable passing values configuration to GraphQLEnumType as a thunk
Replicates graphql/graphql-js@9e2e751
1 parent 85f2d4d commit 44eef9f

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

src/graphql/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@
347347
GraphQLArgumentMap,
348348
GraphQLEnumValue,
349349
GraphQLEnumValueMap,
350+
GraphQLEnumValuesDefinition,
350351
GraphQLField,
351352
GraphQLFieldMap,
352353
GraphQLFieldResolver,
@@ -539,6 +540,7 @@
539540
"GraphQLEnumValue",
540541
"GraphQLEnumValueKwargs",
541542
"GraphQLEnumValueMap",
543+
"GraphQLEnumValuesDefinition",
542544
"GraphQLError",
543545
"GraphQLErrorExtensions",
544546
"GraphQLField",

src/graphql/type/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
GraphQLArgumentMap,
9595
GraphQLEnumValue,
9696
GraphQLEnumValueMap,
97+
GraphQLEnumValuesDefinition,
9798
GraphQLField,
9899
GraphQLFieldMap,
99100
GraphQLInputField,
@@ -196,6 +197,7 @@
196197
"GraphQLEnumValue",
197198
"GraphQLEnumValueKwargs",
198199
"GraphQLEnumValueMap",
200+
"GraphQLEnumValuesDefinition",
199201
"GraphQLField",
200202
"GraphQLFieldKwargs",
201203
"GraphQLFieldMap",

src/graphql/type/definition.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
from enum import Enum
56
from typing import (
67
TYPE_CHECKING,
78
Any,
@@ -13,6 +14,7 @@
1314
Mapping,
1415
NamedTuple,
1516
Optional,
17+
Type,
1618
TypeVar,
1719
Union,
1820
cast,
@@ -25,8 +27,6 @@
2527
from typing_extensions import TypedDict
2628

2729
if TYPE_CHECKING:
28-
from enum import Enum
29-
3030
try:
3131
from typing import TypeAlias, TypeGuard
3232
except ImportError: # Python < 3.10
@@ -85,6 +85,7 @@
8585
"GraphQLEnumValue",
8686
"GraphQLEnumValueKwargs",
8787
"GraphQLEnumValueMap",
88+
"GraphQLEnumValuesDefinition",
8889
"GraphQLField",
8990
"GraphQLFieldKwargs",
9091
"GraphQLFieldMap",
@@ -1014,6 +1015,10 @@ def assert_union_type(type_: Any) -> GraphQLUnionType:
10141015

10151016
GraphQLEnumValueMap: TypeAlias = Dict[str, "GraphQLEnumValue"]
10161017

1018+
GraphQLEnumValuesDefinition: TypeAlias = Union[
1019+
GraphQLEnumValueMap, Mapping[str, Any], Type[Enum]
1020+
]
1021+
10171022

10181023
class GraphQLEnumTypeKwargs(GraphQLNamedTypeKwargs, total=False):
10191024
"""Arguments for GraphQL enum types"""
@@ -1063,7 +1068,7 @@ class RGBEnum(enum.Enum):
10631068
def __init__(
10641069
self,
10651070
name: str,
1066-
values: GraphQLEnumValueMap | Mapping[str, Any] | type[Enum],
1071+
values: Thunk[GraphQLEnumValuesDefinition],
10671072
names_as_values: bool | None = False,
10681073
description: str | None = None,
10691074
extensions: dict[str, Any] | None = None,
@@ -1077,14 +1082,16 @@ def __init__(
10771082
ast_node=ast_node,
10781083
extension_ast_nodes=extension_ast_nodes,
10791084
)
1085+
if not isinstance(values, type):
1086+
values = resolve_thunk(values) # type: ignore
10801087
try: # check for enum
10811088
values = cast("Enum", values).__members__ # type: ignore
10821089
except AttributeError:
10831090
if not isinstance(values, Mapping) or not all(
10841091
isinstance(name, str) for name in values
10851092
):
10861093
try:
1087-
values = dict(values) # pyright: ignore
1094+
values = dict(values) # type: ignore
10881095
except (TypeError, ValueError) as error:
10891096
msg = (
10901097
f"{name} values must be an Enum or a mapping"

tests/type/test_enum.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class Complex2:
4242

4343
ColorType2 = GraphQLEnumType("Color", ColorTypeEnumValues)
4444

45+
ThunkValuesEnum = GraphQLEnumType("ThunkValues", lambda: {"A": "a", "B": "b"})
46+
4547
QueryType = GraphQLObjectType(
4648
"Query",
4749
{
@@ -84,6 +86,13 @@ class Complex2:
8486
if args.get("provideBadValue")
8587
else args.get("fromEnum"),
8688
),
89+
"thunkValuesString": GraphQLField(
90+
GraphQLString,
91+
args={
92+
"fromEnum": GraphQLArgument(ThunkValuesEnum),
93+
},
94+
resolve=lambda _source, _info, fromEnum: fromEnum,
95+
),
8796
},
8897
)
8998

@@ -346,5 +355,10 @@ def may_be_internally_represented_with_complex_values():
346355
],
347356
)
348357

358+
def may_have_values_specified_via_a_callable():
359+
result = execute_query("{ thunkValuesString(fromEnum: B) }")
360+
361+
assert result == ({"thunkValuesString": "b"}, None)
362+
349363
def can_be_introspected_without_error():
350364
introspection_from_schema(schema)

0 commit comments

Comments
 (0)