Added NonNull, List definitions to ClassTypes

This commit is contained in:
Syrus Akbary 2015-12-02 20:28:53 -08:00
parent 3363f588fe
commit afc5e2720b
2 changed files with 27 additions and 1 deletions

View File

@ -43,10 +43,19 @@ class ClassTypeMeta(type):
# Add all attributes to the class. # Add all attributes to the class.
for obj_name, obj in attrs.items(): for obj_name, obj in attrs.items():
cls.add_to_class(obj_name, obj) 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 return cls
class ClassType(six.with_metaclass(ClassTypeMeta)): class ClassType(six.with_metaclass(ClassTypeMeta)):
class Meta:
abstract = True
@classmethod @classmethod
def internal_type(cls, schema): def internal_type(cls, schema):
raise NotImplementedError("Function internal_type not implemented in type {}".format(cls)) 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 FieldsClassType(six.with_metaclass(FieldsClassTypeMeta, ClassType)):
class Meta:
abstract = True
@classmethod @classmethod
def fields_internal_types(cls, schema): def fields_internal_types(cls, schema):
fields = [] fields = []

View File

@ -1,5 +1,5 @@
from ..base import ClassType, FieldsClassType from ..base import ClassType, FieldsClassType
from ...types import Field, String from ...types import Field, String, List, NonNull
from ...schema import Schema from ...schema import Schema
@ -20,6 +20,20 @@ def test_classtype_advanced():
assert Character._meta.description == 'OtherCharacter description' 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(): def test_fieldsclasstype():
f = Field(String()) f = Field(String())