strawberry-orm 0.24.0__tar.gz → 0.24.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/PKG-INFO +5 -1
  2. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/README.md +4 -0
  3. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/pyproject.toml +1 -1
  4. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/pyproject.toml.orig +1 -1
  5. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/backends/_base.py +4 -0
  6. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/backends/django.py +63 -10
  7. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/backends/protocol.py +13 -0
  8. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/backends/sqlalchemy.py +114 -14
  9. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/backends/tortoise.py +50 -4
  10. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/relay/connection.py +254 -35
  11. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/__init__.py +0 -0
  12. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/_async.py +0 -0
  13. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/backends/__init__.py +0 -0
  14. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/backends/filter_pk_shortcut.py +0 -0
  15. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/batching.py +0 -0
  16. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/core.py +0 -0
  17. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/fields.py +0 -0
  18. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/filters.py +0 -0
  19. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/lazy_resolution.py +0 -0
  20. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/mutations.py +0 -0
  21. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/optimizer/__init__.py +0 -0
  22. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/optimizer/extension.py +0 -0
  23. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/optimizer/selections.py +0 -0
  24. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/optimizer/store.py +0 -0
  25. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/payload.py +0 -0
  26. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/policy.py +0 -0
  27. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/py.typed +0 -0
  28. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/relay/__init__.py +0 -0
  29. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/repo.py +0 -0
  30. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/tags.py +0 -0
  31. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/types.py +0 -0
  32. {strawberry_orm-0.24.0 → strawberry_orm-0.24.2}/src/strawberry_orm/upserts.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: strawberry-orm
3
- Version: 0.24.0
3
+ Version: 0.24.2
4
4
  Summary: Unified, backend-agnostic ORM abstraction for Strawberry GraphQL
5
5
  Author: James Davidson, Patrick Arminio
6
6
  Author-email: James Davidson <jamie.t.davidson@gmail.com>, Patrick Arminio <patrick.arminio@gmail.com>
@@ -2044,6 +2044,10 @@ This gives you:
2044
2044
 
2045
2045
  Filters and ordering are applied *before* pagination, so the connection always slices from a correctly filtered and sorted result set.
2046
2046
 
2047
+ The page is cut in the database, not in Python. `first: 50` reads 51 rows (the extra one answers `hasNextPage`), `after` becomes an `OFFSET`, and `totalCount` is a separate count sent only when it is selected — and not even then when the page comes back short, because a short page is the end of the result. `last` without `before` counts first, to find where the window starts. Cursors are the same either way. Each page is its own query, and a database may break ties in a non-unique order differently from one query to the next, so a row could land on two pages and another on none. The cut therefore ends every order — the client's, the resolver's or the model's — with the primary key, unless it already includes it; with no order at all, the primary key alone orders the pages. Rows that do not tie keep their order. A Django queryset the resolver already sliced, or ordered with `.extra(order_by=...)`, is cut in the order it has.
2048
+
2049
+ A few requests are still read whole and sliced in Python: a resolver that returns rows rather than a query, `last: 0` (which Relay defines as every row after the cursor), and, on SQLAlchemy, a join that can repeat a row — to a collection, or through a second `FROM` — without `.distinct()`. SQLAlchemy folds those repeats together with `.unique()` after reading, so a `LIMIT` would count them against the page.
2050
+
2047
2051
  `orm.connection.eager()` accepts the same keyword arguments as `relay.connection()` — `name`, `description`, `deprecation_reason`, `extensions`, and `max_results`.
2048
2052
 
2049
2053
  ### Supplying the queryset yourself
@@ -2009,6 +2009,10 @@ This gives you:
2009
2009
 
2010
2010
  Filters and ordering are applied *before* pagination, so the connection always slices from a correctly filtered and sorted result set.
2011
2011
 
2012
+ The page is cut in the database, not in Python. `first: 50` reads 51 rows (the extra one answers `hasNextPage`), `after` becomes an `OFFSET`, and `totalCount` is a separate count sent only when it is selected — and not even then when the page comes back short, because a short page is the end of the result. `last` without `before` counts first, to find where the window starts. Cursors are the same either way. Each page is its own query, and a database may break ties in a non-unique order differently from one query to the next, so a row could land on two pages and another on none. The cut therefore ends every order — the client's, the resolver's or the model's — with the primary key, unless it already includes it; with no order at all, the primary key alone orders the pages. Rows that do not tie keep their order. A Django queryset the resolver already sliced, or ordered with `.extra(order_by=...)`, is cut in the order it has.
2013
+
2014
+ A few requests are still read whole and sliced in Python: a resolver that returns rows rather than a query, `last: 0` (which Relay defines as every row after the cursor), and, on SQLAlchemy, a join that can repeat a row — to a collection, or through a second `FROM` — without `.distinct()`. SQLAlchemy folds those repeats together with `.unique()` after reading, so a `LIMIT` would count them against the page.
2015
+
2012
2016
  `orm.connection.eager()` accepts the same keyword arguments as `relay.connection()` — `name`, `description`, `deprecation_reason`, `extensions`, and `max_results`.
