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
This commit is contained in:
Chinonso Ani 2021-08-11 14:01:42 +01:00 committed by GitHub
parent e7f7d8da07
commit f5b5d36f72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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,6 +127,13 @@ class SerializerMutation(ClientIDMutation):
if model_class:
if "update" in cls._meta.model_operations and lookup_field in input:
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]}
)