-
-
Notifications
You must be signed in to change notification settings - Fork 132
feat: New Paginated generic to be used as a wrapped for paginated results #642
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
Changes from 1 commit
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 |
---|---|---|
|
@@ -28,7 +28,7 @@ type Fruit { | |
|
||
input OffsetPaginationInput { | ||
offset: Int! = 0 | ||
limit: Int! = -1 | ||
limit: Int = null | ||
} | ||
|
||
type Query { | ||
|
@@ -107,7 +107,7 @@ class Fruit: | |
|
||
@strawberry.type | ||
class Query: | ||
fruits: OffsetPaginated[Fruit] = strawberry_django.field() | ||
fruits: OffsetPaginated[Fruit] = strawberry_django.offset_paginated() | ||
``` | ||
|
||
Would produce the following schema: | ||
|
@@ -118,7 +118,7 @@ type Fruit { | |
} | ||
|
||
type PaginationInfo { | ||
limit: Int! | ||
limit: Int = null | ||
offset: Int! | ||
} | ||
|
||
|
@@ -130,7 +130,7 @@ type FruitOffsetPaginated { | |
|
||
input OffsetPaginationInput { | ||
offset: Int! = 0 | ||
limit: Int! = -1 | ||
limit: Int = null | ||
} | ||
|
||
type Query { | ||
|
@@ -159,6 +159,77 @@ query { | |
> 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) | ||
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 | ||
|
@@ -195,7 +266,7 @@ class FruitOffsetPaginated(OffsetPaginated[Fruit]): | |
|
||
@strawberry.type | ||
class Query: | ||
fruits: FruitOffsetPaginated = strawberry_django.field() | ||
fruits: FruitOffsetPaginated = strawberry_django.offset_paginated() | ||
``` | ||
|
||
Would produce the following schema: | ||
|
@@ -206,7 +277,7 @@ type Fruit { | |
} | ||
|
||
type PaginationInfo { | ||
limit: Int! | ||
limit: Int = null | ||
offset: Int! | ||
} | ||
|
||
|
@@ -220,7 +291,7 @@ type FruitOffsetPaginated { | |
|
||
input OffsetPaginationInput { | ||
offset: Int! = 0 | ||
limit: Int! = -1 | ||
limit: Int = null | ||
} | ||
|
||
type Query { | ||
|
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.
Typo here @bellini666 but this is really gorgeous, overall, I'm loving this feature.