Skip to content

Fixes #371 - Support ListSerializer fields in SerializerMutation #395

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 1 commit into from
Mar 15, 2018
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
15 changes: 15 additions & 0 deletions graphene_django/rest_framework/serializer_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ def convert_serializer_field(field, is_input=True):
global_registry = get_global_registry()
field_model = field.Meta.model
args = [global_registry.get_type_for_model(field_model)]
elif isinstance(field, serializers.ListSerializer):
field = field.child
if is_input:
kwargs['of_type'] = convert_serializer_to_input_type(field.__class__)
else:
del kwargs['of_type']
global_registry = get_global_registry()
field_model = field.Meta.model
args = [global_registry.get_type_for_model(field_model)]

return graphql_type(*args, **kwargs)

Expand Down Expand Up @@ -75,6 +84,12 @@ def convert_serializer_to_field(field):
return graphene.Field


@get_graphene_type_from_serializer_field.register(serializers.ListSerializer)
def convert_list_serializer_to_field(field):
child_type = get_graphene_type_from_serializer_field(field.child)
return (graphene.List, child_type)


@get_graphene_type_from_serializer_field.register(serializers.IntegerField)
def convert_serializer_field_to_int(field):
return graphene.Int
Expand Down
33 changes: 29 additions & 4 deletions graphene_django/rest_framework/tests/test_field_converter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import copy
from rest_framework import serializers
from py.test import raises

import graphene
from django.db import models
from graphene import InputObjectType
from py.test import raises
from rest_framework import serializers

from ..serializer_converter import convert_serializer_field
from ..types import DictType
Expand Down Expand Up @@ -74,7 +76,6 @@ def test_should_uuid_convert_string():


def test_should_model_convert_field():

class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = None
Expand Down Expand Up @@ -128,6 +129,30 @@ class StringListField(serializers.ListField):
assert field_b.of_type == graphene.String


def test_should_list_serializer_convert_to_list():
class FooModel(models.Model):
pass

class ChildSerializer(serializers.ModelSerializer):
class Meta:
model = FooModel
fields = '__all__'

class ParentSerializer(serializers.ModelSerializer):
child = ChildSerializer(many=True)

class Meta:
model = FooModel
fields = '__all__'

converted_type = convert_serializer_field(ParentSerializer().get_fields()['child'], is_input=True)
assert isinstance(converted_type, graphene.List)

converted_type = convert_serializer_field(ParentSerializer().get_fields()['child'], is_input=False)
assert isinstance(converted_type, graphene.List)
assert converted_type.of_type is None


def test_should_dict_convert_dict():
assert_conversion(serializers.DictField, DictType)

Expand Down Expand Up @@ -157,6 +182,6 @@ def test_should_json_convert_jsonstring():


def test_should_multiplechoicefield_convert_to_list_of_string():
field = assert_conversion(serializers.MultipleChoiceField, graphene.List, choices=[1,2,3])
field = assert_conversion(serializers.MultipleChoiceField, graphene.List, choices=[1, 2, 3])

assert field.of_type == graphene.String