updateQuery merging different data when changing query while fetchMore is not yet finished
See original GitHub issueHi, I’ve read https://github.com/apollographql/apollo-client/issues/2499 and I don’t know if it this issue I’m having is related or not.
I am having this issue where prev
and result
on updateQuery(prev, result)
having different context on the data to merge. It happens when I changed query (changing search term) while fetchMore is not yet finished, resulting in items with different query being merged.
How to reproduce the issue: Case:
- I am currently on search result page with term
pillow
already have some data, and calling fetchMore. - Before fetchMore is done, I quickly changed the term with
iphone
. - The
iphone
search query data are merged inupdateQuery
withpillow
fetchMore result.
Sorry, I am currently unable to provide the repository to reproduce this. I will provide it later if it’s necessary.
Intended outcome:
I think the intended outcome is obvious, the pillow
fetchMore result should be merged according to its search term (fetched earlier in step 1).
This is the code I’ve been using:
fetchMore: (nextPage = 0, listview, additional_params) => {
return data.fetchMore({
variables: {
page: nextPage,
per_page: listview ? LIST_VIEW_PER_PAGE : GRID_VIEW_PER_PAGE,
additional_params,
},
updateQuery: (prev, res) => {
console.log('prev: ', prev);
console.log('res: ', res);
if (!res.fetchMoreResult) {
return prev;
}
const newData = res.fetchMoreResult.search_results_product;
return {
...prev,
search_results_product: {
...prev.search_results_product,
total_data: newData.total_data,
items: [...prev.search_results_product.items, ...newData.items],
},
};
},
});
},
Actual outcome:
The data from pillow
fetchMore are merged in updateQuery with iphone
, resulting in wrong search result displayed.
Weirdly, the res.variables
in updateQuery(prev, res)
showing that the query is for iphone
instead of the actual query for fetchMore, which is pillow
.
Version
- apollo-client@2.1.0
Issue Analytics
- State:
- Created 6 years ago
- Reactions:14
- Comments:11 (2 by maintainers)
Top GitHub Comments
I’m experiencing the same behaviour. When the variables for the query are changed while fetchMore is in flight, the results of “fetchMore” (for the old variables) will be merged with the results of the query with current variables.
Example: You have a list with 2 tabs (changing tabs will change the “category”-variable for the query). So fetchMore is in progess while having tab 1 selected (category 1). Now the users switches to tab 2 and later the fetchMore results for tab 1 will be appended to tab 2.
The updateQuery-Function should provide the “previosResults” for the initial variables so we can merge accordingly.
Any update on the status of this issue?