Skip to content

feat(async): add support for async sessions #350

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 21 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
32bf439
feat(async): add support for async sessions
May 16, 2022
41c88f9
fix(test batching): ensure that objects are added to database in asyn…
May 16, 2022
47d224e
test: only run batching tests with sync session
May 17, 2022
811cdf2
chore(fields): use get_query instead of manually crafting the query
May 31, 2022
3149830
fix: throw exceptions if Async Session is used with old sql alchemy
May 31, 2022
0e60c03
test: fix sqlalchemy 1.2 and 1.3 tests, fix batching tests by separat…
May 31, 2022
0180f69
fix: ensure that synchronous execute calls are still feasible
Jun 2, 2022
ec76697
refactor: remove duplicate code by fixing if condition
Jun 7, 2022
6a00846
chore: add specific error if awaitable is returned in synchronous exe…
Jun 7, 2022
9897a03
chore: merge master, resolve conflicts
Sep 15, 2022
fff782f
test: use pytest_asyncio.fixture instead normal fixture, fix issues i…
Sep 15, 2022
1250231
chore: remove duplicate eventually_await_session
Oct 7, 2022
eee2314
chore: remove duplicate skip statement
Oct 7, 2022
bacf15d
fix: fix benchmark tests not being executed properly
Oct 7, 2022
2bc6f84
chore: format files
Oct 7, 2022
a968ff8
chore: move is_graphene_version_less_than to top of file
Oct 7, 2022
1039f03
test: remove unnecessary pytest.mark.asyncio, auto-reformatting
Oct 7, 2022
e61df34
chore: revert faulty formatting
Oct 7, 2022
2ff54dc
fix: run startup checkt for sqlalchemy version
Oct 31, 2022
4321b04
chore: rebase onto master, adapt interface test to run with sync sess…
Dec 9, 2022
1e857e0
fix: allow polymorphism with async session
erikwrede Dec 9, 2022
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
@@ -1,5 +1,5 @@
from .types import SQLAlchemyObjectType
from .fields import SQLAlchemyConnectionField
from .types import SQLAlchemyObjectType
from .utils import get_query, get_session

__version__ = "3.0.0b1"
Expand Down
73 changes: 61 additions & 12 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from graphql_relay import connection_from_array_slice

from .batching import get_batch_resolver
from .utils import EnumValue, get_query
from .utils import (EnumValue, get_query, get_session,
is_sqlalchemy_version_less_than)

if not is_sqlalchemy_version_less_than("1.4"):
from sqlalchemy.ext.asyncio import AsyncSession


