Skip to content
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

Update mutation.py to serialize Enum objects into input values #1431

Merged
merged 2 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions graphene_django/rest_framework/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ class MyFakeModelWithPassword(models.Model):
class MyFakeModelWithDate(models.Model):
cool_name = models.CharField(max_length=50)
last_edited = models.DateField()


class MyFakeModelWithChoiceField(models.Model):
class ChoiceType(models.Choices):
ASDF = "asdf"
HI = "hi"

choice_type = models.CharField(
max_length=4,
default=ChoiceType.HI.name,
)
6 changes: 5 additions & 1 deletion graphene_django/rest_framework/mutation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

from collections import OrderedDict

from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -124,8 +126,10 @@ def __init_subclass_with_meta__(
def get_serializer_kwargs(cls, root, info, **input):
lookup_field = cls._meta.lookup_field
model_class = cls._meta.model_class

if model_class:
for input_dict_key, maybe_enum in input.items():
if isinstance(maybe_enum, Enum):
input[input_dict_key] = maybe_enum.value
if "update" in cls._meta.model_operations and lookup_field in input:
instance = get_object_or_404(
model_class, **{lookup_field: input[lookup_field]}
Expand Down
40 changes: 39 additions & 1 deletion graphene_django/rest_framework/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
from graphene.types.inputobjecttype import InputObjectType

from ...types import DjangoObjectType
from ..models import MyFakeModel, MyFakeModelWithDate, MyFakeModelWithPassword
from ..models import (
MyFakeModel,
MyFakeModelWithDate,
MyFakeModelWithPassword,
MyFakeModelWithChoiceField,
)
from ..mutation import SerializerMutation


Expand Down Expand Up @@ -268,6 +273,39 @@ class Meta:
assert result.days_since_last_edit == 4


def test_perform_mutate_success_with_enum_choice_field():
allen-munsch marked this conversation as resolved.
Show resolved Hide resolved
class ListViewChoiceFieldSerializer(serializers.ModelSerializer):
choice_type = serializers.ChoiceField(
choices=[(x.name, x.value) for x in MyFakeModelWithChoiceField.ChoiceType],
required=False,
)

class Meta:
model = MyFakeModelWithChoiceField
fields = "__all__"

class SomeCreateSerializerMutation(SerializerMutation):
class Meta:
serializer_class = ListViewChoiceFieldSerializer

choice_type = {
"choice_type": SomeCreateSerializerMutation.Input.choice_type.type.get("ASDF")
}
name = MyFakeModelWithChoiceField.ChoiceType.ASDF.name
result = SomeCreateSerializerMutation.mutate_and_get_payload(
None, mock_info(), **choice_type
)
assert result.errors is None
assert result.choice_type == name
kwargs = SomeCreateSerializerMutation.get_serializer_kwargs(
None, mock_info(), **choice_type
)
assert kwargs["data"]["choice_type"] == name
assert 1 == MyFakeModelWithChoiceField.objects.count()
item = MyFakeModelWithChoiceField.objects.first()
assert item.choice_type == name


def test_mutate_and_get_payload_error():
class MyMutation(SerializerMutation):
class Meta:
Expand Down