mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-02 20:54:16 +03:00
Added Enum type ability to be mounted as field or argument
This commit is contained in:
parent
68a9fb9fc4
commit
e1a693e57c
|
@ -1,4 +1,3 @@
|
|||
from collections import Iterable
|
||||
from django.db import models
|
||||
|
||||
from ...core.types.scalars import ID, Boolean, Float, Int, String
|
||||
|
|
|
@ -2,6 +2,7 @@ import six
|
|||
from graphql.core.type import GraphQLEnumType, GraphQLEnumValue
|
||||
|
||||
from .base import ClassTypeMeta, ClassType
|
||||
from ..types.base import MountedType
|
||||
from ...utils.enum import Enum as PyEnum
|
||||
|
||||
|
||||
|
@ -17,7 +18,12 @@ class EnumMeta(ClassTypeMeta):
|
|||
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)
|
||||
}
|
||||
|
@ -26,7 +32,7 @@ class EnumMeta(ClassTypeMeta):
|
|||
return type(name, (Enum,), attrs)
|
||||
|
||||
|
||||
class Enum(six.with_metaclass(EnumMeta, ClassType)):
|
||||
class Enum(six.with_metaclass(EnumMeta, ClassType, MountedType)):
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
|
|
@ -3,6 +3,7 @@ from graphql.core.type import GraphQLEnumType
|
|||
from graphene.core.schema import Schema
|
||||
|
||||
from ..enum import Enum
|
||||
from ..objecttype import ObjectType
|
||||
|
||||
|
||||
def test_enum():
|
||||
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
Loading…
Reference in New Issue
Block a user