class UnsortedSQLAlchemyConnectionField(ConnectionField):
Expand All @@ -26,9 +30,7 @@ def type(self):
assert issubclass(nullable_type, SQLAlchemyObjectType), (
"SQLALchemyConnectionField only accepts SQLAlchemyObjectType types, not {}"
).format(nullable_type.__name__)
assert (
nullable_type.connection
), "The type {} doesn't have a connection".format(
assert nullable_type.connection, "The type {} doesn't have a connection".format(
nullable_type.__name__
)
assert type_ == nullable_type, (
Expand All @@ -47,8 +49,51 @@ def get_query(cls, model, info, **args):

@classmethod
def resolve_connection(cls, connection_type, model, info, args, resolved):
session = get_session(info.context)
if resolved is None:
if not is_sqlalchemy_version_less_than("1.4") and isinstance(
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we need to check this every request, it will just introduce inefficiencies in every request. Maybe a "startup check" or documentation does the job here.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe changing lines 16/17 so that AsyncSession has a defined value (None), if sqlalchemy_version < 1.4 will be more elegant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem here is the isinstance check. The import of it only happens if the version is nor less than 1.4. So in all other versions I need to fail the if statement before doing the isinstance check. I can imagine to ways around:

  • do the startup check as you described and store the result in a constant SQLALCHEMY_LESS_THAN_1_4, so I just need to check the boolean value of the constant
  • Invert this statement to check if it is a synchronous session

I would prefer the first option, as I am not sure how to check if a session is sync in sqlalchemy. There seems to be a protected class attribute _is_asyncio with sessions, but checking that would probably agin require checking the version of sqlalchemy first as I assume the property is new 😉

Copy link
Member

Choose a reason for hiding this comment

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

Startup check option sounds great!

Copy link
Member

Choose a reason for hiding this comment

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

We'll need this anyways as 2.0 is in beta; so we should use 1.4 style on all queries if possible - even in sync session. Scope of a different PR though. This will also require the global option.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Implemented the startup option 👍

session, AsyncSession
):

async def get_result():
return await cls.resolve_connection_async(
connection_type, model, info, args, resolved
)

return get_result()

else:
resolved = cls.get_query(model, info, **args)
if isinstance(resolved, Query):
_len = resolved.count()
else:
_len = len(resolved)

def adjusted_connection_adapter(edges, pageInfo):
return connection_adapter(connection_type, edges, pageInfo)

connection = connection_from_array_slice(
array_slice=resolved,
args=args,
slice_start=0,
array_length=_len,
array_slice_length=_len,
connection_type=adjusted_connection_adapter,
edge_type=connection_type.Edge,
page_info_type=page_info_adapter,
)
connection.iterable = resolved
connection.length = _len
return connection

@classmethod
async def resolve_connection_async(
cls, connection_type, model, info, args, resolved
):
session = get_session(info.context)
if resolved is None:
resolved = cls.get_query(model, info, **args)
query = cls.get_query(model, info, **args)
resolved = (await session.scalars(query)).all()
if isinstance(resolved, Query):
_len = resolved.count()
else:
Expand Down Expand Up @@ -148,7 +193,11 @@ def wrap_resolve(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.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 All @@ -163,17 +212,17 @@ def default_connection_field_factory(relationship, registry, **field_kwargs):

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


def registerConnectionFieldFactory(factoryMethod):
warnings.warn(
'registerConnectionFieldFactory is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
"registerConnectionFieldFactory is deprecated and will be removed in the next "
"major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.",
DeprecationWarning,
)
global __connectionFactory
Expand All @@ -182,8 +231,8 @@ def registerConnectionFieldFactory(factoryMethod):

def unregisterConnectionFieldFactory():
warnings.warn(
'registerConnectionFieldFactory is deprecated and will be removed in the next '
'major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.',
"registerConnectionFieldFactory is deprecated and will be removed in the next "
"major version. Use SQLAlchemyObjectType.Meta.connection_field_factory instead.",
DeprecationWarning,
)
global __connectionFactory
Expand Down
43 changes: 38 additions & 5 deletions graphene_sqlalchemy/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from sqlalchemy.orm import sessionmaker

import graphene
from graphene_sqlalchemy.utils import is_sqlalchemy_version_less_than

from ..converter import convert_sqlalchemy_composite
from ..registry import reset_global_registry
from .models import Base, CompositeFullName

test_db_url = 'sqlite://' # use in-memory database for tests
if not is_sqlalchemy_version_less_than("1.4"):
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine


@pytest.fixture(autouse=True)
Expand All @@ -22,13 +24,44 @@ def convert_composite_class(composite, registry):
return graphene.Field(graphene.Int)


@pytest.fixture(params=[False, True])
def async_session(request):
return request.param


@pytest.fixture
def test_db_url(async_session: bool):
if async_session:
return "sqlite+aiosqlite://"
else:
return "sqlite://"


@pytest.mark.asyncio
@pytest.fixture(scope="function")
def session_factory():
engine = create_engine(test_db_url)
Base.metadata.create_all(engine)
async def session_factory(async_session: bool, test_db_url: str):
if async_session:
if is_sqlalchemy_version_less_than("1.4"):
pytest.skip(f"Async Sessions only work in sql alchemy 1.4 and above")
engine = create_async_engine(test_db_url)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
yield sessionmaker(bind=engine, class_=AsyncSession, expire_on_commit=False)
await engine.dispose()
else:
engine = create_engine(test_db_url)
Base.metadata.create_all(engine)
yield sessionmaker(bind=engine, expire_on_commit=False)
# SQLite in-memory db is deleted when its connection is closed.
# https://www.sqlite.org/inmemorydb.html
engine.dispose()

yield sessionmaker(bind=engine)

@pytest.fixture(scope="function")
async def sync_session_factory():
engine = create_engine("sqlite://")
Base.metadata.create_all(engine)
yield sessionmaker(bind=engine, expire_on_commit=False)
# SQLite in-memory db is deleted when its connection is closed.
# https://www.sqlite.org/inmemorydb.html
engine.dispose()
Expand Down
38 changes: 26 additions & 12 deletions graphene_sqlalchemy/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@


class HairKind(enum.Enum):
LONG = 'long'
SHORT = 'short'
LONG = "long"
SHORT = "short"


Base = declarative_base()
Expand Down Expand Up @@ -64,9 +64,15 @@ class Reporter(Base):
last_name = Column(String(30), doc="Last name")
email = Column(String(), doc="Email")
favorite_pet_kind = Column(PetKind)
pets = relationship("Pet", secondary=association_table, backref="reporters", order_by="Pet.id")
articles = relationship("Article", backref="reporter")
favorite_article = relationship("Article", uselist=False)
pets = relationship(
"Pet",
secondary=association_table,
backref="reporters",
order_by="Pet.id",
lazy="selectin",
)
articles = relationship("Article", backref="reporter", lazy="selectin")
favorite_article = relationship("Article", uselist=False, lazy="selectin")

@hybrid_property
def hybrid_prop_with_doc(self):
Expand Down Expand Up @@ -101,7 +107,9 @@ def hybrid_prop_list(self) -> List[int]:
select([func.cast(func.count(id), Integer)]), doc="Column property"
)

composite_prop = composite(CompositeFullName, first_name, last_name, doc="Composite")
composite_prop = composite(
CompositeFullName, first_name, last_name, doc="Composite"
)


class Article(Base):
Expand Down Expand Up @@ -137,7 +145,7 @@ class ShoppingCartItem(Base):
id = Column(Integer(), primary_key=True)

@hybrid_property
def hybrid_prop_shopping_cart(self) -> List['ShoppingCart']:
def hybrid_prop_shopping_cart(self) -> List["ShoppingCart"]:
return [ShoppingCart(id=1)]


Expand Down Expand Up @@ -192,11 +200,17 @@ def hybrid_prop_list_date(self) -> List[datetime.date]:

@hybrid_property
def hybrid_prop_nested_list_int(self) -> List[List[int]]:
return [self.hybrid_prop_list_int, ]
return [
self.hybrid_prop_list_int,
]

@hybrid_property
def hybrid_prop_deeply_nested_list_int(self) -> List[List[List[int]]]:
return [[self.hybrid_prop_list_int, ], ]
return [
[
self.hybrid_prop_list_int,
],
]

# Other SQLAlchemy Instances
@hybrid_property
Expand All @@ -216,15 +230,15 @@ def hybrid_prop_unsupported_type_tuple(self) -> Tuple[str, str]:
# Self-references

@hybrid_property
def hybrid_prop_self_referential(self) -> 'ShoppingCart':
def hybrid_prop_self_referential(self) -> "ShoppingCart":
return ShoppingCart(id=1)

@hybrid_property
def hybrid_prop_self_referential_list(self) -> List['ShoppingCart']:
def hybrid_prop_self_referential_list(self) -> List["ShoppingCart"]:
return [ShoppingCart(id=1)]

# Optional[T]

@hybrid_property
def hybrid_prop_optional_self_referential(self) -> Optional['ShoppingCart']:
def hybrid_prop_optional_self_referential(self) -> Optional["ShoppingCart"]:
return None
64 changes: 64 additions & 0 deletions graphene_sqlalchemy/tests/models_batching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from __future__ import absolute_import

import enum

from sqlalchemy import (Column, Date, Enum, ForeignKey, Integer, String, Table,
func, select)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import column_property, relationship

PetKind = Enum("cat", "dog", name="pet_kind")


class HairKind(enum.Enum):
LONG = "long"
SHORT = "short"


Base = declarative_base()

association_table = Table(
"association",
Base.metadata,
Column("pet_id", Integer, ForeignKey("pets.id")),
Column("reporter_id", Integer, ForeignKey("reporters.id")),
)


class Pet(Base):
__tablename__ = "pets"
id = Column(Integer(), primary_key=True)
name = Column(String(30))
pet_kind = Column(PetKind, nullable=False)
hair_kind = Column(Enum(HairKind, name="hair_kind"), nullable=False)
reporter_id = Column(Integer(), ForeignKey("reporters.id"))


class Reporter(Base):
__tablename__ = "reporters"

id = Column(Integer(), primary_key=True)
first_name = Column(String(30), doc="First name")
last_name = Column(String(30), doc="Last name")
email = Column(String(), doc="Email")
favorite_pet_kind = Column(PetKind)
pets = relationship(
"Pet",
secondary=association_table,
backref="reporters",
order_by="Pet.id",
)
articles = relationship("Article", backref="reporter")
favorite_article = relationship("Article", uselist=False)

column_prop = column_property(
select([func.cast(func.count(id), Integer)]), doc="Column property"
)


class Article(Base):
__tablename__ = "articles"
id = Column(Integer(), primary_key=True)
headline = Column(String(100))
pub_date = Column(Date())
reporter_id = Column(Integer(), ForeignKey("reporters.id"))
Loading