-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[v3]: bidirectional useInfiniteQuery breaks pages order on refetch #1401
Comments
I think it should currently work like this: Initial:
Pages: [4, 5, 6] Refetch:
Pages: [4, 5, 6] Is this behavior causing issues in your case? |
Yes, I think there is an issue regarding |
Think usually only the cursor changes between fetches while keeping filters and sorting the same. Do you maintain some kind of session state on the server? |
No, the server is stateless. The API design I'm trying to implement is as follows:
And the corresponding response may be like:
As you can see below, current Imagine we have the following DB records:
Step 1I do the initial request and receive
Step 2I scroll to the top of the list and call
Here I have two pages of data. Now it's time to refetch the results. With current implementation of
Since I have different cursors pointing to the head and the tail of the list, I expect requests to be made in the same order and with same parameters while refetching as they were originally sent. Sorry for being a bit verbose, just trying to explain the issue better :D |
Thanks for the additional explanation, the more information the better :) On refetch React Query should start with the first page in the list (resulting into This response does seem a bit strange to me:
|
I don't fully understand this logic. I agree that API I have mentioned is not very consistent, but we may switch to another example. Here is the JSON API doc about cursor pagination. As I can see, we face the similar issue here. |
A flow with JSON API would probably look something like this? Data: [
{ "type": "examples", "id": "1" },
{ "type": "examples", "id": "5" },
{ "type": "examples", "id": "7" },
{ "type": "examples", "id": "8" },
{ "type": "examples", "id": "9" }
] Fetch initial: // Request
{}
// Response
{
"data": [
{ "type": "examples", "id": "7", "meta": { "page": { "cursor": "cursor7" } } },
{ "type": "examples", "id": "8", "meta": { "page": { "cursor": "cursor8" } } }
]
} Fetch previous: // Request
getPreviousPageParams(firstPage)
{ "page[before]": "cursor7" }
// Response
{
"data": [
{ "type": "examples", "id": "1", "meta": { "page": { "cursor": "cursor1" } } },
{ "type": "examples", "id": "5", "meta": { "page": { "cursor": "cursor5" } } }
]
} Result: [
{
"data": [
{ "type": "examples", "id": "1", "meta": { "page": { "cursor": "cursor1" } } },
{ "type": "examples", "id": "5", "meta": { "page": { "cursor": "cursor5" } } }
]
},
{
"data": [
{ "type": "examples", "id": "7", "meta": { "page": { "cursor": "cursor7" } } },
{ "type": "examples", "id": "8", "meta": { "page": { "cursor": "cursor8" } } }
]
}
] Refetch request 1: // Request
{ "page[before]": "cursor7" }
// Response
{
"data": [
{ "type": "examples", "id": "1", "meta": { "page": { "cursor": "cursor1" } } },
{ "type": "examples", "id": "5", "meta": { "page": { "cursor": "cursor5" } } }
]
} Refetch request 2: // Request
getNextPageParams(lastPage)
{ "page[after]": "cursor5" }
// Response
{
"data": [
{ "type": "examples", "id": "7", "meta": { "page": { "cursor": "cursor7" } } },
{ "type": "examples", "id": "8", "meta": { "page": { "cursor": "cursor8" } } }
]
} Result: [
{
"data": [
{ "type": "examples", "id": "1", "meta": { "page": { "cursor": "cursor1" } } },
{ "type": "examples", "id": "5", "meta": { "page": { "cursor": "cursor5" } } }
]
},
{
"data": [
{ "type": "examples", "id": "7", "meta": { "page": { "cursor": "cursor7" } } },
{ "type": "examples", "id": "8", "meta": { "page": { "cursor": "cursor8" } } }
]
}
] |
Thank you for the detailed reply. The problem is that I've missed that refetching starts from the first page in the pages array, not from the initial request. I will make sure that this is working for my case and then return with feedback |
No problem! Let me know if you are encountering any problems |
Describe the bug
Bidirectional infinite query doesn't consider direction when refetching.
According to the code,
getNextPageParam
is called for each page while updating a query, no matter how it was originally fetched - by callingfetchNextPage
orfetchPreviousPage
.Refetch works fine for unidirectional queries. For bidirectional queries refetch routine just loads the same amount of pages as was previously loaded, but in
next
direction only.So, if the original request order was:
...
After refetch it will be:
...
Expected behavior
When refetching an infinite query, original direction in which pages were loaded should be considered. As long as backend responds with the same data for the same page params, refetching a query should not break page data order or structure.
Additional context
v 3.2.0
The text was updated successfully, but these errors were encountered: