Skip to content

Commit

Permalink
feat: Allow setting max_results for connection fields
Browse files Browse the repository at this point in the history
  • Loading branch information
bellini666 committed Jan 12, 2025
1 parent dbaf898 commit 2694167
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- id: check-xml
- id: check-symlinks
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.6
rev: v0.9.1
hooks:
- id: ruff-format
- id: ruff
Expand Down
54 changes: 27 additions & 27 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ django = ">=4.2"
asgiref = ">=3.8"
django-choices-field = { version = ">=2.2.2", optional = true }
django-debug-toolbar = { version = ">=3.4", optional = true }
strawberry-graphql = ">=0.257.0"
strawberry-graphql = ">=0.258.0"
django-tree-queries = "^0.19.0"

[tool.poetry.group.dev.dependencies]
Expand All @@ -54,7 +54,7 @@ pytest-django = "^4.1.0"
pytest-mock = "^3.5.1"
pytest-snapshot = "^0.9.0"
pytest-watch = "^4.2.0"
ruff = "^0.8.5"
ruff = "^0.9.1"
django-polymorphic = "^3.1.0"
setuptools = "^75.1.0"
psycopg2 = "^2.9.9"
Expand Down
8 changes: 7 additions & 1 deletion strawberry_django/fields/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ def connection(
metadata: Mapping[Any, Any] | None = None,
directives: Sequence[object] | None = (),
extensions: Sequence[FieldExtension] = (),
max_results: int | None = None,
filters: type | None = UNSET,
order: type | None = UNSET,
only: TypeOrSequence[str] | None = None,
Expand Down Expand Up @@ -795,6 +796,7 @@ def connection(
metadata: Mapping[Any, Any] | None = None,
directives: Sequence[object] | None = (),
extensions: Sequence[FieldExtension] = (),
max_results: int | None = None,
filters: type | None = UNSET,
order: type | None = UNSET,
only: TypeOrSequence[str] | None = None,
Expand All @@ -821,6 +823,7 @@ def connection(
metadata: Mapping[Any, Any] | None = None,
directives: Sequence[object] | None = (),
extensions: Sequence[FieldExtension] = (),
max_results: int | None = None,
filters: type | None = UNSET,
order: type | None = UNSET,
only: TypeOrSequence[str] | None = None,
Expand Down Expand Up @@ -892,7 +895,10 @@ def connection(
https://relay.dev/graphql/connections.htm
"""
extensions = [*extensions, StrawberryDjangoConnectionExtension()]
extensions = [
*extensions,
StrawberryDjangoConnectionExtension(max_results=max_results),
]
f = field_cls(
python_name=None,
django_name=field_name,
Expand Down
1 change: 1 addition & 0 deletions strawberry_django/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ def _optimize_prefetch_queryset(
related_field_id=related_field_id,
offset=slice_metadata.start,
limit=slice_metadata.end - slice_metadata.start,
max_results=connection_extension.max_results,
)
else:
mark_optimized = False
Expand Down
7 changes: 6 additions & 1 deletion strawberry_django/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def apply_window_pagination(
related_field_id: str,
offset: int = 0,
limit: Optional[int] = UNSET,
max_results: Optional[int] = None,
) -> _QS:
"""Apply pagination using window functions.
Expand Down Expand Up @@ -211,7 +212,11 @@ def apply_window_pagination(

if limit is UNSET:
settings = strawberry_django_settings()
limit = settings["PAGINATION_DEFAULT_LIMIT"]
limit = (
max_results
if max_results is not None
else settings["PAGINATION_DEFAULT_LIMIT"]
)

# Limit == -1 means no limit. sys.maxsize is set by relay when paginating
# from the end to as a way to mimic a "not limit" as well
Expand Down

0 comments on commit 2694167

Please sign in to comment.