Skip to content

Commit 1956c1f

Browse files
committed
Added NonNull and List to Types
1 parent bf168e7 commit 1956c1f

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

graphene/core/types/base.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ def __init__(self, *args, **kwargs):
8282
self.args = args
8383
self.kwargs = kwargs
8484

85+
@property
86+
def List(self): # noqa
87+
from .definitions import List
88+
return List(self, *self.args, **self.kwargs)
89+
90+
@property
91+
def NonNull(self): # noqa
92+
from .definitions import NonNull
93+
return NonNull(self, *self.args, **self.kwargs)
94+
8595

8696
class ArgumentType(MirroredType):
8797

graphene/core/types/objecttype.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from graphene.core.options import Options
1111
from graphene.core.types.argument import ArgumentsGroup
1212
from graphene.core.types.base import BaseType
13+
from graphene.core.types.definitions import List, NonNull
1314
from graphql.core.type import (GraphQLArgument, GraphQLInputObjectType,
1415
GraphQLInterfaceType, GraphQLObjectType)
1516

@@ -99,6 +100,9 @@ def __new__(cls, name, bases, attrs):
99100
new_class._meta.interfaces.append(base)
100101
# new_class._meta.parents.extend(base._meta.parents)
101102

103+
setattr(new_class, 'NonNull', NonNull(new_class))
104+
setattr(new_class, 'List', List(new_class))
105+
102106
new_class._prepare()
103107
return new_class
104108

graphene/core/types/tests/test_base.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ..argument import Argument
66
from ..base import MountedType, OrderedType
77
from ..field import Field, InputField
8+
from ..definitions import List, NonNull
89

910

1011
def test_orderedtype_equal():
@@ -71,3 +72,23 @@ def test_type_as_argument():
7172
a = MountedType(description='A')
7273
argument = a.as_argument()
7374
assert isinstance(argument, Argument)
75+
76+
77+
def test_type_as_list():
78+
m = MountedType(2, 3, my_c='A')
79+
a = m.List
80+
81+
assert isinstance(a, List)
82+
assert a.of_type == m
83+
assert a.args == (2, 3)
84+
assert a.kwargs == {'my_c': 'A'}
85+
86+
87+
def test_type_as_nonnull():
88+
m = MountedType(2, 3, my_c='A')
89+
a = m.NonNull
90+
91+
assert isinstance(a, NonNull)
92+
assert a.of_type == m
93+
assert a.args == (2, 3)
94+
assert a.kwargs == {'my_c': 'A'}

graphene/core/types/tests/test_objecttype.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,17 @@ def resolve_name(self, *args):
112112

113113
field = schema.T(Droid._meta.fields_map['name'])
114114
assert resolver_has_tag(field.resolver, 'test')
115+
116+
117+
def test_type_has_nonnull():
118+
class Droid(Character):
119+
name = String()
120+
121+
assert Droid.NonNull.of_type == Droid
122+
123+
124+
def test_type_has_list():
125+
class Droid(Character):
126+
name = String()
127+
128+
assert Droid.List.of_type == Droid

0 commit comments

Comments
 (0)