Skip to content

fix various SerializerMutation regressions #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion graphene_django/rest_framework/mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init_subclass_with_meta__(cls, serializer_class=None,
output_fields = fields_for_serializer(serializer, only_fields, exclude_fields, is_input=False)

_meta = SerializerMutationOptions(cls)
_meta.serializer_class = serializer_class
_meta.fields = yank_fields_from_attrs(
output_fields,
_as=Field,
Expand Down Expand Up @@ -83,4 +84,4 @@ def mutate_and_get_payload(cls, root, info, **input):
@classmethod
def perform_mutate(cls, serializer, info):
obj = serializer.save()
return cls(**obj)
return cls(errors=None, **obj)
18 changes: 16 additions & 2 deletions graphene_django/rest_framework/serializer_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def convert_serializer_field(field, is_input=True):

if isinstance(field, serializers.ModelSerializer):
if is_input:
return Dynamic(lambda: None)
# graphql_type = convert_serializer_to_input_type(field.__class__)
graphql_type = convert_serializer_to_input_type(field.__class__)
else:
global_registry = get_global_registry()
field_model = field.Meta.model
Expand All @@ -52,6 +51,21 @@ def convert_serializer_field(field, is_input=True):
return graphql_type(*args, **kwargs)


def convert_serializer_to_input_type(serializer_class):
serializer = serializer_class()

items = {
name: convert_serializer_field(field)
for name, field in serializer.fields.items()
}

return type(
'{}Input'.format(serializer.__class__.__name__),
(graphene.InputObjectType,),
items
)


@get_graphene_type_from_serializer_field.register(serializers.Field)
def convert_serializer_field_to_string(field):
return graphene.String
Expand Down
36 changes: 32 additions & 4 deletions graphene_django/rest_framework/tests/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class MySerializer(serializers.Serializer):
text = serializers.CharField()
model = MyModelSerializer()

def create(self, validated_data):
return validated_data


def test_needs_serializer_class():
with raises(Exception) as exc:
Expand Down Expand Up @@ -65,7 +68,32 @@ class Meta:
assert model_field.type == MyFakeModelGrapheneType

model_input = MyMutation.Input._meta.fields['model']
model_input_type = model_input.get_type()
assert not model_input_type
# assert issubclass(model_input_type, InputObjectType)
# assert 'cool_name' in model_input_type._meta.fields
model_input_type = model_input._type.of_type
assert issubclass(model_input_type, InputObjectType)
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