Serializing native Python enums does not work
See original GitHub issueCurrently using SQL enums works well, unless you use a enum.Enum as base for the enum. For example using this model:
class Hairkind(enum.Enum):
LONG = 'long'
SHORT = 'short'
class Pet(Base):
__tablename__ = 'pets'
id = Column(Integer(), primary_key=True)
hair_kind = Column(Enum(Hairkind, name='hair_kind'), nullable=False)
will fail badly if you try to query the hairKind field:
File "lib/python3.7/site-packages/graphql/execution/executor.py", line 622, in complete_leaf_value
path=path,
graphql.error.base.GraphQLError: Expected a value of type "hair_kind" but received: Hairkind.LONG
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Serialising an Enum member to JSON - Stack Overflow
I just went through this exact problem and discovered if you're using ... JSON serializable names # https://docs.python.org/3/library/enum.
Read more >enum.IntEnum is not compatible with JSON serialisation
Messages (50). msg191467 - (view), Author: Nick Coghlan (ncoghlan) * (Python committer), Date: 2013-06-19 13:45.
Read more >Python Generated Code | Protocol Buffers - Google Developers
If you assign a value that is not in the enum, the generated code will throw an exception. proto3 uses open enum semantics:...
Read more >MessagePack: It's like JSON. but fast and small.
MessagePack is an efficient binary serialization format. It lets you exchange data ... If you want to use msgpack at OCaml, you need...
Read more >PHP 8.1: Enums
Note that due to class-semantics of Enums, class_exists function also returns true for an Enum. UnitEnum interface. Enums that are not backed with...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

What is happening here is that
graphqln_sqlalchemy.converter.convert_sqlalchemy_type.registercreates a GraphQL Enum using the enum instances usingitems = type.enum_class.__members__.items(). But ingraphql.type.definition.GraphQLEnumType.serializethis happens:at that point the enum.Enum instance is converted to its value, which is then looked up. But the lookup table has the enum instances, causing the lookup to fail and an error to be returned.
Yeah this has broken enums in one of my libraries and the only way i can fix it is to monkey patch the method!
id suggest a non breaking change, for example: