mirror of
https://github.com/graphql-python/graphene-django.git
synced 2024-11-10 19:57:15 +03:00
Initial implementation of serializer field converter
This commit is contained in:
parent
7c52aa3c7f
commit
fb2af7067f
0
graphene_django/rest_framework/__init__.py
Normal file
0
graphene_django/rest_framework/__init__.py
Normal file
57
graphene_django/rest_framework/serializer_converter.py
Normal file
57
graphene_django/rest_framework/serializer_converter.py
Normal file
|
@ -0,0 +1,57 @@
|
|||
from functools import singledispatch
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from rest_framework import serializers
|
||||
|
||||
import graphene
|
||||
|
||||
|
||||
@singledispatch
|
||||
def convert_serializer_field(field):
|
||||
raise ImproperlyConfigured(
|
||||
"Don't know how to convert the serializer field %s (%s) "
|
||||
"to Graphene type" % (field, field.__class__)
|
||||
)
|
||||
|
||||
|
||||
def required_if_input_and_required(func):
|
||||
"""
|
||||
Marks the field as required if we are creating an input type
|
||||
and the field itself is required
|
||||
"""
|
||||
|
||||
def wrap(field, is_input=True):
|
||||
graphql_type = func(field)
|
||||
|
||||
return graphql_type(
|
||||
description=field.help_text, required=is_input and field.required
|
||||
)
|
||||
|
||||
return wrap
|
||||
|
||||
|
||||
@convert_serializer_field.register(serializers.Field)
|
||||
@required_if_input_and_required
|
||||
def convert_serializer_field_to_string(field):
|
||||
return graphene.String
|
||||
|
||||
|
||||
@convert_serializer_field.register(serializers.IntegerField)
|
||||
@required_if_input_and_required
|
||||
def convert_serializer_field_to_int(field):
|
||||
return graphene.Int
|
||||
|
||||
|
||||
@convert_serializer_field.register(serializers.BooleanField)
|
||||
@required_if_input_and_required
|
||||
def convert_serializer_field_to_bool(field):
|
||||
return graphene.Boolean
|
||||
|
||||
|
||||
@convert_serializer_field.register(serializers.FloatField)
|
||||
@convert_serializer_field.register(serializers.DecimalField)
|
||||
@required_if_input_and_required
|
||||
def convert_serializer_field_to_float(field):
|
||||
return graphene.Float
|
||||
|
||||
|
0
graphene_django/rest_framework/tests/__init__.py
Normal file
0
graphene_django/rest_framework/tests/__init__.py
Normal file
92
graphene_django/rest_framework/tests/test_field_converter.py
Normal file
92
graphene_django/rest_framework/tests/test_field_converter.py
Normal file
|
@ -0,0 +1,92 @@
|
|||
from rest_framework import serializers
|
||||
from py.test import raises
|
||||
|
||||
import graphene
|
||||
|
||||
from ..serializer_converter import convert_serializer_field
|
||||
|
||||
|
||||
# TODO: test required
|
||||
|
||||
def assert_conversion(rest_framework_field, graphene_field, **kwargs):
|
||||
field = rest_framework_field(help_text='Custom Help Text', **kwargs)
|
||||
graphene_type = convert_serializer_field(field)
|
||||
assert isinstance(graphene_type, graphene_field)
|
||||
|
||||
field = graphene_type.Field()
|
||||
assert field.description == 'Custom Help Text'
|
||||
assert not isinstance(field, graphene.NonNull)
|
||||
|
||||
field = rest_framework_field(help_text='Custom Help Text', required=True, **kwargs)
|
||||
graphene_type = convert_serializer_field(field)
|
||||
field = graphene_type.Field()
|
||||
assert isinstance(field.type, graphene.NonNull)
|
||||
|
||||
return field
|
||||
|
||||
|
||||
def test_should_unknown_rest_framework_field_raise_exception():
|
||||
with raises(Exception) as excinfo:
|
||||
convert_serializer_field(None)
|
||||
assert 'Don\'t know how to convert the serializer field' in str(excinfo.value)
|
||||
|
||||
|
||||
def test_should_date_convert_string():
|
||||
assert_conversion(serializers.DateField, graphene.String)
|
||||
|
||||
|
||||
def test_should_time_convert_string():
|
||||
assert_conversion(serializers.TimeField, graphene.String)
|
||||
|
||||
|
||||
def test_should_date_time_convert_string():
|
||||
assert_conversion(serializers.DateTimeField, graphene.String)
|
||||
|
||||
|
||||
def test_should_char_convert_string():
|
||||
assert_conversion(serializers.CharField, graphene.String)
|
||||
|
||||
|
||||
def test_should_email_convert_string():
|
||||
assert_conversion(serializers.EmailField, graphene.String)
|
||||
|
||||
|
||||
def test_should_slug_convert_string():
|
||||
assert_conversion(serializers.SlugField, graphene.String)
|
||||
|
||||
|
||||
def test_should_url_convert_string():
|
||||
assert_conversion(serializers.URLField, graphene.String)
|
||||
|
||||
|
||||
def test_should_choice_convert_string():
|
||||
assert_conversion(serializers.ChoiceField, graphene.String, choices=[])
|
||||
|
||||
|
||||
def test_should_base_field_convert_string():
|
||||
assert_conversion(serializers.Field, graphene.String)
|
||||
|
||||
|
||||
def test_should_regex_convert_string():
|
||||
assert_conversion(serializers.RegexField, graphene.String, regex='[0-9]+')
|
||||
|
||||
|
||||
def test_should_uuid_convert_string():
|
||||
if hasattr(serializers, 'UUIDField'):
|
||||
assert_conversion(serializers.UUIDField, graphene.String)
|
||||
|
||||
|
||||
def test_should_integer_convert_int():
|
||||
assert_conversion(serializers.IntegerField, graphene.Int)
|
||||
|
||||
|
||||
def test_should_boolean_convert_boolean():
|
||||
assert_conversion(serializers.BooleanField, graphene.Boolean)
|
||||
|
||||
|
||||
def test_should_float_convert_float():
|
||||
assert_conversion(serializers.FloatField, graphene.Float)
|
||||
|
||||
|
||||
def test_should_decimal_convert_float():
|
||||
assert_conversion(serializers.DecimalField, graphene.Float, max_digits=4, decimal_places=2)
|
Loading…
Reference in New Issue
Block a user