Skip to content

Commit

Permalink
fix(pagination)!: Use PAGINATION_DEFAULT_LIMIT when limit is not pr…
Browse files Browse the repository at this point in the history
…ovided
  • Loading branch information
bellini666 committed Dec 15, 2024
1 parent e76b3fa commit ad7dad2
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 14 deletions.
28 changes: 18 additions & 10 deletions strawberry_django/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from strawberry_django.fields.base import StrawberryDjangoFieldBase
from strawberry_django.resolvers import django_resolver
from .settings import strawberry_django_settings

from .arguments import argument

Expand All @@ -22,16 +23,14 @@
PAGINATION_ARG = "pagination"


@strawberry.input
class OffsetPaginationInput:
offset: int = 0
limit: Optional[int] = None


@strawberry.type
class OffsetPaginationInfo:
offset: int = 0
limit: Optional[int] = None
limit: Optional[int] = UNSET


@strawberry.input
class OffsetPaginationInput(OffsetPaginationInfo): ...


@strawberry.type
Expand Down Expand Up @@ -142,8 +141,13 @@ def apply(
)
else:
start = pagination.offset
if pagination.limit is not None and pagination.limit >= 0:
stop = start + pagination.limit
limit = pagination.limit
if limit is UNSET:
settings = strawberry_django_settings()
limit = settings["PAGINATION_DEFAULT_LIMIT"]

if limit is not None and limit >= 0:
stop = start + limit
queryset = queryset[start:stop]
else:
queryset = queryset[start:]
Expand All @@ -165,7 +169,7 @@ def apply_window_pagination(
*,
related_field_id: str,
offset: int = 0,
limit: Optional[int] = None,
limit: Optional[int] = UNSET,
) -> _QS:
"""Apply pagination using window functions.
Expand Down Expand Up @@ -204,6 +208,10 @@ def apply_window_pagination(
if offset:
queryset = queryset.filter(_strawberry_row_number__gt=offset)

if limit is UNSET:
settings = strawberry_django_settings()
limit = 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
if limit is not None and limit >= 0 and limit != sys.maxsize:
Expand Down
2 changes: 1 addition & 1 deletion tests/projects/snapshots/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ type OffsetPaginationInfo {

input OffsetPaginationInput {
offset: Int! = 0
limit: Int = null
limit: Int
}

type OperationInfo {
Expand Down
2 changes: 1 addition & 1 deletion tests/projects/snapshots/schema_with_inheritance.gql
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ type OffsetPaginationInfo {

input OffsetPaginationInput {
offset: Int! = 0
limit: Int = null
limit: Int
}

type OperationInfo {
Expand Down
59 changes: 57 additions & 2 deletions tests/test_paginated_type.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import textwrap
from typing import Annotated

from django.test.utils import override_settings
import pytest
import strawberry
from django.db.models import QuerySet

import strawberry_django
from strawberry_django.optimizer import DjangoOptimizerExtension
from strawberry_django.pagination import OffsetPaginated, OffsetPaginationInput
from strawberry_django.settings import StrawberryDjangoSettings
from tests import models


Expand Down Expand Up @@ -69,7 +71,7 @@ class Query:
input OffsetPaginationInput {
offset: Int! = 0
limit: Int = null
limit: Int
}
type Query {
Expand Down Expand Up @@ -630,7 +632,7 @@ def fruits_with_order_and_filter(self) -> QuerySet[models.Fruit]: ...
input OffsetPaginationInput {
offset: Int! = 0
limit: Int = null
limit: Int
}
type Query {
Expand Down Expand Up @@ -900,3 +902,56 @@ def fruits_with_order_and_filter(
],
},
}


@pytest.mark.django_db(transaction=True)
@override_settings(
STRAWBERRY_DJANGO=StrawberryDjangoSettings( # type: ignore
PAGINATION_DEFAULT_LIMIT=2,
),
)
def test_pagination_default_limit():
@strawberry_django.type(models.Fruit)
class Fruit:
id: int
name: str

@strawberry.type
class Query:
fruits: OffsetPaginated[Fruit] = strawberry_django.offset_paginated()

models.Fruit.objects.create(name="Apple")
models.Fruit.objects.create(name="Banana")
models.Fruit.objects.create(name="Strawberry")
models.Fruit.objects.create(name="Watermelon")

schema = strawberry.Schema(query=Query)

query = """\
query GetFruits ($pagination: OffsetPaginationInput) {
fruits (pagination: $pagination) {
totalCount
results {
name
}
}
}
"""

res = schema.execute_sync(query)
assert res.errors is None
assert res.data == {
"fruits": {
"totalCount": 4,
"results": [{"name": "Apple"}, {"name": "Banana"}],
}
}

res = schema.execute_sync(query, variable_values={"pagination": {"offset": 1}})
assert res.errors is None
assert res.data == {
"fruits": {
"totalCount": 4,
"results": [{"name": "Banana"}, {"name": "Strawberry"}],
}
}

0 comments on commit ad7dad2

Please sign in to comment.