mirror of
https://github.com/graphql-python/graphene.git
synced 2025-02-10 00:20:40 +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 django.db import models
|
||||||
|
|
||||||
from ...core.types.scalars import ID, Boolean, Float, Int, String
|
from ...core.types.scalars import ID, Boolean, Float, Int, String
|
||||||
|
|
|
@ -2,6 +2,7 @@ import six
|
||||||
from graphql.core.type import GraphQLEnumType, GraphQLEnumValue
|
from graphql.core.type import GraphQLEnumType, GraphQLEnumValue
|
||||||
|
|
||||||
from .base import ClassTypeMeta, ClassType
|
from .base import ClassTypeMeta, ClassType
|
||||||
|
from ..types.base import MountedType
|
||||||
from ...utils.enum import Enum as PyEnum
|
from ...utils.enum import Enum as PyEnum
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,7 +18,12 @@ class EnumMeta(ClassTypeMeta):
|
||||||
attrs[k] = v.value
|
attrs[k] = v.value
|
||||||
return super(EnumMeta, cls).construct(bases, attrs)
|
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 = {
|
attrs = {
|
||||||
'__enum__': PyEnum(name, names)
|
'__enum__': PyEnum(name, names)
|
||||||
}
|
}
|
||||||
|
@ -26,7 +32,7 @@ class EnumMeta(ClassTypeMeta):
|
||||||
return type(name, (Enum,), attrs)
|
return type(name, (Enum,), attrs)
|
||||||
|
|
||||||
|
|
||||||
class Enum(six.with_metaclass(EnumMeta, ClassType)):
|
class Enum(six.with_metaclass(EnumMeta, ClassType, MountedType)):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
|
@ -3,6 +3,7 @@ from graphql.core.type import GraphQLEnumType
|
||||||
from graphene.core.schema import Schema
|
from graphene.core.schema import Schema
|
||||||
|
|
||||||
from ..enum import Enum
|
from ..enum import Enum
|
||||||
|
from ..objecttype import ObjectType
|
||||||
|
|
||||||
|
|
||||||
def test_enum():
|
def test_enum():
|
||||||
|
@ -35,3 +36,14 @@ def test_enum_values():
|
||||||
assert RGB.RED == 0
|
assert RGB.RED == 0
|
||||||
assert RGB.GREEN == 1
|
assert RGB.GREEN == 1
|
||||||
assert RGB.BLUE == 2
|
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():
|
def test_to_const():
|
||||||
assert to_const('snakes on a plane') == 'SNAKES_ON_A_PLANE'
|
assert to_const('snakes $1. on a "#plane') == 'SNAKES_ON_A_PLANE'
|
||||||
assert to_const('weirdñáunicode$# word') == 'WEIRD_UNICODE_WORD'
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user