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

SerializerMutation not resolving Enums from serializer with ChoiceField #1385

Closed
allen-munsch opened this issue Jan 23, 2023 · 3 comments · Fixed by #1431
Closed

SerializerMutation not resolving Enums from serializer with ChoiceField #1385

allen-munsch opened this issue Jan 23, 2023 · 3 comments · Fixed by #1431
Labels

Comments

@allen-munsch
Copy link
Contributor

allen-munsch commented Jan 23, 2023

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

@katomaso
Copy link

katomaso commented Mar 7, 2023

The same issue:
python==3.9.16
graphene==3.2.1
graphene-django==3.0.0

@mahmoudmostafa0
Copy link
Contributor

i got the same issue

@sjdemartini
Copy link
Collaborator

Thanks for the detailed description of the bug and error! I'm assuming this bug was introduced in the upgrade from graphene v2 to v3, where enums in mutations are now resolved as the enum objects rather than enum values: https://github.com/graphql-python/graphene/wiki/v3-release-notes#better-enum-support

We'd definitely welcome a PR that fixes SerializerMutation to account for this!

allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 14, 2023
allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 24, 2023
allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 24, 2023
allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 24, 2023
allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 24, 2023
… explicit with the naming, remove comment since naming is more verbose
allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 24, 2023
… explicit with the naming, remove comment since naming is more verbose
allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 24, 2023
allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 24, 2023
allen-munsch added a commit to allen-munsch/graphene-django that referenced this issue Jul 24, 2023
…um objects into input values for ChoiceFields
firaskafri pushed a commit that referenced this issue Jul 26, 2023
* Fix for issue #1385: Update mutation.py to serialize Enum objects into input values for ChoiceFields

* Update graphene_django/rest_framework/mutation.py

Co-authored-by: Steven DeMartini <1647130+sjdemartini@users.noreply.github.com>

---------

Co-authored-by: Steven DeMartini <1647130+sjdemartini@users.noreply.github.com>
superlevure pushed a commit to loft-orbital/graphene-django that referenced this issue Aug 9, 2023
…ql-python#1431)

* Fix for issue graphql-python#1385: Update mutation.py to serialize Enum objects into input values for ChoiceFields

* Update graphene_django/rest_framework/mutation.py

Co-authored-by: Steven DeMartini <1647130+sjdemartini@users.noreply.github.com>

---------

Co-authored-by: Steven DeMartini <1647130+sjdemartini@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants