Moved Enum logic

This commit is contained in:
Syrus Akbary 2016-06-22 23:30:49 -07:00
parent 3529fcb29b
commit cbf0d8fedd
4 changed files with 32 additions and 27 deletions

View File

@ -1,4 +1,5 @@
from .definitions import GrapheneInterfaceType, GrapheneObjectType, GrapheneScalarType from .definitions import GrapheneInterfaceType, GrapheneObjectType, GrapheneScalarType, GrapheneEnumType
from .utils import values_from_enum
def generate_interface(interface): def generate_interface(interface):
@ -32,3 +33,12 @@ def generate_scalar(scalar):
parse_value=getattr(scalar, 'parse_value', None), parse_value=getattr(scalar, 'parse_value', None),
parse_literal=getattr(scalar, 'parse_literal', None), parse_literal=getattr(scalar, 'parse_literal', None),
) )
def generate_enum(enum):
return GrapheneEnumType(
graphene_type=enum,
values=values_from_enum(enum._meta.enum),
name=enum._meta.name or enum.__name__,
description=enum._meta.description or enum.__doc__,
)

View File

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

View File

@ -0,0 +1,14 @@
from collections import OrderedDict
from graphql.type import GraphQLEnumValue
def values_from_enum(enum):
_values = OrderedDict()
for name, value in enum.__members__.items():
_values[name] = GraphQLEnumValue(
name=name,
value=value.value,
description=getattr(value, 'description', None),
deprecation_reason=getattr(value, 'deprecation_reason', None)
)
return _values

View File

@ -2,10 +2,7 @@ from collections import OrderedDict
import six import six
from graphql.type import GraphQLEnumType, GraphQLEnumValue
from ..utils.is_base_type import is_base_type from ..utils.is_base_type import is_base_type
from .definitions import GrapheneGraphQLType
from .options import Options from .options import Options
from .unmountedtype import UnmountedType from .unmountedtype import UnmountedType
@ -14,21 +11,7 @@ try:
except ImportError: except ImportError:
from ..utils.enum import Enum as PyEnum from ..utils.enum import Enum as PyEnum
from ..generators import generate_enum
class GrapheneEnumType(GrapheneGraphQLType, GraphQLEnumType):
pass
def values_from_enum(enum):
_values = OrderedDict()
for name, value in enum.__members__.items():
_values[name] = GraphQLEnumValue(
name=name,
value=value.value,
description=getattr(value, 'description', None),
deprecation_reason=getattr(value, 'deprecation_reason', None)
)
return _values
class EnumTypeMeta(type): class EnumTypeMeta(type):
@ -56,13 +39,7 @@ class EnumTypeMeta(type):
cls = super_new(cls, name, bases, new_attrs) cls = super_new(cls, name, bases, new_attrs)
if not options.graphql_type: if not options.graphql_type:
values = values_from_enum(options.enum) options.graphql_type = generate_enum(cls)
options.graphql_type = GrapheneEnumType(
graphene_type=cls,
values=values,
name=options.name or cls.__name__,
description=options.description or cls.__doc__,
)
return cls return cls