feat(query-core): forward caller-provided meta into the invalidate action payload#10540
feat(query-core): forward caller-provided meta into the invalidate action payload#10540maastrich wants to merge 1 commit intoTanStack:mainfrom
Conversation
…cache action Adds an optional `meta` field to `InvalidateOptions` that flows through to the `invalidate` action payload visible in `queryCache.subscribe`, enabling structured observability and echo-suppression without out-of-band bookkeeping. Closes TanStack#10539 (discussion) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughAdds support for passing Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 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.
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)
297-311:⚠️ Potential issue | 🟠 MajorStrip
metabefore forwarding options torefetchQueries.
options.metais intended for the invalidate action, but the fulloptionsobject is also passed intorefetchQueries. That leaks the same object intoquery.fetch(..., fetchOptions), wherefetchOptions.metais interpreted as fetch metadata and can updatefetchMeta/ fetch events with invalidation metadata.Proposed fix
): Promise<void> { return notifyManager.batch(() => { + const { meta, ...refetchOptions } = options + this.#queryCache.findAll(filters).forEach((query) => { - query.invalidate(options.meta) + query.invalidate(meta) }) if (filters?.refetchType === 'none') { return Promise.resolve() } @@ ...filters, type: filters?.refetchType ?? filters?.type ?? 'active', }, - options, + refetchOptions, ) }) }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/query-core/src/queryClient.ts` around lines 297 - 311, The invalidate loop correctly uses options.meta but you must avoid forwarding that same meta into refetchQueries; when calling this.refetchQueries(...) strip out meta from the options passed (e.g., clone options then delete or omit options.meta) so invalidate receives meta but the refetch call receives options without meta; locate the call to notifyManager.batch -> this.#queryCache.findAll(...).forEach(query => query.invalidate(options.meta)) and change the refetchQueries invocation to pass a copy of options with meta removed before spreading into the second argument.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@packages/query-core/src/queryClient.ts`:
- Around line 297-311: The invalidate loop correctly uses options.meta but you
must avoid forwarding that same meta into refetchQueries; when calling
this.refetchQueries(...) strip out meta from the options passed (e.g., clone
options then delete or omit options.meta) so invalidate receives meta but the
refetch call receives options without meta; locate the call to
notifyManager.batch -> this.#queryCache.findAll(...).forEach(query =>
query.invalidate(options.meta)) and change the refetchQueries invocation to pass
a copy of options with meta removed before spreading into the second argument.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6a2f6275-2903-4509-b84a-b2a7e73fffc8
📒 Files selected for processing (7)
.changeset/invalidate-queries-meta.mddocs/framework/react/guides/query-invalidation.mddocs/reference/QueryClient.mdpackages/query-core/src/__tests__/queryClient.test.tsxpackages/query-core/src/query.tspackages/query-core/src/queryClient.tspackages/query-core/src/types.ts
Summary
Implements the proposal from discussion #10539.
meta?: QueryMetatoInvalidateOptions(second arg ofinvalidateQueries)query.invalidate(meta)into the dispatchedInvalidateActionevent.action.metaavailable inqueryCache.subscribefor structured observability and echo-suppressionUsage
Notes
metais optional and defaults toundefined. No existing call site is affected.options.metais distinct fromfilters.meta(which filters queries by their meta).isInvalidatedguard is preserved: if a query is already invalidated, the dispatch is skipped and the meta from subsequent calls is silently dropped. This is consistent with current behavior and can be revisited separately.query-broadcast-client-experimentaldoes not broadcastinvalidateactions today, so no changes are needed there.Checklist
query.ts,queryClient.ts,types.ts)queryClient.test.tsxdocs/reference/QueryClient.md)docs/framework/react/guides/query-invalidation.md) — other framework guides mirror it viaref:minorfor@tanstack/query-core)Closes discussion: #10539
Summary by CodeRabbit
Release Notes
New Features
metaoption that is forwarded to cache subscriber events for observation.Documentation
Tests