|
| 1 | +# https://github.com/graphql-python/graphene/issues/425 |
| 2 | +import six |
| 3 | + |
| 4 | +from graphene.utils.is_base_type import is_base_type |
| 5 | + |
| 6 | +from graphene.types.objecttype import ObjectTypeMeta, ObjectType |
| 7 | +from graphene.types.options import Options |
| 8 | + |
| 9 | +class SpecialObjectTypeMeta(ObjectTypeMeta): |
| 10 | + |
| 11 | + @staticmethod |
| 12 | + def __new__(cls, name, bases, attrs): |
| 13 | + # Also ensure initialization is only performed for subclasses of |
| 14 | + # DjangoObjectType |
| 15 | + if not is_base_type(bases, SpecialObjectTypeMeta): |
| 16 | + return type.__new__(cls, name, bases, attrs) |
| 17 | + |
| 18 | + options = Options( |
| 19 | + attrs.pop('Meta', None), |
| 20 | + other_attr='default', |
| 21 | + ) |
| 22 | + |
| 23 | + return ObjectTypeMeta.__new__(cls, name, bases, dict(attrs, _meta=options)) |
| 24 | + |
| 25 | + return cls |
| 26 | + |
| 27 | + |
| 28 | +class SpecialObjectType(six.with_metaclass(SpecialObjectTypeMeta, ObjectType)): |
| 29 | + pass |
| 30 | + |
| 31 | + |
| 32 | +def test_special_objecttype_could_be_subclassed(): |
| 33 | + class MyType(SpecialObjectType): |
| 34 | + class Meta: |
| 35 | + other_attr = 'yeah!' |
| 36 | + |
| 37 | + assert MyType._meta.other_attr == 'yeah!' |
| 38 | + |
| 39 | + |
| 40 | +def test_special_objecttype_could_be_subclassed_default(): |
| 41 | + class MyType(SpecialObjectType): |
| 42 | + pass |
| 43 | + |
| 44 | + assert MyType._meta.other_attr == 'default' |
| 45 | + |
| 46 | + |
| 47 | +def test_special_objecttype_inherit_meta_options(): |
| 48 | + class MyType(SpecialObjectType): |
| 49 | + pass |
| 50 | + |
| 51 | + assert MyType._meta.name == 'MyType' |
| 52 | + assert MyType._meta.default_resolver == None |
| 53 | + assert MyType._meta.interfaces == () |
0 commit comments