Skip to content

Commit 407b9ea

Browse files
committed
Added Enum class type
1 parent 0531623 commit 407b9ea

File tree

7 files changed

+934
-12
lines changed

7 files changed

+934
-12
lines changed

examples/starwars/schema.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
from graphql.core.type import GraphQLEnumValue
2-
31
import graphene
42
from graphene import resolve_only_args
53

64
from .data import get_character, get_droid, get_hero, get_human
75

8-
Episode = graphene.Enum('Episode', dict(
9-
NEWHOPE=GraphQLEnumValue(4),
10-
EMPIRE=GraphQLEnumValue(5),
11-
JEDI=GraphQLEnumValue(6)
12-
))
6+
7+
class Episode(graphene.Enum):
8+
NEWHOPE = 4
9+
EMPIRE = 5
10+
JEDI = 6
1311

1412

1513
class Character(graphene.Interface):

graphene/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
from graphql.core.type import (
2-
GraphQLEnumType as Enum
3-
)
4-
51
from graphene import signals
62

73
from .core import (
@@ -11,6 +7,7 @@
117
Interface,
128
Mutation,
139
Scalar,
10+
Enum,
1411
InstanceType,
1512
LazyType,
1613
Argument,
@@ -58,6 +55,7 @@
5855
'Interface',
5956
'Mutation',
6057
'Scalar',
58+
'Enum',
6159
'Field',
6260
'InputField',
6361
'StringField',

graphene/core/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
InputObjectType,
88
Interface,
99
Mutation,
10-
Scalar
10+
Scalar,
11+
Enum
1112
)
1213

1314
from .types import (
@@ -42,5 +43,6 @@
4243
'Interface',
4344
'Mutation',
4445
'Scalar',
46+
'Enum',
4547
'Field',
4648
'InputField']

graphene/core/classtypes/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from .objecttype import ObjectType
55
from .options import Options
66
from .scalar import Scalar
7+
from .enum import Enum
78
from .uniontype import UnionType
89

910
__all__ = [
@@ -13,4 +14,5 @@
1314
'ObjectType',
1415
'Options',
1516
'Scalar',
17+
'Enum',
1618
'UnionType']

graphene/core/classtypes/enum.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import six
2+
from graphql.core.type import GraphQLEnumType, GraphQLEnumValue
3+
4+
from .base import ClassTypeMeta, ClassType
5+
from ...utils.enum import Enum as PyEnum
6+
7+
8+
class EnumMeta(ClassTypeMeta):
9+
10+
def construct(cls, bases, attrs):
11+
__enum__ = attrs.get('__enum__', None)
12+
if not cls._meta.abstract and not __enum__:
13+
__enum__ = PyEnum(cls._meta.type_name, attrs)
14+
setattr(cls, '__enum__', __enum__)
15+
if __enum__:
16+
for k, v in __enum__.__members__.items():
17+
attrs[k] = v.value
18+
return super(EnumMeta, cls).construct(bases, attrs)
19+
20+
def __call__(cls, name, names=None, description=None):
21+
attrs = {
22+
'__enum__': PyEnum(name, names)
23+
}
24+
if description:
25+
attrs['__doc__'] = description
26+
return type(name, (Enum,), attrs)
27+
28+
29+
class Enum(six.with_metaclass(EnumMeta, ClassType)):
30+
31+
class Meta:
32+
abstract = True
33+
34+
@classmethod
35+
def internal_type(cls, schema):
36+
if cls._meta.abstract:
37+
raise Exception("Abstract Enum don't have a specific type.")
38+
39+
values = {k: GraphQLEnumValue(v.value) for k, v in cls.__enum__.__members__.items()}
40+
# GraphQLEnumValue
41+
return GraphQLEnumType(
42+
cls._meta.type_name,
43+
values=values,
44+
description=cls._meta.description,
45+
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from graphql.core.type import GraphQLEnumType
2+
3+
from graphene.core.schema import Schema
4+
5+
from ..enum import Enum
6+
7+
8+
def test_enum():
9+
class RGB(Enum):
10+
'''RGB enum description'''
11+
RED = 0
12+
GREEN = 1
13+
BLUE = 2
14+
15+
schema = Schema()
16+
17+
object_type = schema.T(RGB)
18+
assert isinstance(object_type, GraphQLEnumType)
19+
assert RGB._meta.type_name == 'RGB'
20+
assert RGB._meta.description == 'RGB enum description'
21+
assert RGB.RED == 0
22+
assert RGB.GREEN == 1
23+
assert RGB.BLUE == 2
24+
25+
26+
def test_enum_values():
27+
RGB = Enum('RGB', dict(RED=0, GREEN=1, BLUE=2), description='RGB enum description')
28+
29+
schema = Schema()
30+
31+
object_type = schema.T(RGB)
32+
assert isinstance(object_type, GraphQLEnumType)
33+
assert RGB._meta.type_name == 'RGB'
34+
assert RGB._meta.description == 'RGB enum description'
35+
assert RGB.RED == 0
36+
assert RGB.GREEN == 1
37+
assert RGB.BLUE == 2

0 commit comments

Comments
 (0)