Skip to content

fix(query-core): preserve refetch targets in resetQueries when predicate depends on mutable state#10894

Open
DevLikhith5 wants to merge 1 commit into
TanStack:mainfrom
DevLikhith5:fix/resetQueries-refetch-state-dependent-predicate-10705
Open

fix(query-core): preserve refetch targets in resetQueries when predicate depends on mutable state#10894
DevLikhith5 wants to merge 1 commit into
TanStack:mainfrom
DevLikhith5:fix/resetQueries-refetch-state-dependent-predicate-10705

Conversation

@DevLikhith5
Copy link
Copy Markdown

@DevLikhith5 DevLikhith5 commented Jun 6, 2026

Closes #10705.

🐛 The Bug

resetQueries(filters) calls query.reset() on matched queries before passing the same filters to refetchQueries. When the filter predicate reads mutable state like query.state.status === "error", query.reset() changes status from "error""pending". The subsequent refetchQueries.findAll(filters) finds zero matches and skips the refetch entirely.

// This leaves errored queries stuck in "pending" forever:
queryClient.resetQueries({
  predicate: (query) => query.state.status === "error",
})

🔧 The Fix

Save the matched query hashes before the reset loop, then pass an identity-based predicate override to refetchQueries (last-wins after the ...filters spread):

const matched = queryCache.findAll(filters)
const matchedHashes = new Set(matched.map((query) => query.queryHash))
matched.forEach((query) => { query.reset() })
return this.refetchQueries(
  { type: "active", ...filters, predicate: (q) => matchedHashes.has(q.queryHash) },
  options,
)

This ensures refetchQueries finds the exact same queries regardless of state changes.

✅ Verification

  • All 536 existing query-core tests pass
  • New test confirms state-dependent predicates (both status === "success" and status === "error") correctly trigger refetch
  • TypeScript clean (no type errors)
  • Prettier formatted
  • Changeset included (@tanstack/query-core patch)

Note: PR #10704 by @alZyad independently identified the same root cause and fix approach. This PR supersedes it with a cleaner commit, more thorough test coverage, and a changeset.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed resetQueries to correctly refetch queries when using predicates that depend on mutable query state (such as status or data). State mutations during the reset operation are now properly handled to ensure accurate predicate matching.

…ate depends on mutable state

resetQueries(filters) calls query.reset() on matched queries before passing the same filters to refetchQueries. When the filter predicate reads mutable state (e.g. query.state.status === 'error'), reset() mutates that state, causing refetchQueries.findAll() to match zero queries and skip the refetch entirely.

The fix saves the matched query hashes before resetting, then uses an identity-based predicate override (last-wins after the ...filters spread) to ensure refetchQueries finds the exact same queries regardless of state changes.

Closes TanStack#10705
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 6, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 5d297e8d-adf0-413c-8a8c-dea53a2db767

📥 Commits

Reviewing files that changed from the base of the PR and between ba7fbc4 and 7c4aa8e.

📒 Files selected for processing (3)
  • .changeset/quiet-ducks-begin.md
  • packages/query-core/src/__tests__/queryClient.test.tsx
  • packages/query-core/src/queryClient.ts

📝 Walkthrough

Walkthrough

This PR fixes resetQueries to correctly refetch queries when the filter predicate depends on mutable state. The implementation captures matched queries before reset, stores their hashes, and refetches only those specific queries. A test validates the fix works with state-dependent predicates, and a changeset documents the patch release.

Changes

resetQueries Refetch Fix

Layer / File(s) Summary
Capture and refetch matched queries
packages/query-core/src/queryClient.ts, packages/query-core/src/__tests__/queryClient.test.tsx, .changeset/quiet-ducks-begin.md
QueryClient.resetQueries now captures matched query hashes before reset, then refetches only those specific queries via a predicate, preventing state mutations (e.g., status change from error to pending) from breaking the refetch. Test validates state-dependent predicates work correctly. Changeset documents the patch fix.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • TkDodo

Poem

🐰 Ah, the queries now reset with grace,
Capturing hashes before they race,
State mutations can't fool them anymore—
They refetch what was matched before! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main fix: preserving refetch targets in resetQueries when the predicate depends on mutable state (status).
Description check ✅ Passed The description comprehensively explains the bug, fix approach, and verification steps, following the template structure with relevant sections.
Linked Issues check ✅ Passed The PR fully addresses issue #10705 by implementing the exact fix proposed: capturing matched query hashes before reset and using them in refetchQueries.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the resetQueries refetch bug: changeset declaration, test coverage, and implementation in queryClient.ts.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

resetQueries method does not trigger refetch on errored queries

1 participant