mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-02-16 19:40:36 +03:00
added support for partial updates in serializers (#731)
* added support for partial updates in serializers * Add test to verify partial updates Co-authored-by: Jonathan Kim <jkimbo@gmail.com>
This commit is contained in:
parent
45df7445f4
commit
7940a7b954
|
@ -102,8 +102,10 @@ class SerializerMutation(ClientIDMutation):
|
||||||
instance = get_object_or_404(
|
instance = get_object_or_404(
|
||||||
model_class, **{lookup_field: input[lookup_field]}
|
model_class, **{lookup_field: input[lookup_field]}
|
||||||
)
|
)
|
||||||
|
partial = True
|
||||||
elif "create" in cls._meta.model_operations:
|
elif "create" in cls._meta.model_operations:
|
||||||
instance = None
|
instance = None
|
||||||
|
partial = False
|
||||||
else:
|
else:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
'Invalid update operation. Input parameter "{}" required.'.format(
|
'Invalid update operation. Input parameter "{}" required.'.format(
|
||||||
|
@ -115,6 +117,7 @@ class SerializerMutation(ClientIDMutation):
|
||||||
"instance": instance,
|
"instance": instance,
|
||||||
"data": input,
|
"data": input,
|
||||||
"context": {"request": info.context},
|
"context": {"request": info.context},
|
||||||
|
"partial": partial,
|
||||||
}
|
}
|
||||||
|
|
||||||
return {"data": input, "context": {"request": info.context}}
|
return {"data": input, "context": {"request": info.context}}
|
||||||
|
|
|
@ -183,6 +183,16 @@ def test_model_update_mutate_and_get_payload_success():
|
||||||
assert result.cool_name == "New Narf"
|
assert result.cool_name == "New Narf"
|
||||||
|
|
||||||
|
|
||||||
|
@mark.django_db
|
||||||
|
def test_model_partial_update_mutate_and_get_payload_success():
|
||||||
|
instance = MyFakeModel.objects.create(cool_name="Narf")
|
||||||
|
result = MyModelMutation.mutate_and_get_payload(
|
||||||
|
None, mock_info(), **{"id": instance.id}
|
||||||
|
)
|
||||||
|
assert result.errors is None
|
||||||
|
assert result.cool_name == "Narf"
|
||||||
|
|
||||||
|
|
||||||
@mark.django_db
|
@mark.django_db
|
||||||
def test_model_invalid_update_mutate_and_get_payload_success():
|
def test_model_invalid_update_mutate_and_get_payload_success():
|
||||||
class InvalidModelMutation(SerializerMutation):
|
class InvalidModelMutation(SerializerMutation):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user