Skip to content

Commit fb2af70

Browse files
committed
Initial implementation of serializer field converter
1 parent 7c52aa3 commit fb2af70

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

graphene_django/rest_framework/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from functools import singledispatch
2+
3+
from django.core.exceptions import ImproperlyConfigured
4+
from rest_framework import serializers
5+
6+
import graphene
7+
8+
9+
@singledispatch
10+
def convert_serializer_field(field):
11+
raise ImproperlyConfigured(
12+
"Don't know how to convert the serializer field %s (%s) "
13+
"to Graphene type" % (field, field.__class__)
14+
)
15+
16+
17+
def required_if_input_and_required(func):
18+
"""
19+
Marks the field as required if we are creating an input type
20+
and the field itself is required
21+
"""
22+
23+
def wrap(field, is_input=True):
24+
graphql_type = func(field)
25+
26+
return graphql_type(
27+
description=field.help_text, required=is_input and field.required
28+
)
29+
30+
return wrap
31+
32+
33+
@convert_serializer_field.register(serializers.Field)
34+
@required_if_input_and_required
35+
def convert_serializer_field_to_string(field):
36+
return graphene.String
37+
38+
39+
@convert_serializer_field.register(serializers.IntegerField)
40+
@required_if_input_and_required
41+
def convert_serializer_field_to_int(field):
42+
return graphene.Int
43+
44+
45+
@convert_serializer_field.register(serializers.BooleanField)
46+
@required_if_input_and_required
47+
def convert_serializer_field_to_bool(field):
48+
return graphene.Boolean
49+
50+
51+
@convert_serializer_field.register(serializers.FloatField)
52+
@convert_serializer_field.register(serializers.DecimalField)
53+
@required_if_input_and_required
54+
def convert_serializer_field_to_float(field):
55+
return graphene.Float
56+
57+

graphene_django/rest_framework/tests/__init__.py

Whitespace-only changes.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from rest_framework import serializers
2+
from py.test import raises
3+
4+
import graphene
5+
6+
from ..serializer_converter import convert_serializer_field
7+
8+
9+
# TODO: test required
10+
11+
def assert_conversion(rest_framework_field, graphene_field, **kwargs):
12+
field = rest_framework_field(help_text='Custom Help Text', **kwargs)
13+
graphene_type = convert_serializer_field(field)
14+
assert isinstance(graphene_type, graphene_field)
15+
16+
field = graphene_type.Field()
17+
assert field.description == 'Custom Help Text'
18+
assert not isinstance(field, graphene.NonNull)
19+
20+
field = rest_framework_field(help_text='Custom Help Text', required=True, **kwargs)
21+
graphene_type = convert_serializer_field(field)
22+
field = graphene_type.Field()
23+
assert isinstance(field.type, graphene.NonNull)
24+
25+
return field
26+
27+
28+
def test_should_unknown_rest_framework_field_raise_exception():
29+
with raises(Exception) as excinfo:
30+
convert_serializer_field(None)
31+
assert 'Don\'t know how to convert the serializer field' in str(excinfo.value)
32+
33+
34+
def test_should_date_convert_string():
35+
assert_conversion(serializers.DateField, graphene.String)
36+
37+
38+
def test_should_time_convert_string():
39+
assert_conversion(serializers.TimeField, graphene.String)
40+
41+
42+
def test_should_date_time_convert_string():
43+
assert_conversion(serializers.DateTimeField, graphene.String)
44+
45+
46+
def test_should_char_convert_string():
47+
assert_conversion(serializers.CharField, graphene.String)
48+
49+
50+
def test_should_email_convert_string():
51+
assert_conversion(serializers.EmailField, graphene.String)
52+
53+
54+
def test_should_slug_convert_string():
55+
assert_conversion(serializers.SlugField, graphene.String)
56+
57+
58+
def test_should_url_convert_string():
59+
assert_conversion(serializers.URLField, graphene.String)
60+
61+
62+
def test_should_choice_convert_string():
63+
assert_conversion(serializers.ChoiceField, graphene.String, choices=[])
64+
65+
66+
def test_should_base_field_convert_string():
67+
assert_conversion(serializers.Field, graphene.String)
68+
69+
70+
def test_should_regex_convert_string():
71+
assert_conversion(serializers.RegexField, graphene.String, regex='[0-9]+')
72+
73+
74+
def test_should_uuid_convert_string():
75+
if hasattr(serializers, 'UUIDField'):
76+
assert_conversion(serializers.UUIDField, graphene.String)
77+
78+
79+
def test_should_integer_convert_int():
80+
assert_conversion(serializers.IntegerField, graphene.Int)
81+
82+
83+
def test_should_boolean_convert_boolean():
84+
assert_conversion(serializers.BooleanField, graphene.Boolean)
85+
86+
87+
def test_should_float_convert_float():
88+
assert_conversion(serializers.FloatField, graphene.Float)
89+
90+
91+
def test_should_decimal_convert_float():
92+
assert_conversion(serializers.DecimalField, graphene.Float, max_digits=4, decimal_places=2)

0 commit comments

Comments
 (0)