-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
feat: New Paginated generic to be used as a wrapped for paginated results #642
Changes from 11 commits
7c6b44e
ed5019b
eda46e7
4c9c068
836c227
816e7d9
8030863
1825934
a79eac0
89074b1
9705ab0
a997293
21d82d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,20 +12,306 @@ An interface for limit/offset pagination can be use for basic pagination needs: | |
@strawberry_django.type(models.Fruit, pagination=True) | ||
class Fruit: | ||
name: auto | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
fruits: list[Fruit] = strawberry_django.field() | ||
``` | ||
|
||
Would produce the following schema: | ||
|
||
```graphql title="schema.graphql" | ||
type Fruit { | ||
name: String! | ||
} | ||
|
||
input OffsetPaginationInput { | ||
offset: Int! = 0 | ||
limit: Int = null | ||
} | ||
|
||
type Query { | ||
fruits(pagination: OffsetPaginationInput): [Fruit!]! | ||
} | ||
``` | ||
|
||
And can be queried like: | ||
|
||
```graphql title="schema.graphql" | ||
query { | ||
fruits(pagination: { offset: 0, limit: 2 }) { | ||
name | ||
color | ||
} | ||
} | ||
``` | ||
|
||
There is not default limit defined. All elements are returned if no pagination limit is defined. | ||
The `pagination` argument can be given to the type, which will enforce the pagination | ||
argument every time the field is annotated as a list, but you can also give it directly | ||
to the field for more control, like: | ||
|
||
```python title="types.py" | ||
@strawberry_django.type(models.Fruit) | ||
class Fruit: | ||
name: auto | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
fruits: list[Fruit] = strawberry_django.field(pagination=True) | ||
``` | ||
|
||
Which will produce the exact same schema. | ||
|
||
### Default limit for pagination | ||
|
||
The default limit for pagination is set to `100`. This can be changed in the | ||
[strawberry django settings](./settings.md) to increase or decrease that number, | ||
or even set to `None` to set it to unlimited. | ||
|
||
To configure it on a per field basis, you can define your own `OffsetPaginationInput` | ||
subclass and modify its default value, like: | ||
|
||
```python | ||
@strawberry.input | ||
def MyOffsetPaginationInput(OffsetPaginationInput): | ||
limit: int = 250 | ||
|
||
|
||
# Pass it to the pagination argument when defining the type | ||
@strawberry_django.type(models.Fruit, pagination=MyOffsetPaginationInput) | ||
class Fruit: | ||
... | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
# Or pass it to the pagination argument when defining the field | ||
fruits: list[Fruit] = strawberry_django.field(pagination=MyOffsetPaginationInput) | ||
``` | ||
|
||
## OffsetPaginated Generic | ||
|
||
For more complex pagination needs, you can use the `OffsetPaginated` generic, which alongside | ||
the `pagination` argument, will wrap the results in an object that contains the results | ||
and the pagination information, together with the `totalCount` of elements excluding pagination. | ||
|
||
```python title="types.py" | ||
from strawberry_django.pagination import OffsetPaginated | ||
|
||
|
||
@strawberry_django.type(models.Fruit) | ||
class Fruit: | ||
name: auto | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
fruits: OffsetPaginated[Fruit] = strawberry_django.offset_paginated() | ||
``` | ||
|
||
Would produce the following schema: | ||
|
||
```graphql title="schema.graphql" | ||
type Fruit { | ||
name: String! | ||
} | ||
|
||
type PaginationInfo { | ||
limit: Int = null | ||
offset: Int! | ||
} | ||
|
||
type FruitOffsetPaginated { | ||
pageInfo: PaginationInfo! | ||
totalCount: Int! | ||
results: [Fruit]! | ||
} | ||
|
||
input OffsetPaginationInput { | ||
offset: Int! = 0 | ||
limit: Int = null | ||
} | ||
|
||
type Query { | ||
fruits(pagination: OffsetPaginationInput): [FruitOffsetPaginated!]! | ||
} | ||
``` | ||
|
||
Which can be queried like: | ||
|
||
```graphql title="schema.graphql" | ||
query { | ||
fruits(pagination: { offset: 0, limit: 2 }) { | ||
totalCount | ||
pageInfo { | ||
limit | ||
offset | ||
} | ||
results { | ||
name | ||
} | ||
} | ||
} | ||
``` | ||
|
||
> [!NOTE] | ||
> OffsetPaginated follow the same rules for the default pagination limit, and can be configured | ||
> in the same way as explained above. | ||
|
||
### Customizing queryset resolver | ||
|
||
It is possible to define a custom resolver for the queryset to either provide a custom | ||
queryset for it, or even to receive extra arguments alongside the pagination arguments. | ||
|
||
Suppose we want to pre-filter a queryset of fruits for only available ones, | ||
while also adding [ordering](./ordering.md) to it. This can be achieved like: | ||
|
||
```python title="types.py" | ||
|
||
@strawberry_django.type(models.Fruit) | ||
class Fruit: | ||
name: auto | ||
price: auto | ||
|
||
|
||
@strawberry_django.order(models.Fruit) | ||
class FruitOrder: | ||
name: auto | ||
price: auto | ||
|
||
|
||
@strawberry.type | ||
class Query: | ||
@straberry.offset_paginated(OffsetPaginated[Fruit], order=order) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo here @bellini666 but this is really gorgeous, overall, I'm loving this feature. |
||
def fruits(self, only_available: bool = True) -> QuerySet[Fruit]: | ||
queryset = models.Fruit.objects.all() | ||
if only_available: | ||
queryset = queryset.filter(available=True) | ||
|
||
return queryset | ||
``` | ||
|
||
This would produce the following schema: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm a huge fan of adding the resulting schema to the docs for each of these things, it's been a big point of frustration for me with the docs not seeing how it should come out. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback! I'm planing on doing some improvements to the docs soon and I'll do more of this! |
||
|
||
```graphql title="schema.graphql" | ||
type Fruit { | ||
name: String! | ||
price: Decimal! | ||
} | ||
|
||
type FruitOrder { | ||
name: Ordering | ||
price: Ordering | ||
} | ||
|
||
type PaginationInfo { | ||
limit: Int! | ||
offset: Int! | ||
} | ||
|
||
type FruitOffsetPaginated { | ||
pageInfo: PaginationInfo! | ||
totalCount: Int! | ||
results: [Fruit]! | ||
} | ||
|
||
input OffsetPaginationInput { | ||
offset: Int! = 0 | ||
limit: Int = null | ||
} | ||
|
||
type Query { | ||
fruits( | ||
onlyAvailable: Boolean! = true | ||
pagination: OffsetPaginationInput | ||
order: FruitOrder | ||
): [FruitOffsetPaginated!]! | ||
} | ||
``` | ||
|
||
### Customizing the pagination | ||
|
||
Like other generics, `OffsetPaginated` can be customized to modify its behavior or to | ||
add extra functionality in it. For example, suppose we want to add the average | ||
price of the fruits in the pagination: | ||
|
||
```python title="types.py" | ||
from strawberry_django.pagination import OffsetPaginated | ||
|
||
|
||
@strawberry_django.type(models.Fruit) | ||
class Fruit: | ||
name: auto | ||
price: auto | ||
|
||
|
||
@strawberry.type | ||
class FruitOffsetPaginated(OffsetPaginated[Fruit]): | ||
@strawberry_django.field | ||
def average_price(self) -> Decimal: | ||
if self.queryset is None: | ||
return Decimal(0) | ||
|
||
return self.queryset.aggregate(Avg("price"))["price__avg"] | ||
|
||
@strawberry_django.field | ||
def paginated_average_price(self) -> Decimal: | ||
paginated_queryset = self.get_paginated_queryset() | ||
if paginated_queryset is None: | ||
return Decimal(0) | ||
|
||
return paginated_queryset.aggregate(Avg("price"))["price__avg"] | ||
Comment on lines
+251
to
+264
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. neat! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. obs. this also works for relay connections (I've done some similar stuff in the past like this :) |
||
|
||
|
||
@strawberry.type | ||
class Query: | ||
fruits: FruitOffsetPaginated = strawberry_django.offset_paginated() | ||
``` | ||
|
||
Would produce the following schema: | ||
|
||
```graphql title="schema.graphql" | ||
type Fruit { | ||
name: String! | ||
} | ||
|
||
type PaginationInfo { | ||
limit: Int = null | ||
offset: Int! | ||
} | ||
|
||
type FruitOffsetPaginated { | ||
pageInfo: PaginationInfo! | ||
totalCount: Int! | ||
results: [Fruit]! | ||
averagePrice: Decimal! | ||
paginatedAveragePrice: Decimal! | ||
} | ||
|
||
input OffsetPaginationInput { | ||
offset: Int! = 0 | ||
limit: Int = null | ||
} | ||
|
||
type Query { | ||
fruits(pagination: OffsetPaginationInput): [FruitOffsetPaginated!]! | ||
} | ||
``` | ||
|
||
The following attributes/methods can be accessed in the `OffsetPaginated` class: | ||
|
||
- `queryset`: The queryset original queryset with any filters/ordering applied, | ||
but not paginated yet | ||
- `pagination`: The `OffsetPaginationInput` object, with the `offset` and `limit` for pagination | ||
- `get_total_count()`: Returns the total count of elements in the queryset without pagination | ||
- `get_paginated_queryset()`: Returns the queryset with pagination applied | ||
- `resolve_paginated(queryset, *, info, pagiantion, **kwargs)`: The classmethod that | ||
strawberry-django calls to create an instance of the `OffsetPaginated` class/subclass. | ||
|
||
## Relay pagination | ||
## Cursor pagination (aka Relay style pagination) | ||
|
||
For more complex scenarios, a cursor pagination would be better. For this, | ||
use the [relay integration](./relay.md) to define those. | ||
Another option for pagination is to use a | ||
[relay style cursor pagination](https://graphql.org/learn/pagination). For this, | ||
you can leverage the [relay integration](./relay.md) provided by strawberry | ||
to create a relay connection. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙇♂️