Skip to content

fix(query-core): refetch matched queries in resetQueries after reset#10891

Open
chatman-media wants to merge 1 commit into
TanStack:mainfrom
chatman-media:fix/reset-queries-refetch-errored
Open

fix(query-core): refetch matched queries in resetQueries after reset#10891
chatman-media wants to merge 1 commit into
TanStack:mainfrom
chatman-media:fix/reset-queries-refetch-errored

Conversation

@chatman-media
Copy link
Copy Markdown

@chatman-media chatman-media commented Jun 6, 2026

Closes #10705.

resetQueries resets the matched queries first and then calls refetchQueries with the same filters:

queryCache.findAll(filters).forEach((query) => query.reset())
return this.refetchQueries({ type: 'active', ...filters }, options)

But query.reset() mutates each query (status errorpending, etc.). So when filters is state-dependent — e.g. predicate: (q) => q.state.status === 'error' — the now-reset queries no longer match, and refetchQueries refetches nothing. They stay stuck in pending forever.

Fix: snapshot the matched queries before resetting them, then refetch exactly those (active ones) by identity — so re-filtering on mutated state can't drop them.

Test: added a query-core test — an active query that errors, then resetQueries({ predicate: (q) => q.state.status === 'error' }) must reset and refetch it (→ success), not leave it pending.

🤖 Generated with Claude Code

Summary by CodeRabbit

Bug Fixes

  • Fixed resetQueries when using state-dependent predicates. Previously, filtering queries by their current state (e.g., error status) and then calling resetQueries would reset the queries but fail to refetch the originally selected ones, as the state changes during reset. The fix captures matching queries before reset to ensure the correct queries are refetched.

resetQueries reset the matched queries first (mutating their status, e.g.
error -> pending) and then re-ran the same filter to select refetch targets.
State-dependent filters (e.g. predicate: status === 'error') no longer matched
the now-changed queries, so they were never refetched and stayed pending.
Snapshot the matched queries before resetting and refetch those directly.

Closes TanStack#10705
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 6, 2026

Linter diff in the way? Review this PR in Change Stack to focus on meaningful changes and expand context only when needed.

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: 80ebbba4-87b0-43ec-839c-9f59326d5655

📥 Commits

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

📒 Files selected for processing (3)
  • .changeset/reset-queries-refetch-matched.md
  • packages/query-core/src/__tests__/queryClient.test.tsx
  • packages/query-core/src/queryClient.ts

📝 Walkthrough

Walkthrough

QueryClient.resetQueries is fixed to snapshot matched queries before resetting them, then refetch only those instances. This ensures state-dependent predicates work correctly when filtering for queries in error status, which previously remained pending because the reset mutated the state before refetching applied the filter.

Changes

resetQueries state-dependent filter fix

Layer / File(s) Summary
Fix resetQueries to snapshot matched queries before reset
packages/query-core/src/queryClient.ts, .changeset/reset-queries-refetch-matched.md
The implementation now captures query instances in a Set before calling query.reset(), then passes a predicate to refetchQueries that refetches only those captured instances. This ensures state-dependent filters match queries before their state is mutated.
Regression test for state-dependent predicate refetch
packages/query-core/src/__tests__/queryClient.test.tsx
New test creates a query that errors on first fetch, then calls resetQueries with a predicate filtering for query.state.status === 'error'. It verifies the query is refetched and succeeds after the reset operation.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A rabbit hops through queries with glee,
Snapshotting states before they decree,
No more lost refetch on errors so red,
The matched ones stay fresh, no longer misled! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and accurately summarizes the main fix: refetching matched queries in resetQueries after reset, which directly addresses the core issue.
Description check ✅ Passed The description provides a clear problem statement, explanation of the root cause, the fix approach, and test coverage. It follows the template structure with changes and checklist items, though checklist items are not explicitly marked as done.
Linked Issues check ✅ Passed The PR directly addresses issue #10705 by fixing the resetQueries behavior where state-dependent filtering fails due to state mutations before refetch. The implementation snapshots queries before reset and refetches them by identity, preventing filter mismatches.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the resetQueries issue: a changeset entry, a test case validating the fix, and the core implementation change in QueryClient. No unrelated modifications are present.
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