From 0b5f8e8507851dfe2c48cf27b307a97aed92d6f6 Mon Sep 17 00:00:00 2001 From: Luke Hodkinson Date: Mon, 30 Oct 2017 14:57:35 +1100 Subject: [PATCH] Allow upserts when converting DRF serializers Combine create and update into a single mutation. This can be disabled by returning None from `load_existing`. --- graphene_django/rest_framework/mutation.py | 25 +++++++++++++--------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/graphene_django/rest_framework/mutation.py b/graphene_django/rest_framework/mutation.py index 94d1e4b..d90de01 100644 --- a/graphene_django/rest_framework/mutation.py +++ b/graphene_django/rest_framework/mutation.py @@ -1,16 +1,12 @@ from collections import OrderedDict import graphene +from graphene.relay.mutation import ClientIDMutation from graphene.types import Field, InputField from graphene.types.mutation import MutationOptions -from graphene.relay.mutation import ClientIDMutation -from graphene.types.objecttype import ( - yank_fields_from_attrs -) +from graphene.types.objecttype import yank_fields_from_attrs -from .serializer_converter import ( - convert_serializer_field -) +from .serializer_converter import convert_serializer_field from .types import ErrorType @@ -69,7 +65,8 @@ class SerializerMutation(ClientIDMutation): @classmethod def mutate_and_get_payload(cls, root, info, **input): - serializer = cls._meta.serializer_class(data=input) + existing = cls.load_existing(cls._meta.serializer_class, input) + serializer = cls._meta.serializer_class(existing, data=input) if serializer.is_valid(): return cls.perform_mutate(serializer, info) @@ -81,7 +78,15 @@ class SerializerMutation(ClientIDMutation): return cls(errors=errors) + @classmethod + def load_existing(cls, serializer_class, data): + id = data.get('id') + if id is not None: + model_class = serializer_class.Meta.model + return model_class.objects.get(id=data['id']) + return None + @classmethod def perform_mutate(cls, serializer, info): - obj = serializer.save() - return cls(errors=None, **obj) + serializer.save() + return cls(errors=None, **serializer.data)