-
Notifications
You must be signed in to change notification settings - Fork 766
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
Choice fields automatically converted to enums #67
Comments
My personal opinion is that it should be easy to make choices an Enum, but that it shouldn't be the default. The requirements of django choices are much, much more permissive than those of an Enum (in either Python or GraphQL) |
I also ran into this but I thought it were intensional. |
Hi, How I can overwrite the "A_1" output? Thanks |
Choices are also converted to uppercase, which makes it difficult to manage during mutations, especially if the choices were mixed upper and lower case. There should be an option to just leave your choices alone. |
Maybe someone adds |
What would be the desired behavior for enums? I think it makes sense to handle them automatically unless explicitly defined on in the schema but maybe we can introduce some config to handle the different use cases. |
Way I see it there are 3 options:
There are quite a few issues related to this behavior:
|
I like the simplicity of #209 but don't like that it relies on another dependency and requires changes in the model layer. After reading the various proposals, I think a hybrid approach might work.
|
Please add one more step to the hybrid approach above:
|
@denis-ryzhkov updated the original post—removed the list approach and replaced with the global setting. Custom resolvers can be used instead of |
Any updates on this? Using the monkeypatch for now, but that's really sketchy. |
@subwindow we've made no development process on this but it looks like we've agreed on the approach we will take, as @mvanlonden said:
We are currently focused on setting up governance and organising the support around the graphene project as a whole, when that is more stable this should be one of the first features we tackle. I want it too, so I can't wait to get started on it! |
There's also an issue with enum fields containing non-English letters and throwing an error. I guess a way to just disable the conversion overall would be fine, there are many edge cases like that that this conversion doesn't cover BTW, the integration with Django filter lib is weird too, |
#654 Is a small improvement for generating Enums with integer fields. Looks like we need tests with non-english choices. |
@kaglowka v2.4.0 contains a new feature which allows you to turn off automatic enum creation on a case by case basis: http://docs.graphene-python.org/projects/django/en/latest/queries/#choices-to-enum-conversion |
If I want to use the converted choices Enum as the filter argument. class Label(BaseModel):
TYPES = [(x, x) for x in ("AAA", "BBB")]
name = models.CharField(max_length=256)
type = models.CharField(max_length=8, null=True, choices=TYPES)
class LabelQL(DjangoObjectType):
class Meta:
model = Label
only_fields = ("name", "type")
name = "Label"
LabelType = LabelQL._meta.fields["type"].type # That is tricky
class Query(graphene.ObjectType):
labels = graphene.List(LabelQL, types=graphene.List(LabelType))
def resolve_labels(root, info, types=None):
qs = Label.objects.all()
if types and len(types) > 0:
qs = qs.filter(type__in=types)
return qs I have to get the converted type from I there any natural way to use |
@jkimbo What about users using Flask? How can we turn this feature off? |
@levrik you will have to raise that issue with the graphene-flask repo |
Why is this closed? I still have this issue. |
@Suor because Graphene Django now lets you turn off the automatic enum conversion: http://docs.graphene-python.org/projects/django/en/latest/queries/#choices-to-enum-conversion |
I found that this works
|
Hello I have managed to make the Choices field use an Enum, In the past I have done
I know that i can introspect the whole schema with
but how do I introspect the specific type generated for the Enum choices ? Update found solution
|
User model:
Query
Result
A copy-paste from where the weird value is born:
We've had similar problems in the old graphene (< 1.0): graphql-python/graphene#134
The output is "A_1", because the field converter assumes django choices are tuples (id, name) where name is a constant name (not containing spaces etc.) or is convertible to a valid constant name. For some reason it is not successful while doing that for "site 1", but that is not an issue. The problem is it assumes we would like to do that.
In our django application (I suppose it is the norm) we use the second tuple element as a human-readable label. It means that in most cases replacing space with an underscore will do the work, but
in the other names are still bound to be long and there is no character we can safely assume the name won't contain (including hyphens, all native character varieties etc.).
The Enum fields are certainly nice, as their values are schema-validated, to say the least, but I doubt automatic conversion should be the only possible behaviour.
The text was updated successfully, but these errors were encountered: