From f5b5d36f724019b6479ac845ca89a9ece5bd68a9 Mon Sep 17 00:00:00 2001 From: Chinonso Ani <31407950+KingNonso@users.noreply.github.com> Date: Wed, 11 Aug 2021 14:01:42 +0100 Subject: [PATCH] Use `client_mutation_id` for update When using relay, it overrides the id of the model and provides a global id. This id is often what is available when making queries. However, it is unusable to update the model object. This seeks to make the `client_mutation_id` usable for updating the SerializerMutation --- graphene_django/rest_framework/mutation.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 000b21e..c5868bf 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -9,6 +9,8 @@ from graphene.types import Field, InputField from graphene.types.mutation import MutationOptions from graphene.types.objecttype import yank_fields_from_attrs +from graphql_relay import from_global_id + from ..types import ErrorType from .serializer_converter import convert_serializer_field @@ -125,9 +127,16 @@ class SerializerMutation(ClientIDMutation): if model_class: if "update" in cls._meta.model_operations and lookup_field in input: - instance = get_object_or_404( - model_class, **{lookup_field: input[lookup_field]} - ) + if 'client_mutation_id' in input: + global_id = input.pop("client_mutation_id", None) + node_type, global_pk = from_global_id(global_id) + instance = get_object_or_404( + model_class, **{lookup_field: global_pk} + ) + else: + instance = get_object_or_404( + model_class, **{lookup_field: input[lookup_field]} + ) partial = True elif "create" in cls._meta.model_operations: instance = None