Skip to content

Commit a830eb3

Browse files
author
Nicolas Maurice
committed
add possibility to create object types that inherit from SQLAlchemyObjectType with custom ObjectTypeOptions
1 parent 4827ce2 commit a830eb3

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

graphene_sqlalchemy/tests/test_types.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import six
55

66
from ..registry import Registry
7-
from ..types import SQLAlchemyObjectType
7+
from ..types import SQLAlchemyObjectType, SQLAlchemyObjectTypeOptions
88
from .models import Article, Reporter
99

1010
registry = Registry()
@@ -116,3 +116,40 @@ def test_custom_objecttype_registered():
116116
'pets',
117117
'articles',
118118
'favorite_article']
119+
120+
121+
# Test Custom SQLAlchemyObjectType with Custom Options
122+
class CustomOptions(SQLAlchemyObjectTypeOptions):
123+
custom_option = None
124+
125+
126+
class SQLAlchemyObjectTypeWithCustomOptions(SQLAlchemyObjectType):
127+
class Meta:
128+
abstract = True
129+
130+
@classmethod
131+
def __init_subclass_with_meta__(cls, custom_option=None, **options):
132+
_meta = CustomOptions(cls)
133+
_meta.custom_option = custom_option
134+
super().__init_subclass_with_meta__(_meta=_meta, **options)
135+
136+
137+
class ReporterWithCustomOptions(SQLAlchemyObjectTypeWithCustomOptions):
138+
class Meta:
139+
model = Reporter
140+
custom_option = 'custom_option'
141+
142+
143+
def test_objecttype_with_custom_options():
144+
assert issubclass(ReporterWithCustomOptions, ObjectType)
145+
assert ReporterWithCustomOptions._meta.model == Reporter
146+
assert list(
147+
ReporterWithCustomOptions._meta.fields.keys()) == [
148+
'id',
149+
'first_name',
150+
'last_name',
151+
'email',
152+
'pets',
153+
'articles',
154+
'favorite_article']
155+
assert ReporterWithCustomOptions._meta.custom_option == 'custom_option'

graphene_sqlalchemy/types.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class SQLAlchemyObjectType(ObjectType):
9090
@classmethod
9191
def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False,
9292
only_fields=(), exclude_fields=(), connection=None,
93-
use_connection=None, interfaces=(), id=None, **options):
93+
use_connection=None, interfaces=(), id=None, _meta=None, **options):
9494
assert is_mapped_class(model), (
9595
'You need to pass a valid SQLAlchemy Model in '
9696
'{}.Meta, received "{}".'
@@ -121,10 +121,17 @@ def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=Fa
121121
"The connection must be a Connection. Received {}"
122122
).format(connection.__name__)
123123

124-
_meta = SQLAlchemyObjectTypeOptions(cls)
124+
if not _meta:
125+
_meta = SQLAlchemyObjectTypeOptions(cls)
126+
125127
_meta.model = model
126128
_meta.registry = registry
127-
_meta.fields = sqla_fields
129+
130+
if _meta.fields:
131+
_meta.fields.update(sqla_fields)
132+
else:
133+
_meta.fields = sqla_fields
134+
128135
_meta.connection = connection
129136
_meta.id = id or 'id'
130137

0 commit comments

Comments
 (0)