Added InputObjectType conversion

This commit is contained in:
Syrus Akbary 2016-08-13 09:39:46 -07:00
parent 0f66dba7e8
commit 0802aaced0

View File

@ -4,16 +4,17 @@ from collections import OrderedDict
from graphql.type.typemap import GraphQLTypeMap from graphql.type.typemap import GraphQLTypeMap
from .objecttype import ObjectType from .objecttype import ObjectType
from .inputobjecttype import InputObjectType
from .structures import List, NonNull from .structures import List, NonNull
from .scalars import Scalar, String, Boolean, Int, Float, ID from .scalars import Scalar, String, Boolean, Int, Float, ID
from graphql import GraphQLString, GraphQLField, GraphQLList, GraphQLBoolean, GraphQLInt, GraphQLFloat, GraphQLID, GraphQLNonNull from graphql import GraphQLString, GraphQLField, GraphQLList, GraphQLBoolean, GraphQLInt, GraphQLFloat, GraphQLID, GraphQLNonNull, GraphQLInputObjectField
def is_graphene_type(_type): def is_graphene_type(_type):
if isinstance(_type, (List, NonNull)): if isinstance(_type, (List, NonNull)):
return True return True
if inspect.isclass(_type) and issubclass(_type, (ObjectType, Scalar)): if inspect.isclass(_type) and issubclass(_type, (ObjectType, InputObjectType, Scalar)):
return True return True
@ -29,7 +30,7 @@ class TypeMap(GraphQLTypeMap):
@classmethod @classmethod
def graphene_reducer(cls, map, type): def graphene_reducer(cls, map, type):
if isinstance(type, List): if isinstance(type, (List, NonNull)):
return cls.reducer(map, type.of_type) return cls.reducer(map, type.of_type)
return map return map
if type._meta.name in map: if type._meta.name in map:
@ -39,6 +40,8 @@ class TypeMap(GraphQLTypeMap):
return map return map
if issubclass(type, ObjectType): if issubclass(type, ObjectType):
return cls.construct_objecttype(map, type) return cls.construct_objecttype(map, type)
if issubclass(type, InputObjectType):
return cls.construct_inputobjecttype(map, type)
if issubclass(type, Scalar): if issubclass(type, Scalar):
return cls.construct_scalar(map, type) return cls.construct_scalar(map, type)
return map return map
@ -74,7 +77,7 @@ class TypeMap(GraphQLTypeMap):
graphene_type=type, graphene_type=type,
name=type._meta.name, name=type._meta.name,
description=type._meta.description, description=type._meta.description,
fields={}, fields=None,
is_type_of=type.is_type_of, is_type_of=type.is_type_of,
interfaces=type._meta.interfaces interfaces=type._meta.interfaces
) )
@ -82,18 +85,41 @@ class TypeMap(GraphQLTypeMap):
return map return map
@classmethod @classmethod
def construct_fields_for_type(cls, map, type): def construct_inputobjecttype(cls, map, type):
from ..generators.definitions import GrapheneInputObjectType
map[type._meta.name] = GrapheneInputObjectType(
graphene_type=type,
name=type._meta.name,
description=type._meta.description,
fields=None,
)
map[type._meta.name].fields = cls.construct_fields_for_type(map, type, is_input=True)
return map
@classmethod
def construct_fields_for_type(cls, map, type, is_input=False):
fields = OrderedDict() fields = OrderedDict()
for name, field in type._meta.fields.items(): for name, field in type._meta.fields.items():
map = cls.reducer(map, field.type) map = cls.reducer(map, field.type)
field_type = cls.get_field_type(map, field.type) field_type = cls.get_field_type(map, field.type)
_field = GraphQLField( if is_input:
field_type, _field = GraphQLInputObjectField(
args=field.args, field_type,
resolver=field.resolver, default_value=field.default_value,
deprecation_reason=field.deprecation_reason, description=field.description
description=field.description )
) else:
args = OrderedDict()
for arg_name, arg in field.args.items():
map = cls.reducer(map, arg.type)
args[arg_name] = cls.get_field_type(map, arg.type)
_field = GraphQLField(
field_type,
args=args,
resolver=field.resolver,
deprecation_reason=field.deprecation_reason,
description=field.description
)
fields[name] = _field fields[name] = _field
return fields return fields