Skip to content

Consistently translate Python enumerations to values #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion graphql/type/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,10 @@ def parse_literal(self, value_ast):
@cached_property
def _value_lookup(self):
# type: () -> Dict[str, GraphQLEnumValue]
return {value.value: value for value in self.values}
return {
value.value.value if isinstance(value.value, PyEnum) else value.value: value
for value in self.values
}

@cached_property
def _name_lookup(self):
Expand Down
21 changes: 21 additions & 0 deletions graphql/type/tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,24 @@ class Color(Enum):
assert enum_type.serialize(Color.RED.value) == "RED"
assert enum_type.serialize(Color.EXTRA) is None
assert enum_type.serialize(Color.EXTRA.value) is None


def test_serialize_enum_pyenum():
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
EXTRA = 4

enum_type = GraphQLEnumType(
"Color",
values={
"RED": GraphQLEnumValue(Color.RED),
"GREEN": GraphQLEnumValue(Color.GREEN),
"BLUE": GraphQLEnumValue(Color.BLUE),
},
)
assert enum_type.serialize(Color.RED) == "RED"
assert enum_type.serialize(Color.RED.value) == "RED"
assert enum_type.serialize(Color.EXTRA) is None
assert enum_type.serialize(Color.EXTRA.value) is None