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 11 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
52 changes: 49 additions & 3 deletions graphene_sqlalchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
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 SQLAlchemyConnectionField(ConnectionField):
Expand Down Expand Up @@ -81,8 +84,51 @@ def get_query(cls, model, info, sort=None, **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 @@ -179,7 +225,7 @@ def from_relationship(cls, relationship, registry, **field_kwargs):
return cls(
model_type.connection,
resolver=get_batch_resolver(relationship),
**field_kwargs
**field_kwargs,
)


Expand Down
48 changes: 41 additions & 7 deletions graphene_sqlalchemy/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import pytest
import pytest_asyncio
from sqlalchemy import create_engine
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,18 +25,49 @@ def convert_composite_class(composite, registry):
return graphene.Field(graphene.Int)


@pytest.fixture(scope="function")
def session_factory():
engine = create_engine(test_db_url)
Base.metadata.create_all(engine)
@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://"

yield sessionmaker(bind=engine)

@pytest.mark.asyncio
@pytest_asyncio.fixture(scope="function")
async def session_factory(async_session: bool, test_db_url: str):
if async_session:
if is_sqlalchemy_version_less_than("1.4"):
pytest.skip("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()


@pytest_asyncio.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()


@pytest.fixture(scope="function")
@pytest_asyncio.fixture(scope="function")
def session(session_factory):
return session_factory()
10 changes: 7 additions & 3 deletions graphene_sqlalchemy/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ class Reporter(Base):
email = Column(String(), doc="Email")
favorite_pet_kind = Column(PetKind)
pets = relationship(
"Pet", secondary=association_table, backref="reporters", order_by="Pet.id"
"Pet",
secondary=association_table,
backref="reporters",
order_by="Pet.id",
lazy="selectin",
)
articles = relationship("Article", backref="reporter")
favorite_article = relationship("Article", uselist=False)
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
91 changes: 91 additions & 0 deletions graphene_sqlalchemy/tests/models_batching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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"))
readers = relationship(
"Reader", secondary="articles_readers", back_populates="articles"
)


class Reader(Base):
__tablename__ = "readers"
id = Column(Integer(), primary_key=True)
name = Column(String(100))
articles = relationship(
"Article", secondary="articles_readers", back_populates="readers"
)


class ArticleReader(Base):
__tablename__ = "articles_readers"
article_id = Column(Integer(), ForeignKey("articles.id"), primary_key=True)
reader_id = Column(Integer(), ForeignKey("readers.id"), primary_key=True)
Loading