feat(query-core): add opt-in ignoreUndefinedInKeys filter option#10808
feat(query-core): add opt-in ignoreUndefinedInKeys filter option#10808webdevelopersrinu wants to merge 3 commits into
ignoreUndefinedInKeys filter option#10808Conversation
Fixes inconsistency where `hashKey` strips `undefined` object properties
(via `JSON.stringify`) but `partialMatchKey` keeps them, causing
`invalidateQueries({ queryKey: [{ ...filter: undefined }] })` to miss
cached entries that have a concrete value for the same property.
The new option is opt-in (default `false`) — no change to existing
behavior — so it is fully backward-compatible. Threaded through both
`QueryFilters` and `MutationFilters`.
Adds unit tests for `partialMatchKey` covering the broken case, the
nested-object case, and array preservation. Adds an end-to-end
`invalidateQueries` integration test demonstrating the fix at the
public API surface.
Refs TanStack#3741
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughAdds an opt-in ignoreUndefinedInKeys flag to QueryFilters/MutationFilters, forwards it through matchQuery/matchMutation, updates partialMatchKey to ignore undefined object properties during partial matches, and adds unit and integration tests. ChangesUndefined property handling in query key matching
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@packages/query-core/src/utils.ts`:
- Around line 270-282: The new type PartialMatchKeyOptions is declared in utils
(symbol: PartialMatchKeyOptions) but not re-exported from the package public
entrypoint; update the module's public exports to include and export this type
(i.e., add PartialMatchKeyOptions to the existing re-export list alongside
MutationFilters, QueryFilters, SkipToken, and Updater) so downstream code can
import it from the package root.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8a47e77a-67e8-4f35-8f70-c897a5b9e792
📒 Files selected for processing (3)
packages/query-core/src/__tests__/queryClient.test.tsxpackages/query-core/src/__tests__/utils.test.tsxpackages/query-core/src/utils.ts
Summary
Adds an opt-in
ignoreUndefinedInKeysoption toQueryFiltersandMutationFiltersto resolve the long-standing inconsistency betweenhashKey(stripsundefinedviaJSON.stringify) andpartialMatchKey(keepsundefined).Refs #3741
Background
When a query key contains an object property with value
undefined, the cache stores it under a hash that excludes that property — butinvalidateQueriescannot match it back when a sibling cache entry has a concrete value for the same property. See #3741 and the closed #4616 for full history.@TkDodo previously raised the concern that there is no clean non-breaking fix. This PR addresses that by making the new behavior fully opt-in (default
false→ zero behavior change for existing users).API
```ts
queryClient.invalidateQueries({
queryKey: [{ entity: 'todos', filter: undefined }],
ignoreUndefinedInKeys: true,
})
```
Also available on
MutationFilters.Design notes
undefinedinside arrays is preserved.undefinedat every level.PartialMatchKeyOptionstype for downstream adapter use.QueryClientdefaults — kept the surface small. Happy to add a global default in a follow-up.Tests
utils.test.tsx(broken-case, concrete-value-differ, array preservation, nested recursion).queryClient.test.tsxexercisinginvalidateQueries.All 531 query-core tests pass locally.
Summary by CodeRabbit
New Features
Tests