fix: resetQueries predicate filter no longer matches after reset#11012
fix: resetQueries predicate filter no longer matches after reset#11012highoncomputers wants to merge 1 commit into
Conversation
When resetQueries is called with a predicate filter (e.g. filtering by query state status), calling query.reset() changes the query state before the refetch step. The predicate would then no longer match the reset queries, causing them to never be refetched. Fix by capturing the matching query hashes before resetting, then refetching those exact queries. Closes TanStack#10705 Co-authored-by: TkDodo <TkDodo@users.noreply.github.com>
📝 WalkthroughWalkthroughQueryClient.resetQueries now captures matched queries and their queryHash values prior to calling reset(), then refetches using a predicate that matches those captured hashes instead of reapplying the original filters to refetchQueries, ensuring queries whose state changed during reset are still refetched. ChangesresetQueries refetch fix
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant QueryClient
participant QueryCache
participant Query
Caller->>QueryClient: resetQueries(filters, options)
QueryClient->>QueryCache: findAll(filters)
QueryCache-->>QueryClient: matching queries
QueryClient->>Query: reset() for each query
QueryClient->>QueryClient: build Set of queryHash values
QueryClient->>QueryClient: refetchQueries({type: 'active', predicate: hash in Set})
QueryClient-->>Caller: Promise resolved
Related issues: Suggested labels: bug, query-core Suggested reviewers: TkDodo 🐰
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/query-core/src/queryClient.ts (1)
256-283: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winHardcoded
type: 'active'drops caller'stypefilter override.The previous implementation refetched via
{ type: 'active', ...filters }, so a caller-suppliedfilters.type(e.g.'inactive'or'all') would override the default. The new implementation always passestype: 'active'with no way to override it, so queries reset viaresetQueries({ ..., type: 'all' })ortype: 'inactive'will no longer be refetched even though they match the hash set built from the original filters.🔧 Proposed fix to preserve the caller's `type` override
return this.refetchQueries( { - type: 'active', + type: filters?.type ?? 'active', predicate: (query) => queryHashes.has(query.queryHash), }, options, )🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/query-core/src/queryClient.ts` around lines 256 - 283, The resetQueries implementation is hardcoding refetches to active queries and ignoring any caller-provided type override in QueryFilters. Update the resetQueries method in QueryClient so the refetch criteria preserve the original filters’ type behavior instead of always forcing type: 'active'; use the existing filters/queryHashes flow to keep the caller’s requested type (such as all or inactive) when calling refetchQueries.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@packages/query-core/src/queryClient.ts`:
- Around line 256-283: The resetQueries implementation is hardcoding refetches
to active queries and ignoring any caller-provided type override in
QueryFilters. Update the resetQueries method in QueryClient so the refetch
criteria preserve the original filters’ type behavior instead of always forcing
type: 'active'; use the existing filters/queryHashes flow to keep the caller’s
requested type (such as all or inactive) when calling refetchQueries.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 891e5bfb-6291-4350-9885-84a0842e1c3f
📒 Files selected for processing (1)
packages/query-core/src/queryClient.ts
When
resetQueriesis called with a predicate filter (e.g.predicate: (query) => query.state.status === 'error'), callingquery.reset()changes the query state before the refetch step. The predicate would then no longer match the reset queries, causing them to never be refetched and remain stuck inpending.Fix: Capture matching query hashes before resetting the queries, then refetch those exact queries using the captured hashes rather than re-applying the original predicate.
Closes #10705
Summary by CodeRabbit