-
-
Notifications
You must be signed in to change notification settings - Fork 542
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
Add pagination support #175
Comments
Right now there is no built-in support for pagination (as far as I know). It would definitely be neat to support this though! One way to implement basic pagination would be to use something like this: @strawberry.type
class Query:
@strawberry.field
def numbers(self, info, first: int = None, skip: int = None) -> typing.List[int]:
numbers = range(1000)
return numbers[skip:first] This approach could be abstracted into a decorator for use in multiple queries as well. If you'd like to add support for pagination using this approach, or write some documentation for it, a PR would be welcome :) |
@jaydenwindle thanks for answering 😊 at PyCon DE someone asked for Relay support, maybe we could write some helpers for that, I kinda this something here: https://github.com/strawberry-graphql/strawberry-swapi/blob/master/utils.py I think Graphene has something for arrays already, so we could import that. I don't really have much experience with Relay, so I'd happy to see someone working on this :) |
Are there any simple examples of how to implement a decorator that's meant to be used in combination with Any help would be appreciated, even just a simple example :) Sorry if this is the wrong place to ask. |
@denizdogan what did you try already? I'd like to know mainly because I'd like to make the library as user friendly as possible :) I'll try to come up with an example tomorrow :) |
@patrick91 As I said, I didn't really try very hard :) But I slapped together a simple example which shows where I get stuck: def strawberry_limit(limit=100):
def inner(func):
def replacement(*args, **kwargs):
output = inner(*args, **kwargs)
return output[:limit]
return replacement
return inner
@strawberry.type
class UserType:
uuid: str
@strawberry.type
class Query:
@strawberry.field
@strawberry_limit(limit=10) # does order matter?
def users(self, info) -> List[UserType]:
return User.objects.all() The above style gives me the following error:
So this is when I realize that I have to take care to not lose the type annotations, so I start off by using I tried again using the following: def strawberry_limit(func):
@wraps(func)
def replacement(*args, limit: int = None, **kwargs):
output = func(*args, **kwargs)
if limit is not None:
return output[:limit]
return output
return replacement
@strawberry.type
class Query:
@strawberry_limit # again, does order matter?
@strawberry.field
def users(self, info) -> List[UserType]:
return User.objects.all() The above doesn't raise any uncaught exception, but I do get: {
"data": null,
"errors": [
{
"message": "Type Query must define one or more fields.",
"locations": null,
"path": null
}
]
} Am I close? :) |
oh, when we use This is where we do it: one solution (for now) would be do put that attribute yourself, but it is not ideal. I'll think of something better. What happens if you switch the order in the second case? I'd try myself, but I'm on the phone right now :) |
If I switch the order of the decorators, I get:
Whenever you get the time and strength to give this a go, I'm very interested, but it's not really a pressing issue for me right now, so don't sweat it :) I've been considering that maybe using a decorator for this kind of thing is not really the ideal way to implement something like pagination. I could see us wrapping the return value in some lazy wrapper, as such: def users(self, info) -> Paginated[List[User]]:
return Paginated(info, Users.objects.all(), limit=100) Then the logic would happen in the |
Quick update on this, we recently released support for generic types with the goal of making it easier to create pagination types. They'd work like this:
and you'd use them like this:
This would create the PaginatedUser type for you. We plan to add relay pagination and a simple page pagination as built-in :) |
Any progress on this? |
waiting for this to implement in my project |
@memark not yet, are you interested in a Relay pagination or basic pagination? |
@patrick91 Basic pagination would be just fine! |
@memark cool! maybe I can write a guide, it shouldn't be too difficult to roll your own since we have support for generics. Would that help? I wouldn't commit to an API design for basic pagination just yet 😊 |
@patrick91 Yes, that would be very helpful! |
I'm looking to move to strawberry from graphene but don't want to before pagination is supported. How far off is it? I see basic looks to be done. What about cursor based? |
Cursor based pagination can already be implemented in strawberry, the docs just need to be updated (#1345) |
could someone give me a cursor based pagination example ? thanks |
I'm going to close this as we now have docs for pagination (see #1345) We can always include helpers in future (or in an external library), but I'd discuss those in a new issue :) |
[ x ] Feature Request
[ x ] Documentation
If pagination is supported, be nice to include it in documentations. If there is no plan to support it, it'd be nice to provide an example for developers to role their own. Otherwise, let's keep this issue around to track the pagination feature.
The text was updated successfully, but these errors were encountered: