-
-
Notifications
You must be signed in to change notification settings - Fork 137
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
Error when trying to serialize a Python enum #73
Comments
The error message is a bit confusing, because the GraphQL and Python enum types have the same name "MyEnum". The
or
The values correspond to what is used in the data on the Python GraphQL server, the names correspond to what shall be used in the results on the GraphQL client. GraphQL-core will automatically translate between names and values. I plan to write a detailed blog article on enum types in GraphQL to make all of this a bit more clear. |
Hey thanks for the response. The alternative (in the case where my data model is using PyEnums and I want to serve them as GQL enums) is adding a custom resolver for every Enum field isn't it? (I know I could generalize such a resolver, but still..) Jarrad |
It depends on your backend data. If it uses 'YES' and 'NO', the above approach would work. But maybe you're right, and this feature should be re-examined. So we need to investigate and clarify this question: When definining a GraphQLEnum via a Python Enum, should the values of the GraphQLEnum be the enum members (i.e. As far as I see, graphql-core 2 only allowed OrderedDicts when defining GraphQLEnums. In graphql-core 3 you can still use dicts, and they behave the same, but if you use enums, it takes the enum values as GraphQL values. If you want to use the members, you currently can pass Maybe we can add an option or helper function that allows using enums in both ways. This could also help with backward compatibility. Keep in mind graphql-core is a low-level library used by others such as Graphene, and we cannot simply introduce such breaking changes. I think I will investigate this a bit more when I will write the blog article. I particularly want to deal with the situation where you have a database backend. In this case, you may have a database (like PostgreSQL) which has a particular enum type, and an ORM (like SQLAlchemy) with its own enum type, which may or may not be based on a Python enum class. I want all of this to play together seamlessly. Leaving this open as a reminder to re-examine and write that blog post. |
Thanks for the consideration. For reference I am going to use the following GraphQLEnumType definition based on your workaround: |
@akdor1154 it should be I agree that it would make more sense like this but I understand that it is a breaking change and thus difficult to change without impacting a lot of users. Maybe that could be planned in a future major version bump ? Note: made some tests with enums in gql PR 256 |
Made this configurable now. With |
When returning a Python Enum-type object, I get an exception:
graphql.error.graphql_error.GraphQLError: Expected a value of type 'MyEnum' but received: <MyEnum instance>
Reproducible example:
The text was updated successfully, but these errors were encountered: