Account for nested ModelSerializers

This commit is contained in:
Jacob Foster 2017-07-11 13:29:30 -05:00
parent 93bbc194bf
commit 302ea0d3cf

View File

@ -3,6 +3,7 @@ from rest_framework import serializers
import graphene import graphene
from ..registry import get_global_registry
from ..utils import import_single_dispatch from ..utils import import_single_dispatch
from .types import DictType from .types import DictType
@ -41,6 +42,7 @@ def convert_serializer_field(field, is_input=True):
graphql_type = get_graphene_type_from_serializer_field(field) graphql_type = get_graphene_type_from_serializer_field(field)
args = []
kwargs = { kwargs = {
'description': field.help_text, 'description': field.help_text,
'required': is_input and field.required, 'required': is_input and field.required,
@ -52,7 +54,15 @@ def convert_serializer_field(field, is_input=True):
kwargs['of_type'] = graphql_type[1] kwargs['of_type'] = graphql_type[1]
graphql_type = graphql_type[0] graphql_type = graphql_type[0]
return graphql_type(**kwargs) if isinstance(field, serializers.ModelSerializer):
if is_input:
graphql_type = convert_serializer_to_input_type(field.__class__)
else:
global_registry = get_global_registry()
field_model = field.Meta.model
args = [global_registry.get_type_for_model(field_model)]
return graphql_type(*args, **kwargs)
@get_graphene_type_from_serializer_field.register(serializers.Field) @get_graphene_type_from_serializer_field.register(serializers.Field)
@ -60,6 +70,11 @@ def convert_serializer_field_to_string(field):
return graphene.String return graphene.String
@get_graphene_type_from_serializer_field.register(serializers.ModelSerializer)
def convert_serializer_to_field(field):
return graphene.Field
@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