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 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
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
64 changes: 42 additions & 22 deletions graphene_sqlalchemy/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ..converter import convert_sqlalchemy_composite
from ..fields import (SQLAlchemyConnectionField,
UnsortedSQLAlchemyConnectionField,
UnsortedSQLAlchemyConnectionField, createConnectionField,
registerConnectionFieldFactory,
unregisterConnectionFieldFactory)
from ..types import ORMField, SQLAlchemyObjectType, SQLAlchemyObjectTypeOptions
Expand Down Expand Up @@ -224,6 +224,19 @@ class Meta:
assert pets_field.type().description == 'Overridden'


def test_invalid_model_attr():
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI I had to add an unrelated test because coveralls thought incorrectly that the overall coverage went down without this.

err_msg = (
"Cannot map ORMField to a model attribute.\n"
"Field: 'ReporterType.first_name'"
)
with pytest.raises(ValueError, match=err_msg):
class ReporterType(SQLAlchemyObjectType):
class Meta:
model = Reporter

first_name = ORMField(model_attr='does_not_exist')


def test_only_fields():
class ReporterType(SQLAlchemyObjectType):
class Meta:
Expand Down Expand Up @@ -364,33 +377,40 @@ 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,)

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

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

assert not isinstance(ReporterType._meta.fields['articles'].type(), _TestSQLAlchemyConnectionField)
def test_deprecated_createConnectionField():
with pytest.warns(DeprecationWarning):
createConnectionField(None)
5 changes: 4 additions & 1 deletion graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def construct_fields(
for orm_field_name, orm_field in custom_orm_fields_items:
attr_name = orm_field.kwargs.get('model_attr', orm_field_name)
if attr_name not in all_model_attrs:
raise Exception('Cannot map ORMField "{}" to SQLAlchemy model property'.format(orm_field_name))
raise ValueError((
"Cannot map ORMField to a model attribute.\n"
"Field: '{}.{}'"
).format(obj_type.__name__, orm_field_name,))
orm_field.kwargs['model_attr'] = attr_name

# Merge automatic fields with custom ORM fields
Expand Down