Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to create SQLAlchemyObjectType with custom SQLAlchemyObjectTypeOptions #95

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 44 additions & 2 deletions graphene_sqlalchemy/tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

from collections import OrderedDict
from graphene import Field, Int, Interface, ObjectType
from graphene.relay import Node, is_node
import six

from ..registry import Registry
from ..types import SQLAlchemyObjectType
from ..types import SQLAlchemyObjectType, SQLAlchemyObjectTypeOptions
from .models import Article, Reporter

registry = Registry()
Expand Down Expand Up @@ -116,3 +116,45 @@ def test_custom_objecttype_registered():
'pets',
'articles',
'favorite_article']


# Test Custom SQLAlchemyObjectType with Custom Options
class CustomOptions(SQLAlchemyObjectTypeOptions):
custom_option = None
custom_fields = None


class SQLAlchemyObjectTypeWithCustomOptions(SQLAlchemyObjectType):
class Meta:
abstract = True

@classmethod
def __init_subclass_with_meta__(cls, custom_option=None, custom_fields=None, **options):
_meta = CustomOptions(cls)
_meta.custom_option = custom_option
_meta.fields = custom_fields
super(SQLAlchemyObjectTypeWithCustomOptions, cls).__init_subclass_with_meta__(_meta=_meta, **options)


class ReporterWithCustomOptions(SQLAlchemyObjectTypeWithCustomOptions):
class Meta:
model = Reporter
custom_option = 'custom_option'
custom_fields = OrderedDict([('custom_field', Field(Int()))])


def test_objecttype_with_custom_options():
assert issubclass(ReporterWithCustomOptions, ObjectType)
assert ReporterWithCustomOptions._meta.model == Reporter
assert list(
ReporterWithCustomOptions._meta.fields.keys()) == [
'custom_field',
'id',
'first_name',
'last_name',
'email',
'pets',
'articles',
'favorite_article']
assert ReporterWithCustomOptions._meta.custom_option == 'custom_option'
assert isinstance(ReporterWithCustomOptions._meta.fields['custom_field'].type, Int)
13 changes: 10 additions & 3 deletions graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class SQLAlchemyObjectType(ObjectType):
@classmethod
def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=False,
only_fields=(), exclude_fields=(), connection=None,
use_connection=None, interfaces=(), id=None, **options):
use_connection=None, interfaces=(), id=None, _meta=None, **options):
assert is_mapped_class(model), (
'You need to pass a valid SQLAlchemy Model in '
'{}.Meta, received "{}".'
Expand Down Expand Up @@ -121,10 +121,17 @@ def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=Fa
"The connection must be a Connection. Received {}"
).format(connection.__name__)

_meta = SQLAlchemyObjectTypeOptions(cls)
if not _meta:
_meta = SQLAlchemyObjectTypeOptions(cls)

_meta.model = model
_meta.registry = registry
_meta.fields = sqla_fields

if _meta.fields:
_meta.fields.update(sqla_fields)
else:
_meta.fields = sqla_fields

_meta.connection = connection
_meta.id = id or 'id'

Expand Down