7
7
8
8
from graphene import Field
9
9
from graphene .relay import Connection , Node
10
+ from graphene .types .base import BaseType
11
+ from graphene .types .interface import Interface , InterfaceOptions
10
12
from graphene .types .objecttype import ObjectType , ObjectTypeOptions
11
13
from graphene .types .utils import yank_fields_from_attrs
12
14
from graphene .utils .orderedtype import OrderedType
@@ -211,14 +213,7 @@ def construct_fields(
211
213
return fields
212
214
213
215
214
- class SQLAlchemyObjectTypeOptions (ObjectTypeOptions ):
215
- model = None # type: sqlalchemy.Model
216
- registry = None # type: sqlalchemy.Registry
217
- connection = None # type: sqlalchemy.Type[sqlalchemy.Connection]
218
- id = None # type: str
219
-
220
-
221
- class SQLAlchemyObjectType (ObjectType ):
216
+ class SQLAlchemyBase (BaseType ):
222
217
@classmethod
223
218
def __init_subclass_with_meta__ (
224
219
cls ,
@@ -237,6 +232,11 @@ def __init_subclass_with_meta__(
237
232
_meta = None ,
238
233
** options
239
234
):
235
+ # We always want to bypass this hook unless we're defining a concrete
236
+ # `SQLAlchemyObjectType` or `SQLAlchemyInterface`.
237
+ if not _meta :
238
+ return
239
+
240
240
# Make sure model is a valid SQLAlchemy model
241
241
if not is_mapped_class (model ):
242
242
raise ValueError (
@@ -290,9 +290,6 @@ def __init_subclass_with_meta__(
290
290
"The connection must be a Connection. Received {}"
291
291
).format (connection .__name__ )
292
292
293
- if not _meta :
294
- _meta = SQLAlchemyObjectTypeOptions (cls )
295
-
296
293
_meta .model = model
297
294
_meta .registry = registry
298
295
@@ -306,7 +303,7 @@ def __init_subclass_with_meta__(
306
303
307
304
cls .connection = connection # Public way to get the connection
308
305
309
- super (SQLAlchemyObjectType , cls ).__init_subclass_with_meta__ (
306
+ super (SQLAlchemyBase , cls ).__init_subclass_with_meta__ (
310
307
_meta = _meta , interfaces = interfaces , ** options
311
308
)
312
309
@@ -345,3 +342,39 @@ def enum_for_field(cls, field_name):
345
342
sort_enum = classmethod (sort_enum_for_object_type )
346
343
347
344
sort_argument = classmethod (sort_argument_for_object_type )
345
+
346
+
347
+ class SQLAlchemyObjectTypeOptions (ObjectTypeOptions ):
348
+ model = None # type: sqlalchemy.Model
349
+ registry = None # type: sqlalchemy.Registry
350
+ connection = None # type: sqlalchemy.Type[sqlalchemy.Connection]
351
+ id = None # type: str
352
+
353
+
354
+ class SQLAlchemyObjectType (SQLAlchemyBase , ObjectType ):
355
+ @classmethod
356
+ def __init_subclass_with_meta__ (cls , _meta = None , ** options ):
357
+ if not _meta :
358
+ _meta = SQLAlchemyObjectTypeOptions (cls )
359
+
360
+ super (SQLAlchemyObjectType , cls ).__init_subclass_with_meta__ (
361
+ _meta = _meta , ** options
362
+ )
363
+
364
+
365
+ class SQLAlchemyInterfaceOptions (InterfaceOptions ):
366
+ model = None # type: sqlalchemy.Model
367
+ registry = None # type: sqlalchemy.Registry
368
+ connection = None # type: sqlalchemy.Type[sqlalchemy.Connection]
369
+ id = None # type: str
370
+
371
+
372
+ class SQLAlchemyInterface (SQLAlchemyBase , Interface ):
373
+ @classmethod
374
+ def __init_subclass_with_meta__ (cls , _meta = None , ** options ):
375
+ if not _meta :
376
+ _meta = SQLAlchemyInterfaceOptions (cls )
377
+
378
+ super (SQLAlchemyInterface , cls ).__init_subclass_with_meta__ (
379
+ _meta = _meta , ** options
380
+ )
0 commit comments