mirror of
https://github.com/graphql-python/graphene-django.git
synced 2025-02-07 15:10:35 +03:00
Merge pull request #395 from mbarrientos/drf-list-serializers
Fixes #371 - Support ListSerializer fields in SerializerMutation
This commit is contained in:
commit
fdb7d5a253
|
@ -46,6 +46,15 @@ def convert_serializer_field(field, is_input=True):
|
||||||
global_registry = get_global_registry()
|
global_registry = get_global_registry()
|
||||||
field_model = field.Meta.model
|
field_model = field.Meta.model
|
||||||
args = [global_registry.get_type_for_model(field_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)
|
return graphql_type(*args, **kwargs)
|
||||||
|
|
||||||
|
@ -75,6 +84,12 @@ def convert_serializer_to_field(field):
|
||||||
return graphene.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)
|
@get_graphene_type_from_serializer_field.register(serializers.IntegerField)
|
||||||
def convert_serializer_field_to_int(field):
|
def convert_serializer_field_to_int(field):
|
||||||
return graphene.Int
|
return graphene.Int
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import copy
|
import copy
|
||||||
from rest_framework import serializers
|
|
||||||
from py.test import raises
|
|
||||||
|
|
||||||
import graphene
|
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 ..serializer_converter import convert_serializer_field
|
||||||
from ..types import DictType
|
from ..types import DictType
|
||||||
|
@ -74,7 +76,6 @@ def test_should_uuid_convert_string():
|
||||||
|
|
||||||
|
|
||||||
def test_should_model_convert_field():
|
def test_should_model_convert_field():
|
||||||
|
|
||||||
class MyModelSerializer(serializers.ModelSerializer):
|
class MyModelSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = None
|
model = None
|
||||||
|
@ -128,6 +129,30 @@ def test_should_list_convert_to_list():
|
||||||
assert field_b.of_type == graphene.String
|
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():
|
def test_should_dict_convert_dict():
|
||||||
assert_conversion(serializers.DictField, DictType)
|
assert_conversion(serializers.DictField, DictType)
|
||||||
|
|
||||||
|
@ -157,6 +182,6 @@ def test_should_json_convert_jsonstring():
|
||||||
|
|
||||||
|
|
||||||
def test_should_multiplechoicefield_convert_to_list_of_string():
|
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
|
assert field.of_type == graphene.String
|
||||||
|
|
Loading…
Reference in New Issue
Block a user