Skip to content

Commit f747102

Browse files
committed
Add support for rest framework List Field
1 parent 6de3bbc commit f747102

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

graphene_django/rest_framework/serializer_converter.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,20 @@ def convert_serializer_field(field, is_input=True):
3838
and the field itself is required
3939
"""
4040

41-
# TODO: sub types? kwargs
42-
4341
graphql_type = get_graphene_type_from_serializer_field(field)
4442

45-
return graphql_type(
46-
description=field.help_text, required=is_input and field.required
47-
)
43+
kwargs = {
44+
'description': field.help_text,
45+
'required': is_input and field.required,
46+
}
47+
48+
# if it is a tuple or a list it means that we are returning
49+
# the graphql type and the child type
50+
if isinstance(graphql_type, (list, tuple)):
51+
kwargs['of_type'] = graphql_type[1]
52+
graphql_type = graphql_type[0]
53+
54+
return graphql_type(**kwargs)
4855

4956

5057
@get_graphene_type_from_serializer_field.register(serializers.Field)
@@ -66,3 +73,10 @@ def convert_serializer_field_to_bool(field):
6673
@get_graphene_type_from_serializer_field.register(serializers.DecimalField)
6774
def convert_serializer_field_to_float(field):
6875
return graphene.Float
76+
77+
78+
@get_graphene_type_from_serializer_field.register(serializers.ListField)
79+
def convert_serializer_field_to_list(field, is_input=True):
80+
child_type = get_graphene_type_from_serializer_field(field.child)
81+
82+
return (graphene.List, child_type)

graphene_django/rest_framework/tests/test_field_converter.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,20 @@ def test_should_float_convert_float():
9898

9999
def test_should_decimal_convert_float():
100100
assert_conversion(serializers.DecimalField, graphene.Float, max_digits=4, decimal_places=2)
101+
102+
103+
def test_should_list_convert_to_list():
104+
class StringListField(serializers.ListField):
105+
child = serializers.CharField()
106+
107+
field_a = assert_conversion(
108+
serializers.ListField,
109+
graphene.List,
110+
child=serializers.IntegerField(min_value=0, max_value=100)
111+
)
112+
113+
assert field_a.of_type == graphene.Int
114+
115+
field_b = assert_conversion(StringListField, graphene.List)
116+
117+
assert field_b.of_type == graphene.String

0 commit comments

Comments
 (0)