2013
2017
 
2014
2018
  ### Supplying the queryset yourself
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "strawberry-orm"
3
- version = "0.24.0"
3
+ version = "0.24.2"
4
4
  description = "Unified, backend-agnostic ORM abstraction for Strawberry GraphQL"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "strawberry-orm"
3
- version = "0.24.0"
3
+ version = "0.24.2"
4
4
  description = "Unified, backend-agnostic ORM abstraction for Strawberry GraphQL"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -1516,6 +1516,10 @@ class BaseBackend:
1516
1516
  """Model a query object reads, or ``None`` if this backend cannot tell."""
1517
1517
  return None
1518
1518
 
1519
+ def apply_page(self, query: Any, offset: int, limit: int) -> Any | None:
1520
+ """Cut *query* to one page in SQL, or ``None`` to have it read whole."""
1521
+ return None
1522
+
1519
1523
  def apply_type_scope(self, query: Any, model: type | None, info: Any) -> Any:
1520
1524
  """Apply *model*'s ``scope_rows`` to *query*, if one is registered.
1521
1525
 
@@ -645,10 +645,10 @@ class DjangoBackend(BaseBackend):
645
645
  from django.db.models.functions import RowNumber
646
646
 
647
647
  partition = [F(k) for k in group_key_fields]
648
- if order_input:
649
- ordering = _build_django_order_from_input(order_input)
650
- else:
651
- ordering = [F("pk")]
648
+ ordering = _build_django_order_from_input(order_input) if order_input else []
649
+ tiebreak = _pk_tiebreak(ordering, model)
650
+ if tiebreak is not None:
651
+ ordering = [*ordering, F(tiebreak)]
652
652
 
653
653
  def _run():
654
654
  qs = (
@@ -799,8 +799,22 @@ class DjangoBackend(BaseBackend):
799
799
  with connection.execute_wrapper(_wrapper):
800
800
  yield probe
801
801
 
802
- def count_query(self, query: Any, info: Any) -> int:
803
- return query.count()
802
+ def count_query(self, query: Any, info: Any) -> Any:
803
+ return run_sync(query.count, thread_sensitive=True)
804
+
805
+ def apply_page(self, query: Any, offset: int, limit: int) -> Any | None:
806
+ from strawberry_orm.lazy_resolution import mark_query_scoped, query_is_scoped
807
+
808
+ # Rows already in memory cost nothing to slice; a cut would re-read them.
809
+ if query._result_cache is not None:
810
+ return None
811
+ # Slicing composes with an existing slice. The clone forgets that
812
+ # scope_rows was applied, and a sliced queryset can no longer be
813
+ # filtered, so the optimizer must not try to apply it again.
814
+ page = _ordered_to_the_row(query)[offset : offset + limit]
815
+ if query_is_scoped(query):
816
+ mark_query_scoped(page)
817
+ return page
804
818
 
805
819
  def materialize_query(self, query: Any, info: Any) -> Any:
806
820
  from strawberry_orm._async import in_async_context
@@ -881,12 +895,14 @@ class DjangoBackend(BaseBackend):
881
895
  resolver.__name__ = field_name
882
896
  return resolver
883
897
 
884
- def group_counts(self, query: Any, key_field: str, info: Any) -> dict[Any, int]:
885
- """Only reached once the windowed page came back synchronously."""
898
+ def group_counts(self, query: Any, key_field: str, info: Any) -> Any:
886
899
  from django.db.models import Count
887
900
 
888
- rows = query.values(key_field).annotate(_n=Count("pk"))
889
- return {row[key_field]: row["_n"] for row in rows}
901
+ def _run() -> dict[Any, int]:
902
+ rows = query.values(key_field).annotate(_n=Count("pk"))
903
+ return {row[key_field]: row["_n"] for row in rows}
904
+
905
+ return run_sync(_run, thread_sensitive=True)
890
906
 
891
907
  def _relation_connection_spec(
892
908
  self, model: type, field_name: str, relation: str
@@ -1221,6 +1237,43 @@ class DjangoBackend(BaseBackend):
1221
1237
  # ---------------------------------------------------------------------------
1222
1238
 
1223
1239
 
1240
+ def _pk_tiebreak(ordering: Any, model: type) -> str | None:
1241
+ """The primary key to end *ordering* with, or ``None`` if it names it.
1242
+
1243
+ Each page is cut by its own query, and a database breaks ties in a
1244
+ non-unique order however it likes, differently from one page to the next,
1245
+ so a row could land on two pages and another on none.
1246
+ """
1247
+ pk = model._meta.pk # type: ignore[attr-defined]
1248
+ names = {"pk", pk.name, pk.attname}
1249
+ for term in ordering:
1250
+ if isinstance(term, str):
1251
+ name = term.lstrip("-")
1252
+ else:
1253
+ name = getattr(getattr(term, "expression", term), "name", None)
1254
+ if name in names:
1255
+ return None
1256
+ return pk.attname
1257
+
1258
+
1259
+ def _ordered_to_the_row(queryset: Any) -> Any:
1260
+ """*queryset* in an order no two rows share: its own, then the primary key.
1261
+
1262
+ A sliced queryset can no longer be reordered, and reordering one with an
1263
+ ``.extra(order_by=...)`` would drop that order, so those are left as they are.
1264
+ """
1265
+ query = queryset.query
1266
+ if query.is_sliced or query.extra_order_by:
1267
+ return queryset.all()
1268
+ ordering = query.order_by or (
1269
+ query.get_meta().ordering if query.default_ordering else ()
1270
+ )
1271
+ tiebreak = _pk_tiebreak(ordering, queryset.model)
1272
+ if tiebreak is None:
1273
+ return queryset.all()
1274
+ return queryset.order_by(*ordering, tiebreak)
1275
+
1276
+
1224
1277
  def _make_on_resolver(
1225
1278
  backend: Any, field_name: str, relation: str, return_ann: Any
1226
1279
  ) -> Any:
@@ -150,6 +150,19 @@ class Backend(Protocol):
150
150
  """
