Skip to content

Commit d57d1e3

Browse files
committed
Added abstract ObjectType definition. Fixed #55
1 parent c617067 commit d57d1e3

File tree

3 files changed

+69
-8
lines changed

3 files changed

+69
-8
lines changed

graphene/core/options.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from ..utils import cached_property
44

55
DEFAULT_NAMES = ('description', 'name', 'is_interface', 'is_mutation',
6-
'type_name', 'interfaces')
6+
'type_name', 'interfaces', 'abstract')
77

88

99
class Options(object):
@@ -14,6 +14,7 @@ def __init__(self, meta=None):
1414
self.is_interface = False
1515
self.is_mutation = False
1616
self.is_union = False
17+
self.abstract = False
1718
self.interfaces = []
1819
self.parents = []
1920
self.types = []

graphene/core/types/objecttype.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
from .definitions import List, NonNull
1717

1818

19+
def is_objecttype(cls):
20+
if not issubclass(cls, BaseObjectType):
21+
return False
22+
_meta = getattr(cls, '_meta', None)
23+
return not(_meta and (_meta.abstract or _meta.is_interface))
24+
25+
1926
class ObjectTypeMeta(type):
2027
options_cls = Options
2128

@@ -39,19 +46,19 @@ def __new__(cls, name, bases, attrs):
3946
'__doc__': doc
4047
})
4148
attr_meta = attrs.pop('Meta', None)
49+
abstract = getattr(attr_meta, 'abstract', False)
4250
if not attr_meta:
43-
meta = None
44-
# meta = getattr(new_class, 'Meta', None)
51+
meta = getattr(new_class, 'Meta', None)
4552
else:
4653
meta = attr_meta
4754

48-
getattr(new_class, '_meta', None)
55+
base_meta = getattr(new_class, '_meta', None)
4956

5057
new_class.add_to_class('_meta', new_class.options_cls(meta))
5158

5259
new_class._meta.is_interface = new_class.is_interface(parents)
53-
new_class._meta.is_mutation = new_class.is_mutation(parents)
54-
union_types = [p for p in parents if issubclass(p, BaseObjectType)]
60+
new_class._meta.is_mutation = new_class.is_mutation(parents) or (base_meta and base_meta.is_mutation)
61+
union_types = filter(is_objecttype, parents)
5562

5663
new_class._meta.is_union = len(union_types) > 1
5764
new_class._meta.types = union_types
@@ -66,6 +73,10 @@ def __new__(cls, name, bases, attrs):
6673
for obj_name, obj in attrs.items():
6774
new_class.add_to_class(obj_name, obj)
6875

76+
if abstract:
77+
new_class._prepare()
78+
return new_class
79+
6980
if new_class._meta.is_mutation:
7081
assert hasattr(
7182
new_class, 'mutate'), "All mutations must implement mutate method"
@@ -138,8 +149,10 @@ class BaseObjectType(BaseType):
138149
def __new__(cls, *args, **kwargs):
139150
if cls._meta.is_interface:
140151
raise Exception("An interface cannot be initialized")
141-
if cls._meta.is_union:
152+
elif cls._meta.is_union:
142153
raise Exception("An union cannot be initialized")
154+
elif cls._meta.abstract:
155+
raise Exception("An abstract ObjectType cannot be initialized")
143156
return super(BaseObjectType, cls).__new__(cls)
144157

145158
def __init__(self, *args, **kwargs):
@@ -187,6 +200,9 @@ def _resolve_type(cls, schema, instance, *args):
187200

188201
@classmethod
189202
def internal_type(cls, schema):
203+
if cls._meta.abstract:
204+
raise Exception("Abstract ObjectTypes don't have a specific type.")
205+
190206
if cls._meta.is_interface:
191207
return GraphQLInterfaceType(
192208
cls._meta.type_name,

graphene/core/types/tests/test_objecttype.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from py.test import raises
66

77
from graphene.core.schema import Schema
8-
from graphene.core.types import Int, Interface, String
8+
from graphene.core.types import Int, Interface, ObjectType, String
99

1010

1111
class Character(Interface):
@@ -148,3 +148,47 @@ class Droid(Character):
148148
name = String()
149149

150150
assert Droid.List.of_type == Droid
151+
152+
153+
def test_abstracttype():
154+
class MyObject1(ObjectType):
155+
class Meta:
156+
abstract = True
157+
name1 = String()
158+
159+
class MyObject2(ObjectType):
160+
class Meta:
161+
abstract = True
162+
name2 = String()
163+
164+
class MyObject(MyObject1, MyObject2):
165+
pass
166+
167+
object_type = schema.T(MyObject)
168+
169+
assert MyObject._meta.fields_map.keys() == ['name1', 'name2']
170+
assert MyObject._meta.fields_map['name1'].object_type == MyObject
171+
assert MyObject._meta.fields_map['name2'].object_type == MyObject
172+
assert isinstance(object_type, GraphQLObjectType)
173+
174+
175+
def test_abstracttype_initialize():
176+
class MyAbstractObjectType(ObjectType):
177+
class Meta:
178+
abstract = True
179+
180+
with raises(Exception) as excinfo:
181+
MyAbstractObjectType()
182+
183+
assert 'An abstract ObjectType cannot be initialized' == str(excinfo.value)
184+
185+
186+
def test_abstracttype_type():
187+
class MyAbstractObjectType(ObjectType):
188+
class Meta:
189+
abstract = True
190+
191+
with raises(Exception) as excinfo:
192+
schema.T(MyAbstractObjectType)
193+
194+
assert 'Abstract ObjectTypes don\'t have a specific type.' == str(excinfo.value)

0 commit comments

Comments
 (0)