From afc5e2720bb21bfc5ac365565b3c9d9a22345f7e Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Wed, 2 Dec 2015 20:28:53 -0800 Subject: [PATCH] Added NonNull, List definitions to ClassTypes --- graphene/core/classtypes/base.py | 12 ++++++++++++ graphene/core/classtypes/tests/test_base.py | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/graphene/core/classtypes/base.py b/graphene/core/classtypes/base.py index a662ec96..5e338ebe 100644 --- a/graphene/core/classtypes/base.py +++ b/graphene/core/classtypes/base.py @@ -43,10 +43,19 @@ class ClassTypeMeta(type): # Add all attributes to the class. for obj_name, obj in attrs.items(): cls.add_to_class(obj_name, obj) + + if not cls._meta.abstract: + from ..types import List, NonNull + setattr(cls, 'NonNull', NonNull(cls)) + setattr(cls, 'List', List(cls)) + return cls class ClassType(six.with_metaclass(ClassTypeMeta)): + class Meta: + abstract = True + @classmethod def internal_type(cls, schema): raise NotImplementedError("Function internal_type not implemented in type {}".format(cls)) @@ -74,6 +83,9 @@ class FieldsClassTypeMeta(ClassTypeMeta): class FieldsClassType(six.with_metaclass(FieldsClassTypeMeta, ClassType)): + class Meta: + abstract = True + @classmethod def fields_internal_types(cls, schema): fields = [] diff --git a/graphene/core/classtypes/tests/test_base.py b/graphene/core/classtypes/tests/test_base.py index 2830b9bb..2564a606 100644 --- a/graphene/core/classtypes/tests/test_base.py +++ b/graphene/core/classtypes/tests/test_base.py @@ -1,5 +1,5 @@ from ..base import ClassType, FieldsClassType -from ...types import Field, String +from ...types import Field, String, List, NonNull from ...schema import Schema @@ -20,6 +20,20 @@ def test_classtype_advanced(): assert Character._meta.description == 'OtherCharacter description' +def test_classtype_definition_list(): + class Character(ClassType): + '''Character description''' + pass + assert isinstance(Character.List, List) + + +def test_classtype_definition_nonnull(): + class Character(ClassType): + '''Character description''' + pass + assert isinstance(Character.NonNull, NonNull) + + def test_fieldsclasstype(): f = Field(String())