mirror of
https://github.com/graphql-python/graphene.git
synced 2024-11-22 17:46:57 +03:00
extended support for subclassing with meta to Enum and InputObjectType
This commit is contained in:
parent
5036d164b7
commit
a16c5ba00b
|
@ -2,8 +2,11 @@
|
||||||
# Adapted for Graphene 2.0
|
# Adapted for Graphene 2.0
|
||||||
|
|
||||||
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
|
from graphene.types.objecttype import ObjectType, ObjectTypeOptions
|
||||||
|
from graphene.types.inputobjecttype import InputObjectType, InputObjectTypeOptions
|
||||||
|
from graphene.types.enum import Enum, EnumOptions
|
||||||
|
|
||||||
|
|
||||||
|
# ObjectType
|
||||||
class SpecialOptions(ObjectTypeOptions):
|
class SpecialOptions(ObjectTypeOptions):
|
||||||
other_attr = None
|
other_attr = None
|
||||||
|
|
||||||
|
@ -40,3 +43,77 @@ def test_special_objecttype_inherit_meta_options():
|
||||||
assert MyType._meta.name == 'MyType'
|
assert MyType._meta.name == 'MyType'
|
||||||
assert MyType._meta.default_resolver is None
|
assert MyType._meta.default_resolver is None
|
||||||
assert MyType._meta.interfaces == ()
|
assert MyType._meta.interfaces == ()
|
||||||
|
|
||||||
|
|
||||||
|
# InputObjectType
|
||||||
|
class SpecialInputObjectTypeOptions(ObjectTypeOptions):
|
||||||
|
other_attr = None
|
||||||
|
|
||||||
|
|
||||||
|
class SpecialInputObjectType(InputObjectType):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __init_subclass_with_meta__(cls, other_attr='default', **options):
|
||||||
|
_meta = SpecialInputObjectTypeOptions(cls)
|
||||||
|
_meta.other_attr = other_attr
|
||||||
|
super(SpecialInputObjectType, cls).__init_subclass_with_meta__(_meta=_meta, **options)
|
||||||
|
|
||||||
|
|
||||||
|
def test_special_inputobjecttype_could_be_subclassed():
|
||||||
|
class MyInputObjectType(SpecialInputObjectType):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
other_attr = 'yeah!'
|
||||||
|
|
||||||
|
assert MyInputObjectType._meta.other_attr == 'yeah!'
|
||||||
|
|
||||||
|
|
||||||
|
def test_special_inputobjecttype_could_be_subclassed_default():
|
||||||
|
class MyInputObjectType(SpecialInputObjectType):
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert MyInputObjectType._meta.other_attr == 'default'
|
||||||
|
|
||||||
|
|
||||||
|
def test_special_inputobjecttype_inherit_meta_options():
|
||||||
|
class MyInputObjectType(SpecialInputObjectType):
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert MyInputObjectType._meta.name == 'MyInputObjectType'
|
||||||
|
|
||||||
|
|
||||||
|
# Enum
|
||||||
|
class SpecialEnumOptions(EnumOptions):
|
||||||
|
other_attr = None
|
||||||
|
|
||||||
|
|
||||||
|
class SpecialEnum(Enum):
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def __init_subclass_with_meta__(cls, other_attr='default', **options):
|
||||||
|
_meta = SpecialEnumOptions(cls)
|
||||||
|
_meta.other_attr = other_attr
|
||||||
|
super(SpecialEnum, cls).__init_subclass_with_meta__(_meta=_meta, **options)
|
||||||
|
|
||||||
|
|
||||||
|
def test_special_enum_could_be_subclassed():
|
||||||
|
class MyEnum(SpecialEnum):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
other_attr = 'yeah!'
|
||||||
|
|
||||||
|
assert MyEnum._meta.other_attr == 'yeah!'
|
||||||
|
|
||||||
|
|
||||||
|
def test_special_enum_could_be_subclassed_default():
|
||||||
|
class MyEnum(SpecialEnum):
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert MyEnum._meta.other_attr == 'default'
|
||||||
|
|
||||||
|
|
||||||
|
def test_special_enum_inherit_meta_options():
|
||||||
|
class MyEnum(SpecialEnum):
|
||||||
|
pass
|
||||||
|
|
||||||
|
assert MyEnum._meta.name == 'MyEnum'
|
||||||
|
|
|
@ -60,8 +60,9 @@ class EnumMeta(SubclassWithMeta_Meta):
|
||||||
class Enum(six.with_metaclass(EnumMeta, UnmountedType, BaseType)):
|
class Enum(six.with_metaclass(EnumMeta, UnmountedType, BaseType)):
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_subclass_with_meta__(cls, enum=None, **options):
|
def __init_subclass_with_meta__(cls, enum=None, _meta=None, **options):
|
||||||
_meta = EnumOptions(cls)
|
if not _meta:
|
||||||
|
_meta = EnumOptions(cls)
|
||||||
_meta.enum = enum or cls.__enum__
|
_meta.enum = enum or cls.__enum__
|
||||||
_meta.deprecation_reason = options.pop('deprecation_reason', None)
|
_meta.deprecation_reason = options.pop('deprecation_reason', None)
|
||||||
for key, value in _meta.enum.__members__.items():
|
for key, value in _meta.enum.__members__.items():
|
||||||
|
|
|
@ -40,8 +40,9 @@ class InputObjectType(UnmountedType, BaseType):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def __init_subclass_with_meta__(cls, container=None, **options):
|
def __init_subclass_with_meta__(cls, container=None, _meta=None, **options):
|
||||||
_meta = InputObjectTypeOptions(cls)
|
if not _meta:
|
||||||
|
_meta = InputObjectTypeOptions(cls)
|
||||||
|
|
||||||
fields = OrderedDict()
|
fields = OrderedDict()
|
||||||
for base in reversed(cls.__mro__):
|
for base in reversed(cls.__mro__):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user