Skip to content

Commit 79ba938

Browse files
committed
Add support for passing name to enum_value
1 parent ca04c88 commit 79ba938

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

strawberry/types/enum.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def is_graphql_generic(self) -> bool:
5050
@dataclasses.dataclass
5151
class EnumValueDefinition:
5252
value: Any
53+
graphql_name: Optional[str] = None
5354
deprecation_reason: Optional[str] = None
5455
directives: Iterable[object] = ()
5556
description: Optional[str] = None
@@ -60,6 +61,7 @@ def __int__(self) -> int:
6061

6162
def enum_value(
6263
value: Any,
64+
name: Optional[str] = None,
6365
deprecation_reason: Optional[str] = None,
6466
directives: Iterable[object] = (),
6567
description: Optional[str] = None,
@@ -68,6 +70,7 @@ def enum_value(
6870
6971
Args:
7072
value: The value of the enum member.
73+
name: The GraphQL name of the enum member.
7174
deprecation_reason: The deprecation reason of the enum member,
7275
setting this will mark the enum member as deprecated.
7376
directives: The directives to attach to the enum member.
@@ -90,6 +93,7 @@ class MyEnum(Enum):
9093
"""
9194
return EnumValueDefinition(
9295
value=value,
96+
graphql_name=name,
9397
deprecation_reason=deprecation_reason,
9498
directives=directives,
9599
description=description,
@@ -123,12 +127,16 @@ def _process_enum(
123127
item_directives = item_value.directives
124128
enum_value_description = item_value.description
125129
deprecation_reason = item_value.deprecation_reason
126-
item_value = item_value.value
127130

128131
# update _value2member_map_ so that doing `MyEnum.MY_VALUE` and
129132
# `MyEnum['MY_VALUE']` both work
130-
cls._value2member_map_[item_value] = item
131-
cls._member_map_[item_name]._value_ = item_value
133+
cls._value2member_map_[item_value.value] = item
134+
cls._member_map_[item_name]._value_ = item_value.value
135+
136+
if item_value.graphql_name:
137+
item_name = item_value.graphql_name
138+
139+
item_value = item_value.value
132140

133141
value = EnumValue(
134142
item_name,

tests/schema/test_enum.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,46 @@ def receive_enum(self, test: TestEnum) -> str:
431431
assert result.data["a"] == "TestEnum.A"
432432
assert result.data["b"] == "TestEnum.B"
433433
assert result.data["c"] == "TestEnum.C"
434+
435+
436+
def test_can_give_custom_name_to_enum_value():
437+
@strawberry.enum
438+
class IceCreamFlavour(Enum):
439+
VANILLA = "vanilla"
440+
CHOCOLATE_COOKIE = strawberry.enum_value("chocolate", name="chocolateCookie")
441+
442+
@strawberry.type
443+
class Query:
444+
@strawberry.field
445+
def best_flavour(self) -> IceCreamFlavour:
446+
return IceCreamFlavour.CHOCOLATE_COOKIE
447+
448+
schema = strawberry.Schema(query=Query)
449+
450+
assert (
451+
str(schema)
452+
== dedent(
453+
"""
454+
enum IceCreamFlavour {
455+
VANILLA
456+
chocolateCookie
457+
}
458+
459+
type Query {
460+
bestFlavour: IceCreamFlavour!
461+
}
462+
"""
463+
).strip()
464+
)
465+
466+
query = """
467+
{
468+
bestFlavour
469+
}
470+
"""
471+
472+
result = schema.execute_sync(query)
473+
474+
assert not result.errors
475+
assert result.data
476+
assert result.data["bestFlavour"] == "chocolateCookie"

0 commit comments

Comments
 (0)