Refactored InputObjectType

This commit is contained in:
Syrus Akbary 2016-06-22 23:35:50 -07:00
parent cbf0d8fedd
commit b6522f393c
3 changed files with 18 additions and 13 deletions

View File

@ -1,4 +1,4 @@
from .definitions import GrapheneInterfaceType, GrapheneObjectType, GrapheneScalarType, GrapheneEnumType
from .definitions import GrapheneInterfaceType, GrapheneObjectType, GrapheneScalarType, GrapheneEnumType, GrapheneInputObjectType
from .utils import values_from_enum
@ -42,3 +42,12 @@ def generate_enum(enum):
name=enum._meta.name or enum.__name__,
description=enum._meta.description or enum.__doc__,
)
def generate_inputobjecttype(inputobjecttype):
return GrapheneInputObjectType(
graphene_type=inputobjecttype,
name=inputobjecttype._meta.name or inputobjecttype.__name__,
description=inputobjecttype._meta.description or inputobjecttype.__doc__,
fields=inputobjecttype._meta.get_fields,
)

View File

@ -1,4 +1,4 @@
from graphql import GraphQLObjectType, GraphQLInterfaceType, GraphQLScalarType, GraphQLEnumType
from graphql import GraphQLObjectType, GraphQLInterfaceType, GraphQLScalarType, GraphQLEnumType, GraphQLInputObjectType
class GrapheneGraphQLType(object):
@ -35,3 +35,7 @@ class GrapheneScalarType(GrapheneGraphQLType, GraphQLScalarType):
class GrapheneEnumType(GrapheneGraphQLType, GraphQLEnumType):
pass
class GrapheneInputObjectType(GrapheneGraphQLType, GraphQLInputObjectType):
pass

View File

@ -1,19 +1,15 @@
import six
from graphql import GraphQLInputObjectType
from ..utils.copy_fields import copy_fields
from ..utils.get_fields import get_fields
from ..utils.is_base_type import is_base_type
from .definitions import GrapheneGraphQLType
from .field import InputField
from .objecttype import attrs_without_fields
from .options import Options
from .unmountedtype import UnmountedType
class GrapheneInputObjectType(GrapheneGraphQLType, GraphQLInputObjectType):
pass
from ..generators import generate_inputobjecttype
class InputObjectTypeMeta(type):
@ -39,12 +35,8 @@ class InputObjectTypeMeta(type):
if not options.graphql_type:
fields = copy_fields(InputField, fields, parent=cls)
options.graphql_type = GrapheneInputObjectType(
graphene_type=cls,
name=options.name or cls.__name__,
description=options.description or cls.__doc__,
fields=fields,
)
options.get_fields = lambda: fields
options.graphql_type = generate_inputobjecttype(cls)
else:
assert not fields, "Can't mount InputFields in an InputObjectType with a defined graphql_type"
fields = copy_fields(InputField, options.graphql_type.get_fields(), parent=cls)