From 3d021ce6bf104350486bc81cbe3e1d3da4e8058d Mon Sep 17 00:00:00 2001 From: Jacob Smallwood Date: Mon, 2 Oct 2017 14:32:25 -0400 Subject: [PATCH 1/2] Handle serialization of standard library Enum values --- graphql/type/definition.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/graphql/type/definition.py b/graphql/type/definition.py index b10a5428..a420c097 100644 --- a/graphql/type/definition.py +++ b/graphql/type/definition.py @@ -6,6 +6,11 @@ from ..pyutils.ordereddict import OrderedDict from ..utils.assert_valid_name import assert_valid_name +try: + from enum import Enum as PyEnum +except ImportError: + from ..pyutils.enum import Enum as PyEnum + def is_type(type): return isinstance(type, ( @@ -407,8 +412,9 @@ def __init__(self, name, values, description=None): self.values = define_enum_values(self, values) def serialize(self, value): - if isinstance(value, collections.Hashable): - enum_value = self._value_lookup.get(value) + input_value = value.value if isinstance(value, PyEnum) else value + if isinstance(input_value, collections.Hashable): + enum_value = self._value_lookup.get(input_value) if enum_value: return enum_value.name From 5878bfefd8fce90412056407b83c8c21dc2d50cc Mon Sep 17 00:00:00 2001 From: Jacob Smallwood Date: Mon, 2 Oct 2017 15:44:26 -0400 Subject: [PATCH 2/2] pyutils.enum does not exist in graphql-core --- graphql/type/definition.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphql/type/definition.py b/graphql/type/definition.py index a420c097..cda2d00f 100644 --- a/graphql/type/definition.py +++ b/graphql/type/definition.py @@ -9,7 +9,7 @@ try: from enum import Enum as PyEnum except ImportError: - from ..pyutils.enum import Enum as PyEnum + PyEnum = None def is_type(type): @@ -412,7 +412,7 @@ def __init__(self, name, values, description=None): self.values = define_enum_values(self, values) def serialize(self, value): - input_value = value.value if isinstance(value, PyEnum) else value + input_value = value.value if PyEnum is not None and isinstance(value, PyEnum) else value if isinstance(input_value, collections.Hashable): enum_value = self._value_lookup.get(input_value)