Skip to content

Commit 25c452c

Browse files
authored
Merge pull request graphql-python#95 from nmvalera/feature/custom-objecttype-options
Add ability to create SQLAlchemyObjectType with custom SQLAlchemyObjectTypeOptions
2 parents 6e4b721 + 1840814 commit 25c452c

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

graphene_sqlalchemy/tests/test_types.py

+44-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
1+
from collections import OrderedDict
22
from graphene import Field, Int, Interface, ObjectType
33
from graphene.relay import Node, is_node
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,45 @@ 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+
custom_fields = None
125+
126+
127+
class SQLAlchemyObjectTypeWithCustomOptions(SQLAlchemyObjectType):
128+
class Meta:
129+
abstract = True
130+
131+
@classmethod
132+
def __init_subclass_with_meta__(cls, custom_option=None, custom_fields=None, **options):
133+
_meta = CustomOptions(cls)
134+
_meta.custom_option = custom_option
135+
_meta.fields = custom_fields
136+
super(SQLAlchemyObjectTypeWithCustomOptions, cls).__init_subclass_with_meta__(_meta=_meta, **options)
137+
138+
139+
class ReporterWithCustomOptions(SQLAlchemyObjectTypeWithCustomOptions):
140+
class Meta:
141+
model = Reporter
142+
custom_option = 'custom_option'
143+
custom_fields = OrderedDict([('custom_field', Field(Int()))])
144+
145+
146+
def test_objecttype_with_custom_options():
147+
assert issubclass(ReporterWithCustomOptions, ObjectType)
148+
assert ReporterWithCustomOptions._meta.model == Reporter
149+
assert list(
150+
ReporterWithCustomOptions._meta.fields.keys()) == [
151+
'custom_field',
152+
'id',
153+
'first_name',
154+
'last_name',
155+
'email',
156+
'pets',
157+
'articles',
158+
'favorite_article']
159+
assert ReporterWithCustomOptions._meta.custom_option == 'custom_option'
160+
assert isinstance(ReporterWithCustomOptions._meta.fields['custom_field'].type, Int)

graphene_sqlalchemy/types.py

+10-3
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)