Skip to content

Add support for using custom connections #143

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

Merged
merged 2 commits into from Jul 14, 2018
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
12 changes: 12 additions & 0 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@

class UnsortedSQLAlchemyConnectionField(ConnectionField):

@property
def type(self):
from .types import SQLAlchemyObjectType
_type = super(ConnectionField, self).type
if issubclass(_type, Connection):
return _type
assert issubclass(_type, SQLAlchemyObjectType), (
"SQLALchemyConnectionField only accepts SQLAlchemyObjectType types, not {}"
).format(_type.__name__)
assert _type._meta.connection, "The type {} doesn't have a connection".format(_type.__name__)
return _type._meta.connection

@property
def model(self):
return self.type._meta.node._meta.model
Expand Down
9 changes: 7 additions & 2 deletions graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ 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, _meta=None, **options):
connection_class=None, 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 All @@ -114,7 +115,11 @@ def __init_subclass_with_meta__(cls, model=None, registry=None, skip_registry=Fa

if use_connection and not connection:
# We create the connection automatically
connection = Connection.create_type('{}Connection'.format(cls.__name__), node=cls)
if not connection_class:
connection_class = Connection

connection = connection_class.create_type(
'{}Connection'.format(cls.__name__), node=cls)

if connection is not None:
assert issubclass(connection, Connection), (
Expand Down