Skip to content

Add class property connection to SQLAlchemyObjectType #263

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
Feb 11, 2020
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/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def dynamic_type():
**field_kwargs
)
elif direction in (interfaces.ONETOMANY, interfaces.MANYTOMANY):
if _type._meta.connection:
if _type.connection:
# TODO Add a way to override connection_field_factory
return connection_field_factory(relationship_prop, registry, **field_kwargs)
return Field(
Expand Down
6 changes: 3 additions & 3 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def type(self):
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(
assert _type.connection, "The type {} doesn't have a connection".format(
_type.__name__
)
return _type._meta.connection
return _type.connection

@property
def model(self):
Expand Down Expand Up @@ -115,7 +115,7 @@ def get_resolver(self, parent_resolver):
def from_relationship(cls, relationship, registry, **field_kwargs):
model = relationship.mapper.entity
model_type = registry.get_type_for_model(model)
return cls(model_type._meta.connection, resolver=get_batch_resolver(relationship), **field_kwargs)
return cls(model_type.connection, resolver=get_batch_resolver(relationship), **field_kwargs)


def default_connection_field_factory(relationship, registry, **field_kwargs):
Expand Down
8 changes: 4 additions & 4 deletions graphene_sqlalchemy/tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def resolver(_obj, _info):
return Promise.resolve([])

result = UnsortedSQLAlchemyConnectionField.connection_resolver(
resolver, Pet._meta.connection, Pet, None, None
resolver, Pet.connection, Pet, None, None
)
assert isinstance(result, Promise)

Expand All @@ -51,18 +51,18 @@ def test_type_assert_object_has_connection():


def test_sort_added_by_default():
field = SQLAlchemyConnectionField(Pet._meta.connection)
field = SQLAlchemyConnectionField(Pet.connection)
assert "sort" in field.args
assert field.args["sort"] == Pet.sort_argument()


def test_sort_can_be_removed():
field = SQLAlchemyConnectionField(Pet._meta.connection, sort=None)
field = SQLAlchemyConnectionField(Pet.connection, sort=None)
assert "sort" not in field.args


def test_custom_sort():
field = SQLAlchemyConnectionField(Pet._meta.connection, sort=Editor.sort_argument())
field = SQLAlchemyConnectionField(Pet.connection, sort=Editor.sort_argument())
assert field.args["sort"] == Editor.sort_argument()


Expand Down
10 changes: 10 additions & 0 deletions graphene_sqlalchemy/tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from graphene import (Dynamic, Field, GlobalID, Int, List, Node, NonNull,
ObjectType, Schema, String)
from graphene.relay import Connection

from ..converter import convert_sqlalchemy_composite
from ..fields import (SQLAlchemyConnectionField,
Expand Down Expand Up @@ -46,6 +47,15 @@ class Meta:
assert reporter == reporter_node


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

assert issubclass(ReporterType.connection, Connection)


def test_sqlalchemy_default_fields():
@convert_sqlalchemy_composite.register(CompositeFullName)
def convert_composite_class(composite, registry):
Expand Down
2 changes: 2 additions & 0 deletions graphene_sqlalchemy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ def __init_subclass_with_meta__(
_meta.connection = connection
_meta.id = id or "id"

cls.connection = connection # Public way to get the connection

super(SQLAlchemyObjectType, cls).__init_subclass_with_meta__(
_meta=_meta, interfaces=interfaces, **options
)
Expand Down