Skip to content

Commit c130490

Browse files
committed
ensure SerializerMutation.errors is None on success in 2.x
Upon success the result was correct but also included: "errors": [ { "message": "User Error: expected iterable, but did not find one for field <SerializerMutation_Subclass>Payload.errors." } ] This seemed to be due to Payload.errors defaulting to graphene.List rather than unset or None. Unsure what exactly changed with 2.x to break this, so I welcome a better fix, but explicitly setting errors to None also seems easy enough.
1 parent cc58b91 commit c130490

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

graphene_django/rest_framework/mutation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ def mutate_and_get_payload(cls, root, info, **input):
8484
@classmethod
8585
def perform_mutate(cls, serializer, info):
8686
obj = serializer.save()
87-
return cls(**obj)
87+
return cls(errors=None, **obj)

graphene_django/rest_framework/tests/test_mutation.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class MySerializer(serializers.Serializer):
2222
text = serializers.CharField()
2323
model = MyModelSerializer()
2424

25+
def create(self, validated_data):
26+
return validated_data
27+
2528

2629
def test_needs_serializer_class():
2730
with raises(Exception) as exc:
@@ -68,3 +71,29 @@ class Meta:
6871
model_input_type = model_input._type.of_type
6972
assert issubclass(model_input_type, InputObjectType)
7073
assert 'cool_name' in model_input_type._meta.fields
74+
75+
76+
def test_mutate_and_get_payload_success():
77+
78+
class MyMutation(SerializerMutation):
79+
class Meta:
80+
serializer_class = MySerializer
81+
82+
result = MyMutation.mutate_and_get_payload(None, None, **{
83+
'text': 'value',
84+
'model': {
85+
'cool_name': 'other_value'
86+
}
87+
})
88+
assert result.errors is None
89+
90+
91+
def test_mutate_and_get_payload_error():
92+
93+
class MyMutation(SerializerMutation):
94+
class Meta:
95+
serializer_class = MySerializer
96+
97+
# missing required fields
98+
result = MyMutation.mutate_and_get_payload(None, None, **{})
99+
assert len(result.errors) > 0

0 commit comments

Comments
 (0)