-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
pagination with previous/next #466
Comments
@faassen Thanks for the question! You're definitely not missing something: Relay was designed to make infinite scroll easy and hence the API for this is relatively simple. However, windowed pagination hasn't been a common requirement so it currently requires more work (as you've seen). That said, we're open to suggestions and contributions to make this use-case simpler for developers. Do you need to be able to jump to arbitrary pages in the list (e.g. would you have links to non-adjacent pages such as "...[2] [3] [4] ... [10]"), or would the user always page through from the beginning (via previous/next links)? |
@josephsavona Although not entirely connected with initial question above i jumped in because i see this going in the direction i need. So im adding my question. What you asked: is exactly what i need. How can i pass it nicely to the component (i need total count to be able to render page numbers)? |
@GrzegorzKaczan Instead of putting the count on
|
ha, thought so, thx. |
This was is not driven by any concrete need, just my experimentation I wanted to report on my adventures first to give an impression of the My main point is that it would be better if either:
This may mean you actually want both: a forward type, a backward type and a Now into the details:
You may be right in that in a bidirectional UI people generally want
|
Oh, another potential pagination feature that is needed sometimes is "jump to start" and "jump to end". |
Thanks for posting this @faassen. I prototyped a version of my current app with traditional window-based paging. Realized that's not what Relay is after, and your post and this thread really helped distill all the issues for me. Much appreciated. I don't have hard requirements for how paging will work, so I will re-work the paging to be Relay style. But if I had a hard requirement from a boss or client, this would be a bummer! |
It seems to me that implementing pagination through a connection with both a previous and a next button is currently rather difficult to implement. (I want the previous/next buttons to be hidden or disabled when there's no previous or next page). I finallly managed to implement it, it's pretty hairy:
My initial attempt was to define a query along these lines:
The idea was to set the variables to something like this when clicking previous:
and when going next, do this:
(incidentally,
pageInfo.startCursor
andpageInfo.endCursor
don't appear here: https://facebook.github.io/relay/graphql/connections.htm Should they?)This failed because the compiler complains when I include those variables at the same time.
So then I split up the code to please the compiler:
Note that I noticed that Relay when you pass in a variable of
null
in these cases does omit the variables, so is the compiler currently too strict?In the React code I then have to decide whether to display
previousStories
ornextStories
depending on whether there is any content there.Now I can paginate previously and next. I do get a ton of warnings I have no clue what to do about:
This may be related to #247, but it's a different warning...
I'm incidentally confused about the behavior of
@include
as even when$wantPrevious
is false I still get a full-fledged 'previousStories' object, and I have to checkedges.length
to see whether it's really empty. I guess@include
only applies to that field and other fields are loaded but empty? It's unclear to me what it's supposed to do from the GraphQl spec...Now I get to the next problem: the previous button shouldn't do anything (or be greyed out or hidden) if you are at the start of the content, and the next button shouldn't do anything (or be greyed out or hidden) if you're at the end. But how does one accomplish this? At first sight you'd think
hasPreviousPage
andhasNextPage
inpageInfo
can do it, but they can't, ashasPreviousPage
is alwaysfalse
if you're paginating forward and vice versa. That's not what is needed for this use case, but it is at the very least rather misleading. Here are these fields that given their names you think are perfect for this use case, but it's a lot more subtle than that.So I came up with something like this:
but I was defeated again:
previousStory
gives back 1 story, even if I'm at the start of the array: the last item! This is because I actually pass in$before
asnull
in the initial query, which means nobefore
parameter is passed in at all, and this gives the last entry. The same problem occurs fornextStory
.So back to
pageInfo
again. Can I somehow get the information I want out of it after all? It turns out I can, but it's hairy:so, if we just retrieved the previous page (
wantPrevious
), we know thathasPreviousPage
has useful information, and we know there must be a next batch as we just came from there.if we just retrieved the next page, then we know that
hasNextPage
has useful information, and we knowthat there must be a previous page, unless we're at the first page which we can detect by checking whether the
after
variable is the initialnull
, as at the first page we never have a previous page anyway.This appears to work, but it requires at least two things that are rather non-obvious:
wantPrevious
/wantNext
variables, and logic to check which we got in React component to show the stuff we really did get.So in conclusion, again: am I missing something obvious here in which documentation may be able to help)? Or is this an unsupported use case I should've stayed away from? Perhaps a Relay Connection only support expanding web pages with "load more", not a batch display. If so, documentation could point this out - in fact the whole API currently is misleading with its 'hasNextPage' naming too. Perhaps for next/previous batching some other connection-like facility (with limit/offset?) would be more useful.
The text was updated successfully, but these errors were encountered: