-
Notifications
You must be signed in to change notification settings - Fork 769
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
Mutation can not represent TextChoice value #1476
Comments
I'm facing the same issue. Is there any solution? |
It seems working if I return the value (not Enum).
|
I believe the problem is in this check: class GrapheneEnumType(GrapheneGraphQLType, GraphQLEnumType):
def serialize(self, value):
if not isinstance(value, PyEnum): # <-------
...
return super(GrapheneEnumType, self).serialize(value) Graphene check that field is enum, but expect that this is graphene enum, and in case it's get django enum exception is raised. I think it can be fixed with: if not isinstance(value, self.graphene_type._meta.enum): |
Have you ever tried |
But it will change scheme type to string right? But I'd like to have enum type |
I've found a way to handle django class choices in graphene-django. I'm not sure that this is the right place to do it, but the main idea is to check if the return value has a choice type and return its value. I assume that it's better to handle this in # graphene_django/converter.py
@@ -54,6 +56,8 @@ class BlankValueField(Field):
return_value = func(*args, **kwargs)
if return_value == "":
return None
+ if isinstance(return_value, models.Choices):
+ return return_value.value
return return_value
return wrapped_resolver |
And here is a monkey patch to use it right now: from graphene.types.resolver import dict_or_attr_resolver, set_default_resolver
def custom_resolver(*args, **kwargs):
resolved = dict_or_attr_resolver(*args, **kwargs)
if isinstance(resolved, models.Choices):
resolved = resolved.value
return resolved
set_default_resolver(custom_resolver)
schema = graphene.Schema(...) |
I'd love to have this working in Graphene too! Are there any updates on whether this will be addressed in a future release? |
Note: for support questions, please use stackoverflow. This repository's issues are reserved for feature requests and bug reports.
When executing a mutation that returns a Type of a Model that has a CharField with a TextChoices enum, it returns the following error:
"Enum 'Status' cannot represent value: <AppointmentStatus.CANCELLED: 'C'>"
a github repo, https://repl.it or similar (you can use this template as a starting point: https://repl.it/@jkimbo/Graphene-Django-Example).
Define these classes
When using this mutation and querying the result status
and querying the result status I receive the following error:
What is the expected behavior?
The appointment status values should be shown without errors.
What is the motivation / use case for changing the behavior?
Please tell us about your environment:
Other information (e.g. detailed explanation, stacktraces, related issues, suggestions how to fix, links for us to have context, eg. stackoverflow)
I'm trying to migrate from graphene-django 2.15.0 to 3.1.5.
These are the whole traces of the problem:
The text was updated successfully, but these errors were encountered: