From 06d86b05ed39ff1d5d3e3aef88df8d939e3bb1e8 Mon Sep 17 00:00:00 2001 From: Wichert Akkerman Date: Fri, 13 Jul 2018 09:17:48 +0200 Subject: [PATCH 1/2] Add support for using custom connections This builds on graphql-python/graphene-sqlalchemy#36, and adds support for specifying a custom connection class to SQLAlchemyObjectType. The implementation is mostly a copy & paste from [the identical feature in DjangoObjectType](https://github.com/graphql-python/graphene-django/blob/master/graphene_django/types.py#L72). --- graphene_sqlalchemy/fields.py | 10 ++++++++++ graphene_sqlalchemy/types.py | 9 +++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/graphene_sqlalchemy/fields.py b/graphene_sqlalchemy/fields.py index 4a283ad8..5f5f01c2 100644 --- a/graphene_sqlalchemy/fields.py +++ b/graphene_sqlalchemy/fields.py @@ -11,6 +11,16 @@ 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 diff --git a/graphene_sqlalchemy/types.py b/graphene_sqlalchemy/types.py index 69bf310c..5d17747d 100644 --- a/graphene_sqlalchemy/types.py +++ b/graphene_sqlalchemy/types.py @@ -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 "{}".' @@ -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), ( From 79eb7ceed21c65d34e0674fc86d5a68d774e634b Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Fri, 13 Jul 2018 10:03:15 -0700 Subject: [PATCH 2/2] Fixed lint issues --- graphene_sqlalchemy/fields.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/graphene_sqlalchemy/fields.py b/graphene_sqlalchemy/fields.py index 5f5f01c2..bf5ed3a5 100644 --- a/graphene_sqlalchemy/fields.py +++ b/graphene_sqlalchemy/fields.py @@ -17,7 +17,9 @@ def type(self): _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 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