Skip to content

Fix createConnectionField deprecation warnings #229

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 3 commits into from
Jun 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion graphene_sqlalchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from .fields import SQLAlchemyConnectionField
from .utils import get_query, get_session

__version__ = "2.2.0"
__version__ = "2.2.1"

__all__ = [
"__version__",
Expand Down
21 changes: 11 additions & 10 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
import warnings
from functools import partial

from promise import Promise, is_thenable
Expand All @@ -10,8 +10,6 @@

from .utils import get_query

log = logging.getLogger()


class UnsortedSQLAlchemyConnectionField(ConnectionField):
@property
Expand Down Expand Up @@ -100,34 +98,37 @@ def __init__(self, type, *args, **kwargs):
def default_connection_field_factory(relationship, registry, **field_kwargs):
model = relationship.mapper.entity
model_type = registry.get_type_for_model(model)
return createConnectionField(model_type, **field_kwargs)
return __connectionFactory(model_type, **field_kwargs)


# TODO Remove in next major version
__connectionFactory = UnsortedSQLAlchemyConnectionField


def createConnectionField(_type, **field_kwargs):
log.warning(
warnings.warn(
'createConnectionField is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
return __connectionFactory(_type, **field_kwargs)


def registerConnectionFieldFactory(factoryMethod):
log.warning(
warnings.warn(
'registerConnectionFieldFactory is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
global __connectionFactory
__connectionFactory = factoryMethod


def unregisterConnectionFieldFactory():
log.warning(
warnings.warn(
'registerConnectionFieldFactory is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.'
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
DeprecationWarning,
)
global __connectionFactory
__connectionFactory = UnsortedSQLAlchemyConnectionField
44 changes: 23 additions & 21 deletions graphene_sqlalchemy/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,33 +364,35 @@ class Meta:


def test_deprecated_registerConnectionFieldFactory():
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
with pytest.warns(DeprecationWarning):
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)

class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)

class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)
class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)

assert isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
assert isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)


def test_deprecated_unregisterConnectionFieldFactory():
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
unregisterConnectionFieldFactory()
with pytest.warns(DeprecationWarning):
registerConnectionFieldFactory(_TestSQLAlchemyConnectionField)
unregisterConnectionFieldFactory()

class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter
interfaces = (Node,)

class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)
class ArticleType(SQLAlchemyObjectType):
class Meta:
model = Article
interfaces = (Node,)

assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)