16
16
from .definitions import List , NonNull
17
17
18
18
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
+
19
26
class ObjectTypeMeta (type ):
20
27
options_cls = Options
21
28
@@ -39,19 +46,19 @@ def __new__(cls, name, bases, attrs):
39
46
'__doc__' : doc
40
47
})
41
48
attr_meta = attrs .pop ('Meta' , None )
49
+ abstract = getattr (attr_meta , 'abstract' , False )
42
50
if not attr_meta :
43
- meta = None
44
- # meta = getattr(new_class, 'Meta', None)
51
+ meta = getattr (new_class , 'Meta' , None )
45
52
else :
46
53
meta = attr_meta
47
54
48
- getattr (new_class , '_meta' , None )
55
+ base_meta = getattr (new_class , '_meta' , None )
49
56
50
57
new_class .add_to_class ('_meta' , new_class .options_cls (meta ))
51
58
52
59
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 )
55
62
56
63
new_class ._meta .is_union = len (union_types ) > 1
57
64
new_class ._meta .types = union_types
@@ -66,6 +73,10 @@ def __new__(cls, name, bases, attrs):
66
73
for obj_name , obj in attrs .items ():
67
74
new_class .add_to_class (obj_name , obj )
68
75
76
+ if abstract :
77
+ new_class ._prepare ()
78
+ return new_class
79
+
69
80
if new_class ._meta .is_mutation :
70
81
assert hasattr (
71
82
new_class , 'mutate' ), "All mutations must implement mutate method"
@@ -138,8 +149,10 @@ class BaseObjectType(BaseType):
138
149
def __new__ (cls , * args , ** kwargs ):
139
150
if cls ._meta .is_interface :
140
151
raise Exception ("An interface cannot be initialized" )
141
- if cls ._meta .is_union :
152
+ elif cls ._meta .is_union :
142
153
raise Exception ("An union cannot be initialized" )
154
+ elif cls ._meta .abstract :
155
+ raise Exception ("An abstract ObjectType cannot be initialized" )
143
156
return super (BaseObjectType , cls ).__new__ (cls )
144
157
145
158
def __init__ (self , * args , ** kwargs ):
@@ -187,6 +200,9 @@ def _resolve_type(cls, schema, instance, *args):
187
200
188
201
@classmethod
189
202
def internal_type (cls , schema ):
203
+ if cls ._meta .abstract :
204
+ raise Exception ("Abstract ObjectTypes don't have a specific type." )
205
+
190
206
if cls ._meta .is_interface :
191
207
return GraphQLInterfaceType (
192
208
cls ._meta .type_name ,
0 commit comments