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.
This commit is contained in:
Andy Clayton 2017-08-31 12:15:18 -05:00
parent cc58b91e43
commit c130490b4f
2 changed files with 30 additions and 1 deletions

View File

@ -84,4 +84,4 @@ class SerializerMutation(ClientIDMutation):
@classmethod @classmethod
def perform_mutate(cls, serializer, info): def perform_mutate(cls, serializer, info):
obj = serializer.save() obj = serializer.save()
return cls(**obj) return cls(errors=None, **obj)

View File

@ -22,6 +22,9 @@ class MySerializer(serializers.Serializer):
text = serializers.CharField() text = serializers.CharField()
model = MyModelSerializer() model = MyModelSerializer()
def create(self, validated_data):
return validated_data
def test_needs_serializer_class(): def test_needs_serializer_class():
with raises(Exception) as exc: with raises(Exception) as exc:
@ -68,3 +71,29 @@ def test_nested_model():
model_input_type = model_input._type.of_type model_input_type = model_input._type.of_type
assert issubclass(model_input_type, InputObjectType) assert issubclass(model_input_type, InputObjectType)
assert 'cool_name' in model_input_type._meta.fields assert 'cool_name' in model_input_type._meta.fields
def test_mutate_and_get_payload_success():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MySerializer
result = MyMutation.mutate_and_get_payload(None, None, **{
'text': 'value',
'model': {
'cool_name': 'other_value'
}
})
assert result.errors is None
def test_mutate_and_get_payload_error():
class MyMutation(SerializerMutation):
class Meta:
serializer_class = MySerializer
# missing required fields
result = MyMutation.mutate_and_get_payload(None, None, **{})
assert len(result.errors) > 0