Skip to content

SerializerMutation not resolving Enums from serializer with ChoiceField #1385

Closed
@allen-munsch

Description

@allen-munsch

Why would this be the case?

I am using the latest branch.

class SomeCreateSerializerMutation(SerializerMutation)
    ID = graphene.ID()

    class Meta:
        serializer_class = SomeListViewSerializer

    @classmethod 
    def mutate_and_get_payload(cls, root, info, **input):
        # kwargs = cls.get_serializer_kwargs(root, info, **input)
        kwargs = patched_get_serializer_kwargs(cls, root, info, **input)
class SomeListViewSerializer(...):
    choice_type = serializers.ChoiceField(
           choices=choice.CHOICE_TYPE_CHOICES, allow_blank=True, required=False
    )

where i have to:

def patched_get_serializer_kwargs(cls, root, info, **input):
    '''
    This is a patched version of:
        graphene_django.rest_framework.mutation.SerializerMutation.get_serializer_kwargs

    ############################
    # field.run_validation(primitive_value)
    # the serializer mutation type is not converting the `Choice_Type`,
    # after it is passed to the serializer kwargs
    # (Pdb) kwargs
    # {'instance': None, 'data': {'choice_type': <choice_type.TURN: 'TURN'>,
    # 'name': '', 'description': ''},
    # 'context': {'request': <WSGIRequest: POST '/graphql'>}, 'partial': False}
    # (Pdb) c
    # -> if serializer.is_valid():
    # (Pdb) serializer.is_valid()
    # False
    # (Pdb) serializer.errors
    # {'choice_type': [ErrorDetail(string='"choice_type.TURN" is not a valid choice.', code='invalid_choice')]}
    # input['choice_type'] = input['choice_type'].value
    #########################
    '''
    lookup_field = cls._meta.lookup_field
    model_class = cls._meta.model_class
    if model_class:
        ############ PATCH #################
        # i'm not sure why
        for k, v in input.items():
            attr = getattr(cls.Input, k)
            type_ = getattr(attr, 'type')
            if hasattr(v, 'value') and type_.get(v.value):
                input[k] = v.value
        ############ END PATCH #################

        if "update" in cls._meta.model_operations and lookup_field in input:
            instance = get_object_or_404(
                model_class, **{lookup_field: input[lookup_field]}
            )
            partial = True
        elif "create" in cls._meta.model_operations:
            instance = None
            partial = False
        else:
            raise Exception(
                'Invalid update operation. Input parameter "{}" required.'.format(
                    lookup_field
                )
            )

        return {
            "instance": instance,
            "data": input,
            "context": {"request": info.context},
            "partial": partial,
        }

DRF version 3.14
django 3.2
python 3.9.10
graphene-django==3.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions