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 .objecttype import ObjectType
from .inputobjecttype import InputObjectType
from .structures import List, NonNull
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):
if isinstance(_type, (List, NonNull)):
return True
if inspect.isclass(_type) and issubclass(_type, (ObjectType, Scalar)):
if inspect.isclass(_type) and issubclass(_type, (ObjectType, InputObjectType, Scalar)):
return True
@ -29,7 +30,7 @@ class TypeMap(GraphQLTypeMap):
@classmethod
def graphene_reducer(cls, map, type):
if isinstance(type, List):
if isinstance(type, (List, NonNull)):
return cls.reducer(map, type.of_type)
return map
if type._meta.name in map:
@ -39,6 +40,8 @@ class TypeMap(GraphQLTypeMap):
return map
if issubclass(type, ObjectType):
return cls.construct_objecttype(map, type)
if issubclass(type, InputObjectType):
return cls.construct_inputobjecttype(map, type)
if issubclass(type, Scalar):
return cls.construct_scalar(map, type)
return map
@ -74,7 +77,7 @@ class TypeMap(GraphQLTypeMap):
graphene_type=type,
name=type._meta.name,
description=type._meta.description,
fields={},
fields=None,
is_type_of=type.is_type_of,
interfaces=type._meta.interfaces
)
@ -82,18 +85,41 @@ class TypeMap(GraphQLTypeMap):
return map
@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()
for name, field in type._meta.fields.items():
map = cls.reducer(map, field.type)
field_type = cls.get_field_type(map, field.type)
_field = GraphQLField(
field_type,
args=field.args,
resolver=field.resolver,
deprecation_reason=field.deprecation_reason,
description=field.description
)
if is_input:
_field = GraphQLInputObjectField(
field_type,
default_value=field.default_value,
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
return fields