Skip to content

Commit

Permalink
Added Enum type ability to be mounted as field or argument
Browse files Browse the repository at this point in the history
  • Loading branch information
syrusakbary committed Apr 2, 2016
1 parent 68a9fb9 commit e1a693e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 0 additions & 1 deletion graphene/contrib/django/converter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import Iterable
from django.db import models

from ...core.types.scalars import ID, Boolean, Float, Int, String
Expand Down
10 changes: 8 additions & 2 deletions graphene/core/classtypes/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from graphql.core.type import GraphQLEnumType, GraphQLEnumValue

from .base import ClassTypeMeta, ClassType
from ..types.base import MountedType
from ...utils.enum import Enum as PyEnum


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

def __call__(cls, name, names=None, description=None):
def __call__(cls, *args, **kwargs):
if cls is Enum:
return cls.create_enum(*args, **kwargs)
return super(EnumMeta, cls).__call__(*args, **kwargs)

def create_enum(cls, name, names=None, description=None):
attrs = {
'__enum__': PyEnum(name, names)
}
Expand All @@ -26,7 +32,7 @@ def __call__(cls, name, names=None, description=None):
return type(name, (Enum,), attrs)


class Enum(six.with_metaclass(EnumMeta, ClassType)):
class Enum(six.with_metaclass(EnumMeta, ClassType, MountedType)):

class Meta:
abstract = True
Expand Down
12 changes: 12 additions & 0 deletions graphene/core/classtypes/tests/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from graphene.core.schema import Schema

from ..enum import Enum
from ..objecttype import ObjectType


def test_enum():
Expand Down Expand Up @@ -35,3 +36,14 @@ def test_enum_values():
assert RGB.RED == 0
assert RGB.GREEN == 1
assert RGB.BLUE == 2


def test_enum_instance():
RGB = Enum('RGB', dict(RED=0, GREEN=1, BLUE=2))
RGB_field = RGB(description='RGB enum description')

class ObjectWithColor(ObjectType):
color = RGB_field

object_field = ObjectWithColor._meta.fields_map['color']
assert object_field.description == 'RGB enum description'
3 changes: 1 addition & 2 deletions graphene/utils/tests/test_str_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ def test_camel_case():


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

0 comments on commit e1a693e

Please sign in to comment.