151
151
  ...
152
152
 
153
+ def apply_page(self, query: Any, offset: int, limit: int) -> Any | None:
154
+ """Cut *query* to *limit* rows starting *offset* rows in.
155
+
156
+ The cut composes with a limit or offset *query* already has. Returns
157
+ ``None`` when cutting in SQL could change which rows the page holds,
158
+ in which case the connection reads the whole result instead.
159
+ """
160
+ ...
161
+
162
+ def count_query(self, query: Any, info: Any) -> AwaitableOrValue[int]:
163
+ """Count the rows *query* yields, as the connection would read them."""
164
+ ...
165
+
153
166
  # -- Grouping / aggregation -----------------------------------------------
154
167
 
155
168
  def apply_aggregation(
@@ -540,11 +540,10 @@ class SQLAlchemyBackend(BaseBackend):
540
540
  from sqlalchemy import func, select
541
541
 
542
542
  key_cols = [getattr(model, k) for k in group_key_fields]
543
- if order_input:
544
- order_clauses = _build_sa_order_from_input(order_input, model)
545
- else:
546
- pk_col = _get_sa_pk_column(model)
547
- order_clauses = [pk_col]
543
+ order_clauses = (
544
+ _build_sa_order_from_input(order_input, model) if order_input else []
545
+ )
546
+ order_clauses += _missing_pk(order_clauses, model)
548
547
 
549
548
  rn = (
550
549
  func.row_number()
@@ -761,12 +760,44 @@ class SQLAlchemyBackend(BaseBackend):
761
760
  finally:
762
761
  event.remove(target, "do_orm_execute", _on_execute)
763
762
 
764
- def count_query(self, query: Any, info: Any) -> int:
763
+ def count_query(self, query: Any, info: Any) -> Any:
765
764
  from sqlalchemy import func, select
766
765
 
767
766
  subq = query.subquery()
768
- stmt = select(func.count()).select_from(subq)
769
- return self._get_session(info).execute(stmt).scalar_one()
767
+ counted: Any = subq
768
+ if _may_repeat_rows(query):
769
+ # Rows are read with .unique(), so a join that repeats an entity
770
+ # yields it once; count the distinct entities to match.
771
+ entity = self.query_model(query)
772
+ keys = [
773
+ subq.corresponding_column(c) for c in sa_inspect(entity).primary_key
774
+ ]
775
+ counted = select(*keys).distinct().subquery()
776
+ stmt = select(func.count()).select_from(counted)
777
+ session = self._get_session(info)
778
+ if self._is_async_session(session):
779
+ return self._scalar_async(session, stmt)
780
+ return session.execute(stmt).scalar_one()
781
+
782
+ async def _scalar_async(self, session: Any, stmt: Any) -> Any:
783
+ return (await session.execute(stmt)).scalar_one()
784
+
785
+ def apply_page(self, query: Any, offset: int, limit: int) -> Any | None:
786
+ from sqlalchemy.exc import CompileError
787
+
788
+ if _may_repeat_rows(query) or query._fetch_clause is not None:
789
+ return None
790
+ try:
791
+ prior_offset, prior_limit = query._offset or 0, query._limit
792
+ except CompileError:
793
+ # A limit or offset given as an expression cannot be composed.
794
+ return None
795
+ if prior_limit is not None:
796
+ limit = max(0, min(limit, prior_limit - offset))
797
+ missing = _missing_pk(query._order_by_clauses, self.query_model(query))
798
+ if missing:
799
+ query = query.order_by(*missing)
800
+ return query.offset(prior_offset + offset).limit(limit)
770
801
 
771
802
  def materialize_query(self, query: Any, info: Any) -> Any:
772
803
  return self._execute_stmt(query, info)
@@ -812,8 +843,13 @@ class SQLAlchemyBackend(BaseBackend):
812
843
  subq = query.subquery()
813
844
  key_col = subq.c[key_field]
814
845
  stmt = select(key_col, func.count()).select_from(subq).group_by(key_col)
815
- # Only reached once the windowed page came back synchronously.
816
- return dict(self._get_session(info).execute(stmt).all())
846
+ session = self._get_session(info)
847
+ if self._is_async_session(session):
848
+ return self._group_counts_async(session, stmt)
849
+ return dict(session.execute(stmt).all())
850
+
851
+ async def _group_counts_async(self, session: Any, stmt: Any) -> dict[Any, int]:
852
+ return dict((await session.execute(stmt)).all())
817
853
 
818
854
  def _relation_connection_spec(
819
855
  self, model: type, field_name: str, relation: str
@@ -1295,8 +1331,8 @@ class SQLAlchemyBackend(BaseBackend):
1295
1331
 
1296
1332
  try:
1297
1333
  result = session.execute(stmt)
1298
- except (OperationalError, ProgrammingError):
1299
- raise ValueError("Invalid filter expression") from None
1334
+ except (OperationalError, ProgrammingError) as exc:
1335
+ raise _sanitized(exc) from exc
1300
1336
 
1301
1337
  return list(result.scalars().unique().all())
1302
1338
 
@@ -1305,8 +1341,8 @@ class SQLAlchemyBackend(BaseBackend):
1305
1341
 
1306
1342
  try:
1307
1343
  result = await session.execute(stmt)
1308
- except (OperationalError, ProgrammingError):
1309
- raise ValueError("Invalid filter expression") from None
1344
+ except (OperationalError, ProgrammingError) as exc:
1345
+ raise _sanitized(exc) from exc
1310
1346
 
1311
1347
  return list(result.scalars().unique().all())
1312
1348
 
@@ -1959,6 +1995,70 @@ def _get_sa_pk_column(model: type) -> Any:
1959
1995
  return getattr(model, pk_col.key)
1960
1996
 
1961
1997
 
1998
+ def _missing_pk(order_clauses: Any, entity: Any) -> list[Any]:
1999
+ """The primary-key columns an order lacks, to end it with.
2000
+
2001
+ Each page is cut by its own query, and a database breaks ties in a
2002
+ non-unique order however it likes, differently from one page to the next,
2003
+ so a row could land on two pages and another on none. The key is read
2004
+ off *entity* itself, so an aliased entity is ordered by its own columns.
2005
+ """
2006
+ from sqlalchemy.sql import coercions, roles
2007
+ from sqlalchemy.sql.util import unwrap_order_by
2008
+
2009
+ mapper = getattr(sa_inspect(entity, raiseerr=False), "mapper", None)
2010
+ if mapper is None:
2011
+ return []
2012
+ ordered = [
2013
+ column
2014
+ for clause in order_clauses
2015
+ for column in unwrap_order_by(coercions.expect(roles.OrderByRole, clause))
2016
+ ]
2017
+ primary_key = [
2018
+ getattr(entity, mapper.get_property_by_column(column).key).expression
2019
+ for column in mapper.primary_key
2020
+ ]
2021
+ return [
2022
+ pk
2023
+ for pk in primary_key
2024
+ if not any(
2025
+ pk.shares_lineage(column) and getattr(column, "table", None) is pk.table
2026
+ for column in ordered
2027
+ )
2028
+ ]
2029
+
2030
+
2031
+ def _sanitized(exc: Any) -> Exception:
2032
+ """What a client sees in place of a database error.
2033
+
2034
+ The original names tables and columns, or the database host, so it is
2035
+ kept only as the cause, for the server log. Only an error from running the
2036
+ statement can be a bad filter: a refused connection ("too many clients")
2037
+ carries no statement, and a dropped one is invalidated.
2038
+ """
2039
+ if exc.statement is None or exc.connection_invalidated:
2040
+ return ConnectionError("Database connection failed")
2041
+ return ValueError("Invalid filter expression")
2042
+
2043
+
2044
+ def _may_repeat_rows(stmt: Any) -> bool:
2045
+ """True when *stmt* can return one entity on several rows.
2046
+
2047
+ That happens through a join to many rows, a join the ORM cannot see the
2048
+ shape of, or a second FROM. A join along a many-to-one relationship
2049
+ cannot repeat the row, and neither can a ``DISTINCT`` statement.
2050
+ """
2051
+ if stmt._distinct:
2052
+ return False
2053
+ if len(stmt.column_descriptions) != 1 or len(stmt.get_final_froms()) != 1:
2054
+ return True
2055
+ for target, *_rest in stmt._setup_joins:
2056
+ prop = getattr(target, "property", None)
2057
+ if getattr(prop, "uselist", True):
2058
+ return True
2059
+ return False
2060
+
2061
+
1962
2062
  def _introspect_sa_model(
1963
2063
  model: type,
1964
2064
  ) -> list[tuple[str, type, bool, type | None]]:
@@ -863,7 +863,26 @@ class TortoiseBackend(BaseBackend):
863
863
  setattr(client, name, original)
864
864
 
865
865
  async def count_query(self, query: Any, info: Any) -> int:
866
- return await query.count()
866
+ if not query._distinct:
867
+ return await query.count()
868
+ # Tortoise's count drops DISTINCT and would count a join's repeats,
869
+ # so count the rows of the statement itself. Built on a clone: the
870
+ # caller may still cut its page from *query*.
871
+ counted = query._clone()
872
+ counted._choose_db_if_not_chosen()
873
+ counted._make_query()
874
+ inner, values = counted.query.get_parameterized_sql()
875
+ rows = await counted._db.execute_query_dict(
876
+ f"SELECT COUNT(*) AS n FROM ({inner}) _orm_counted", values
877
+ )
878
+ return rows[0]["n"]
879
+
880
+ def apply_page(self, query: Any, offset: int, limit: int) -> Any | None:
881
+ prior_offset, prior_limit = query._offset or 0, query._limit
882
+ if prior_limit is not None:
883
+ limit = max(0, min(limit, prior_limit - offset))
884
+ page = _ordered_to_the_row(query)
885
+ return page.offset(prior_offset + offset).limit(limit)
867
886
 
868
887
  async def materialize_query(self, query: Any, info: Any) -> list[Any]:
869
888
  return _apply_python_ordering(
@@ -1980,22 +1999,49 @@ def _quote_ident(name: str, model: type, quote_char: str) -> str:
1980
1999
  def _window_ordering(
1981
2000
  model: type, order_clauses: list[str], quote_char: str
1982
2001
  ) -> list[str]:
1983
- """Render the window's ORDER BY, falling back to the primary key."""
2002
+ """Render the window's ORDER BY, ending with the primary key.
2003
+
2004
+ Rows that tie on the requested order would otherwise be numbered however
2005
+ the database likes, differently from one page to the next.
2006
+ """
1984
2007
  meta = model._meta # type: ignore[attr-defined]
1985
2008
  projection = _db_columns(model)
1986
2009
  rendered: list[str] = []
2010
+ columns: set[str] = set()
1987
2011
  for clause in order_clauses:
1988
2012
  descending = clause.startswith("-")
1989
2013
  field = clause[1:] if descending else clause
1990
2014
  column = projection.get(field, field)
2015
+ columns.add(column)
1991
2016
  direction = " DESC" if descending else " ASC"
1992
2017
  rendered.append(_quote_ident(column, model, quote_char) + direction)
1993
- if not rendered:
1994
- pk_column = projection.get(meta.pk_attr, meta.db_pk_column)
2018
+ pk_column = projection.get(meta.pk_attr, meta.db_pk_column)
2019
+ if pk_column not in columns:
1995
2020
  rendered.append(_quote_ident(pk_column, model, quote_char) + " ASC")
1996
2021
  return rendered
1997
2022
 
1998
2023
 
2024
+ def _ordered_to_the_row(query: Any) -> Any:
2025
+ """*query* in an order no two rows share: its own, then the primary key.
2026
+
2027
+ Each page is cut by its own query, and a database breaks ties in a
2028
+ non-unique order however it likes, differently from one page to the next,
2029
+ so a row could land on two pages and another on none.
2030
+ """
2031
+ from pypika_tortoise import Order
2032
+
2033
+ meta = query.model._meta
2034
+ orderings = list(query._orderings)
2035
+ # Tortoise falls back to the model's ordering, but not for annotated queries.
2036
+ if not orderings and not query._annotations:
2037
+ orderings = list(meta.ordering)
2038
+ if any(field == meta.pk_attr for field, _ in orderings):
2039
+ return query
2040
+ ordered = query._clone()
2041
+ ordered._orderings = [*orderings, (meta.pk_attr, Order.asc)]
2042
+ return ordered
2043
+
2044
+
1999
2045
  def _windowed_sql(
2000
2046
  query: Any,
2001
2047
  model: type,
@@ -3,7 +3,8 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import inspect
6
- from collections.abc import Iterable
6
+ import sys
7
+ from collections.abc import Callable, Iterable
7
8
  from typing import Any, TypeVar, cast
8
9
 
9
10
  import strawberry
@@ -15,15 +16,40 @@ from strawberry.extensions.field_extension import (
15
16
  from strawberry.relay import Connection, Edge, ListConnection, PageInfo
16
17
  from strawberry.relay.fields import ConnectionExtension
17
18
  from strawberry.relay.types import Node
19
+ from strawberry.relay.utils import (
20
+ SliceMetadata,
21
+ should_resolve_list_connection_edges,
22
+ to_base64,
23
+ )
18
24
  from strawberry.types import Info
25
+ from strawberry.types.base import StrawberryContainer, get_object_definition
19
26
 
20
- from strawberry_orm._async import AwaitableOrValue
27
+ from strawberry_orm._async import AwaitableOrValue, await_maybe
21
28
  from strawberry_orm.backends._base import (
22
29
  _selection_requests,
23
30
  )
24
- from strawberry_orm.optimizer.extension import optimize_query_nodes
31
+ from strawberry_orm.optimizer.extension import (
32
+ get_configured_optimizer,
33
+ optimize_query_nodes,
34
+ )
25
35
 
26
36
  NodeType = TypeVar("NodeType", bound=relay.Node)
37
+ T = TypeVar("T")
38
+
39
+ #: Databases number rows with signed 64-bit integers, and a cursor can carry
40
+ #: any number at all; nothing past this is sent as an offset or a limit.
41
+ _LAST_POSITION = 2**63 - 1
42
+
43
+
44
+ def _then(value: AwaitableOrValue[T], step: Callable[[T], Any]) -> Any:
45
+ """Apply *step* to *value*, awaiting either one where it is awaitable."""
46
+ if not inspect.isawaitable(value):
47
+ return step(value)
48
+
49
+ async def _chained() -> Any:
50
+ return await await_maybe(step(await value))
51
+
52
+ return _chained()
27
53
 
28
54
 
29
55
  def _should_await_nodes(nodes: Any, info: Info) -> bool:
@@ -61,6 +87,24 @@ def _orm_backend_from_info(info: Any) -> Any | None:
61
87
  return backend
62
88
 
63
89
 
90
+ class _PageRows:
91
+ """A page the database cut at *offset*, indexed as the whole result.
92
+
93
+ Strawberry slices what it is given by absolute position and numbers the
94
+ cursors from there, so handing it the page at its original positions keeps
95
+ every cursor and ``pageInfo`` flag exactly what slicing the full list gave.
96
+ """
97
+
98
+ __slots__ = ("offset", "rows")
99
+
100
+ def __init__(self, rows: list[Any], offset: int) -> None:
101
+ self.rows = rows
102
+ self.offset = offset
103
+
104
+ def __getitem__(self, window: slice) -> list[Any]:
105
+ return self.rows[window.start - self.offset : window.stop - self.offset]
106
+
107
+
64
108
  class PreslicedRows(list):
65
109
  """One parent's page, already cut to size by a windowed query.
66
110
 
@@ -131,7 +175,10 @@ class ORMConnectionExtension(ConnectionExtension):
131
175
  first: int | None = None,
132
176
  last: int | None = None,
133
177
  ) -> Any:
134
- nodes = optimize_query_nodes(nodes, info)
178
+ # An ORM connection cuts its page out of the query before reading it,
179
+ # so it has to receive the query, not every row the query matches.
180
+ if not _cuts_its_own_page(self.connection_type):
181
+ nodes = optimize_query_nodes(nodes, info)
135
182
  if inspect.isawaitable(nodes):
136
183
 
137
184
  async def _await_optimized() -> Any:
@@ -232,6 +279,12 @@ class ORMConnectionExtension(ConnectionExtension):
232
279
  return connection
233
280
 
234
281
 
282
+ def _cuts_its_own_page(connection_type: Any) -> bool:
283
+ return isinstance(connection_type, type) and issubclass(
284
+ connection_type, ORMListConnection
285
+ )
286
+
287
+
235
288
  def _use_orm_connection_extension(field: Any) -> None:
236
289
  """Replace Strawberry's ``ConnectionExtension`` with ``ORMConnectionExtension``."""
237
290
  field.extensions = [
@@ -354,6 +407,10 @@ def connection_type_for_node(node_type: type) -> Any:
354
407
  class ORMListConnection(ORMConnection[NodeType]):
355
408
  """A ListConnection that works with any ORM backend.
356
409
 
410
+ Given a query, it reads only the requested page plus one row, which is
411
+ what answers ``hasNextPage``; ``totalCount`` is counted separately, and
412
+ only when selected. Given rows, it slices them as Strawberry does.
413
+
357
414
  When the generated connection subclass has ``_orm_aggregate_meta`` set,
358
415
  this override computes aggregates, groups, and page-level aggregates
359
416
  based on the client's selection set.
@@ -367,7 +424,183 @@ class ORMListConnection(ORMConnection[NodeType]):
367
424
  info,
368
425
  **kwargs,
369
426
  ) -> AwaitableOrValue:
370
- nodes = optimize_query_nodes(nodes, info)
427
+ backend, store = get_configured_optimizer(info)
428
+ if backend is not None and store is not None and backend.is_query_object(nodes):
429
+ paged = cls._resolve_query_page(backend, nodes, info=info, **kwargs)
430
+ if paged is not None:
431
+ return paged
432
+ return cls._resolve_rows(nodes, info=info, **kwargs)
433
+
434
+ @classmethod
435
+ def _resolve_query_page(
436
+ cls,
437
+ backend: Any,
438
+ query: Any,
439
+ *,
440
+ info: Any,
441
+ before: str | None = None,
442
+ after: str | None = None,
443
+ first: int | None = None,
444
+ last: int | None = None,
445
+ max_results: int | None = None,
446
+ **kwargs: Any,
447
+ ) -> Any:
448
+ """Read only the page *query* is asked for, or ``None`` to read it whole.
449
+
450
+ ``scope_rows`` goes on before the cut, so the count and the page see
451
+ the same rows, and Django can still filter a queryset it then slices.
452
+ """
453
+ prefix = cls._edge_type().CURSOR_PREFIX
454
+ window = SliceMetadata.from_arguments(
455
+ info,
456
+ before=before,
457
+ after=after,
458
+ first=first,
459
+ last=last,
460
+ max_results=max_results,
461
+ prefix=prefix,
462
+ )
463
+ # Cursors are the client's to forge. A negative position slices a
464
+ # Python list from its end, which only reading the whole list repeats.
465
+ if window.start < 0 or window.end < 0:
466
+ return None
467
+ query = backend.apply_type_scope(query, backend.query_model(query), info)
468
+ want_total = _selection_requests(info, "totalCount")
469
+ args = {
470
+ "before": before,
471
+ "after": after,
472
+ "first": first,
473
+ "last": last,
474
+ "max_results": max_results,
475
+ **kwargs,
476
+ }
477
+
478
+ def count() -> AwaitableOrValue[int | None]:
479
+ return backend.count_query(query, info) if want_total else None
480
+
481
+ if not should_resolve_list_connection_edges(info):
482
+ # Strawberry builds no edges without edges or pageInfo selected.
483
+ connection = cls._slice(_PageRows([], window.start), info=info, **args)
484
+ return cls._finish_connection(connection, count(), info=info, **args)
485
+
486
+ if window.end != sys.maxsize:
487
+ limit = min(window.overfetch, _LAST_POSITION) - window.start
488
+
489
+ def finish(rows: list[Any]) -> Any:
490
+ connection = cls._slice(
491
+ _PageRows(rows, window.start), info=info, **args
492
+ )
493
+ # A short page is the end of the result, unless it started past
494
+ # the end, so it gives the total. Otherwise count - only now,
495
+ # after the page: an AsyncSession runs one statement at a time.
496
+ ended = len(rows) < limit and (rows or not window.start)
497
+ total = window.start + len(rows) if want_total and ended else count()
498
+ return cls._finish_connection(connection, total, info=info, **args)
499
+
500
+ # A window that ends where it starts, or past every addressable
501
+ # row, holds nothing - as slicing the list would have found.
502
+ if limit <= 0:
503
+ return finish([])
504
+ page = backend.apply_page(query, window.start, limit)
505
+ if page is None:
506
+ return None
507
+ return _then(optimize_query_nodes(page, info), finish)
508
+
509
+ # ``last`` without ``before`` counts back from the end, so the total
510
+ # locates the window. ``last: 0`` is Strawberry's ``[-0:]``: every row
511
+ # after the cursor, which is a full read either way.
512
+ if not last:
513
+ return None
514
+ return _then(
515
+ backend.count_query(query, info),
516
+ lambda total: cls._cut_tail(
517
+ backend,
518
+ query,
519
+ total,
520
+ start=window.start,
521
+ prefix=prefix,
522
+ info=info,
523
+ want_total=want_total,
524
+ **args,
525
+ ),
526
+ )
527
+
528
+ @classmethod
529
+ def _cut_tail(
530
+ cls,
531
+ backend: Any,
532
+ query: Any,
533
+ total: int,
534
+ *,
535
+ start: int,
536
+ prefix: str,
537
+ info: Any,
538
+ want_total: bool,
539
+ **args: Any,
540
+ ) -> Any:
541
+ """The last ``last`` rows after *start*, given the query's *total*."""
542
+ last = args["last"]
543
+ offset = max(start, total - last)
544
+ limit = min(offset + last, _LAST_POSITION) - offset
545
+
546
+ def finish(rows: list[Any]) -> Any:
547
+ # Asked for as the plain page it now is, so Strawberry numbers the
548
+ # cursors from *offset*. Relay measures hasPreviousPage from the
549
+ # ``after`` cursor, not from the start of the list.
550
+ connection = cls._slice(
551
+ _PageRows(rows, offset),
552
+ info=info,
553
+ after=to_base64(prefix, offset - 1) if offset else None,
554
+ first=len(rows),
555
+ max_results=args["max_results"],
556
+ )
557
+ connection.page_info.has_previous_page = offset > start
558
+ return cls._finish_connection(
559
+ connection, total if want_total else None, info=info, **args
560
+ )
561
+
562
+ if limit <= 0:
563
+ return finish([])
564
+ page = backend.apply_page(query, offset, limit)
565
+ if page is None:
566
+ return cls._resolve_rows(query, info=info, **args)
567
+ return _then(optimize_query_nodes(page, info), finish)
568
+
569
+ @classmethod
570
+ def _slice(cls, nodes: Any, **kwargs: Any) -> AwaitableOrValue[Any]:
571
+ return super().resolve_connection(nodes, **kwargs)
572
+
573
+ @classmethod
574
+ def _edge_type(cls) -> Any:
575
+ """The edge class, whose cursor prefix the page's cursors carry."""
576
+ type_def = get_object_definition(cls, strict=True)
577
+ edge = type_def.get_field("edges").resolve_type(type_definition=type_def)
578
+ while isinstance(edge, StrawberryContainer):
579
+ edge = edge.of_type
580
+ return edge
581
+
582
+ @classmethod
583
+ def _resolve_rows(
584
+ cls,
585
+ nodes,
586
+ *,
587
+ info,
588
+ **kwargs,
589
+ ) -> AwaitableOrValue:
590
+ """Read every row and slice the page out in Python."""
591
+ rows = optimize_query_nodes(nodes, info)
592
+ if _should_await_nodes(rows, info):
593
+ return _then(rows, lambda read: cls._slice_rows(read, info=info, **kwargs))
594
+ return cls._slice_rows(rows, info=info, **kwargs)
595
+
596
+ @classmethod
597
+ def _slice_rows(
598
+ cls,
599
+ nodes,
600
+ *,
601
+ info,
602
+ **kwargs,
603
+ ) -> AwaitableOrValue:
371
604
  want_total = _selection_requests(info, "totalCount")
372
605
  total_count: AwaitableOrValue[int | None] = (
373
606
  _connection_total_count(nodes, info) if want_total else None
@@ -408,21 +641,15 @@ class ORMListConnection(ORMConnection[NodeType]):
408
641
  info: Any,
409
642
  **kwargs: Any,
410
643
  ) -> AwaitableOrValue[Any]:
411
- if inspect.isawaitable(total_count):
412
-
413
- async def _await_total() -> Any:
414
- count = await total_count
415
- processed = cls._post_process_connection(
416
- connection, info=info, **kwargs
417
- )
418
- if inspect.isawaitable(processed):
419
- processed = await processed
420
- return cls._attach_total_count(processed, count)
421
-
422
- return _await_total()
423
-
424
- processed = cls._post_process_connection(connection, info=info, **kwargs)
425
- return cls._attach_total_count(processed, total_count)
644
+ # Groups and aggregates are awaitable on async backends whether or not
645
+ # the total is, so the total waits for them either way.
646
+ return _then(
647
+ total_count,
648
+ lambda count: _then(
649
+ cls._post_process_connection(connection, info=info, **kwargs),
650
+ lambda processed: cls._attach_total_count(processed, count),
651
+ ),
652
+ )
426
653
 
427
654
  @classmethod
428
655
  def _post_process_connection(cls, connection, *, info, **kwargs):
@@ -503,8 +730,11 @@ class ORMListConnection(ORMConnection[NodeType]):
503
730
  order_input=order_input,
504
731
  )
505
732
 
506
- items_by_key = None
507
- if _selection_requests(info, "groups", "items"):
733
+ def with_items(resolved: list[Any]) -> Any:
734
+ if not _selection_requests(info, "groups", "items"):
735
+ return resolved, None
736
+ # Only now are the group key fields known: an async apply_grouping
737
+ # records them on *meta* when it runs, not when it is called.
508
738
  after_cursor = _extract_items_after(info)
509
739
  offset = _decode_cursor_offset(after_cursor) if after_cursor else 0
510
740
  items_by_key = backend.batch_group_items(
@@ -515,20 +745,9 @@ class ORMListConnection(ORMConnection[NodeType]):
515
745
  per_group_limit=(_extract_items_first(info) or 10) + offset + 1,
516
746
  order_input=_extract_items_order(info),
517
747
  )
748
+ return _then(items_by_key, lambda by_key: (resolved, by_key))
518
749
 
519
- if inspect.isawaitable(groups) or inspect.isawaitable(items_by_key):
520
-
521
- async def _resolve() -> Any:
522
- return (
523
- await groups if inspect.isawaitable(groups) else groups,
524
- await items_by_key
525
- if inspect.isawaitable(items_by_key)
526
- else items_by_key,
527
- )
528
-
529
- return _resolve()
530
-
531
- return groups, items_by_key
750
+ return _then(groups, with_items)
532
751
 
533
752
  @classmethod
534
753
  def _attach_groups(