Added NonNull and List to Types

This commit is contained in:
Syrus Akbary 2015-11-11 22:35:03 -08:00
parent bf168e7b12
commit 1956c1fb03
4 changed files with 49 additions and 0 deletions

View File

@ -82,6 +82,16 @@ class MirroredType(OrderedType):
self.args = args self.args = args
self.kwargs = kwargs self.kwargs = kwargs
@property
def List(self): # noqa
from .definitions import List
return List(self, *self.args, **self.kwargs)
@property
def NonNull(self): # noqa
from .definitions import NonNull
return NonNull(self, *self.args, **self.kwargs)
class ArgumentType(MirroredType): class ArgumentType(MirroredType):

View File

@ -10,6 +10,7 @@ from graphene.core.exceptions import SkipField
from graphene.core.options import Options from graphene.core.options import Options
from graphene.core.types.argument import ArgumentsGroup from graphene.core.types.argument import ArgumentsGroup
from graphene.core.types.base import BaseType from graphene.core.types.base import BaseType
from graphene.core.types.definitions import List, NonNull
from graphql.core.type import (GraphQLArgument, GraphQLInputObjectType, from graphql.core.type import (GraphQLArgument, GraphQLInputObjectType,
GraphQLInterfaceType, GraphQLObjectType) GraphQLInterfaceType, GraphQLObjectType)
@ -99,6 +100,9 @@ class ObjectTypeMeta(type):
new_class._meta.interfaces.append(base) new_class._meta.interfaces.append(base)
# new_class._meta.parents.extend(base._meta.parents) # new_class._meta.parents.extend(base._meta.parents)
setattr(new_class, 'NonNull', NonNull(new_class))
setattr(new_class, 'List', List(new_class))
new_class._prepare() new_class._prepare()
return new_class return new_class

View File

@ -5,6 +5,7 @@ from graphene.core.types import InputObjectType, ObjectType
from ..argument import Argument from ..argument import Argument
from ..base import MountedType, OrderedType from ..base import MountedType, OrderedType
from ..field import Field, InputField from ..field import Field, InputField
from ..definitions import List, NonNull
def test_orderedtype_equal(): def test_orderedtype_equal():
@ -71,3 +72,23 @@ def test_type_as_argument():
a = MountedType(description='A') a = MountedType(description='A')
argument = a.as_argument() argument = a.as_argument()
assert isinstance(argument, Argument) assert isinstance(argument, Argument)
def test_type_as_list():
m = MountedType(2, 3, my_c='A')
a = m.List
assert isinstance(a, List)
assert a.of_type == m
assert a.args == (2, 3)
assert a.kwargs == {'my_c': 'A'}
def test_type_as_nonnull():
m = MountedType(2, 3, my_c='A')
a = m.NonNull
assert isinstance(a, NonNull)
assert a.of_type == m
assert a.args == (2, 3)
assert a.kwargs == {'my_c': 'A'}

View File

@ -112,3 +112,17 @@ def test_field_mantain_resolver_tags():
field = schema.T(Droid._meta.fields_map['name']) field = schema.T(Droid._meta.fields_map['name'])
assert resolver_has_tag(field.resolver, 'test') assert resolver_has_tag(field.resolver, 'test')
def test_type_has_nonnull():
class Droid(Character):
name = String()
assert Droid.NonNull.of_type == Droid
def test_type_has_list():
class Droid(Character):
name = String()
assert Droid.List.of_type == Droid