T
TanStack2w ago
flat-fuchsia

Question about targeted refetch behavior in on-demand with queryCollection

Hi! I have a question about TanStack DB's refetching behavior when using a query collection in on-demand mode after updating a specific item (I attached a POC script to reproduce this). Scenario: - Collection in on-demand mode, starts empty (with 20 items in the server) - Live query in frontend asks for "10 items ordered by createdAt desc" → returns items with id 1-10 - Frontend updates item 6 (which is in our loaded set) using
collection.update('6', (draft) =>{draft.completed = true})
collection.update('6', (draft) =>{draft.completed = true})
. - onUpdate runs a fetch request to server, and handler returns { refetch: true } (default behavior) Expected behavior (naive): - Refetch should call queryFn with WHERE id=6 in its ctx.meta.loadSubsetOptions to only update that item Actual behavior observed: - Refetch re-runs the ORIGINAL query: ORDER BY createdAt DESC LIMIT 10 - This fetches 10 items from the API - Since item 6 is no longer in the "last 10 ", it gets removed from the collection entirely (when the new queryFunc call returns array with item ids [11-20], it overrides all the old data) Questions: 1. Is this the intended behavior? 2. I assumed that by using on-demand, it would push only targeted predicates to the queryFunc after an update happens, (e.g., WHERE id=X) instead of re-running the full query. Is there a mistake in my current code setup? 3. When the query func re-runs with the same predicates, (order by createdAt desc, limit 10), is it normal that old items just get whipped out? Why this matters: - With large datasets, re-fetching 10 items instead of 1 = 10x more network traffic. And targeted loadSubsetOptions that of only changed items after an update is usually what's expected. - More critically, since the initial query is what's refetched, the old item gets kicked out of the collection.
3 Replies
extended-salmon
extended-salmon2w ago
Yeah this is a bug. Please file an issue on GitHub Thanks for the detailed write-up! 🙏
flat-fuchsia
flat-fuchsiaOP2w ago
On it 👍
flat-fuchsia
flat-fuchsiaOP2w ago
GitHub
Bug: Mutation refetch in on-demand mode re-runs entire query instea...
Describe the bug When using a Query Collection in on-demand mode, mutations trigger a refetch that re-runs the entire original query (with orderBy + limit) instead of targeting the specific item th...

Did you find this page helpful?