Skip to content

Commit e1a693e

Browse files
committed
Added Enum type ability to be mounted as field or argument
1 parent 68a9fb9 commit e1a693e

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

graphene/contrib/django/converter.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections import Iterable
21
from django.db import models
32

43
from ...core.types.scalars import ID, Boolean, Float, Int, String

graphene/core/classtypes/enum.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from graphql.core.type import GraphQLEnumType, GraphQLEnumValue
33

44
from .base import ClassTypeMeta, ClassType
5+
from ..types.base import MountedType
56
from ...utils.enum import Enum as PyEnum
67

78

@@ -17,7 +18,12 @@ def construct(cls, bases, attrs):
1718
attrs[k] = v.value
1819
return super(EnumMeta, cls).construct(bases, attrs)
1920

20-
def __call__(cls, name, names=None, description=None):
21+
def __call__(cls, *args, **kwargs):
22+
if cls is Enum:
23+
return cls.create_enum(*args, **kwargs)
24+
return super(EnumMeta, cls).__call__(*args, **kwargs)
25+
26+
def create_enum(cls, name, names=None, description=None):
2127
attrs = {
2228
'__enum__': PyEnum(name, names)
2329
}
@@ -26,7 +32,7 @@ def __call__(cls, name, names=None, description=None):
2632
return type(name, (Enum,), attrs)
2733

2834

29-
class Enum(six.with_metaclass(EnumMeta, ClassType)):
35+
class Enum(six.with_metaclass(EnumMeta, ClassType, MountedType)):
3036

3137
class Meta:
3238
abstract = True

graphene/core/classtypes/tests/test_enum.py

+12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from graphene.core.schema import Schema
44

55
from ..enum import Enum
6+
from ..objecttype import ObjectType
67

78

89
def test_enum():
@@ -35,3 +36,14 @@ def test_enum_values():
3536
assert RGB.RED == 0
3637
assert RGB.GREEN == 1
3738
assert RGB.BLUE == 2
39+
40+
41+
def test_enum_instance():
42+
RGB = Enum('RGB', dict(RED=0, GREEN=1, BLUE=2))
43+
RGB_field = RGB(description='RGB enum description')
44+
45+
class ObjectWithColor(ObjectType):
46+
color = RGB_field
47+
48+
object_field = ObjectWithColor._meta.fields_map['color']
49+
assert object_field.description == 'RGB enum description'

graphene/utils/tests/test_str_converter.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ def test_camel_case():
1919

2020

2121
def test_to_const():
22-
assert to_const('snakes on a plane') == 'SNAKES_ON_A_PLANE'
23-
assert to_const('weirdñáunicode$# word') == 'WEIRD_UNICODE_WORD'
22+
assert to_const('snakes $1. on a "#plane') == 'SNAKES_ON_A_PLANE'

0 commit comments

Comments
 (0)