Closed cngai closed 8 months ago
Hey @cngai ,
As I said on discord, I usually use django-choices-field which automatically integrates with strawberry: https://strawberry-graphql.github.io/strawberry-django/integrations/choices-field/, so I was not really aware of this =P
I think your "Potential Fix" is the way to go here! Do you want to try to open a PR with that solution? Let me know if I can help you with that :)
Thanks for taking a look @bellini666 ! I'll put up a PR in the next few days. Appreciate the help!
When calling a mutation with an input argument that accepts an enum value, instead of saving the enum value to the field, the object attempts to save the enum itself, thus throwing a Django ValidationError
"Value 'ExampleEnum.VALUE' is not a valid choice."
.Describe the Bug
Example
In the example above, Strawberry would auto-generate a
FruitStatusEnum
for me since theFruit.status
model field has definedchoices
. The input type for myFruitInputPartial.status
field is auto-resolved asFruitStatusEnum
. However, when I call the mutation defined above, it tries saving the status field asFruitStatusEnum.FRESH
as opposed toFruitStatusEnum.FRESH.value
, which is why I get thedjango.core.exceptions.ValidationError: {'status': ["Value 'FruitStatusEnum.FRESH' is not a valid choice."]}
error.Reproduction Steps
GENERATE_ENUMS_FROM_CHOICES
to beTrue
by adding the@override_settings
decorator to the test (shown below).Add this decorator to the test function
@override_settings( STRAWBERRY_DJANGO={ **strawberry_django_settings(), "GENERATE_ENUMS_FROM_CHOICES": True, }, ) @pytest.mark.django_db(transaction=True) def test_input_update_mutation(db, gql_client: GraphQLTestClient): query = """ mutation CreateIssue ($input: IssueInputPartial!) { updateIssue (input: $input) { ...
{'updateIssue': {'__typename': 'OperationInfo', 'messages': [{'kind': 'VALIDATION', 'field': 'kind', 'message': "Value 'ProjectsIssueKindEnum.f' is not a valid choice."}]}}
Additional Context
The current workaround is to change the
strawberry.auto
type for my input field to bestr | None
, but then I lose the strong typing for my client-side app.Let me know if you need any more information or if you'd like me to submit a PR. Thanks!
System Information
Upvote & Fund