Skip to content

Commit 366e941

Browse files
committed
Translate Python enumeration values in GraphQLEnumType when using a Python enumeraiton as the value type.
1 parent d8e9d3a commit 366e941

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

graphql/type/definition.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,10 @@ def parse_literal(self, value_ast):
551551
@cached_property
552552
def _value_lookup(self):
553553
# type: () -> Dict[str, GraphQLEnumValue]
554-
return {value.value: value for value in self.values}
554+
return {
555+
value.value.value if isinstance(value.value, PyEnum) else value.value: value
556+
for value in self.values
557+
}
555558

556559
@cached_property
557560
def _name_lookup(self):

graphql/type/tests/test_serialization.py

+21
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,24 @@ class Color(Enum):
8181
assert enum_type.serialize(Color.RED.value) == "RED"
8282
assert enum_type.serialize(Color.EXTRA) is None
8383
assert enum_type.serialize(Color.EXTRA.value) is None
84+
85+
86+
def test_serialize_enum_pyenum():
87+
class Color(Enum):
88+
RED = 1
89+
GREEN = 2
90+
BLUE = 3
91+
EXTRA = 4
92+
93+
enum_type = GraphQLEnumType(
94+
"Color",
95+
values={
96+
"RED": GraphQLEnumValue(Color.RED),
97+
"GREEN": GraphQLEnumValue(Color.GREEN),
98+
"BLUE": GraphQLEnumValue(Color.BLUE),
99+
},
100+
)
101+
assert enum_type.serialize(Color.RED) == "RED"
102+
assert enum_type.serialize(Color.RED.value) == "RED"
103+
assert enum_type.serialize(Color.EXTRA) is None
104+
assert enum_type.serialize(Color.EXTRA.value) is None

0 commit comments

Comments
 (0)