fix(query): apply queryOptions mutator to generated invalidate function (#3190) - #3290
Conversation
…val-labs#3190) Generated invalidateXxx helpers used the raw queryKey function and therefore ignored any keys appended by the user-supplied query.queryOptions mutator (e.g. tenant prefixes), making invalidation miss the cache entries that were actually written. Route the invalidate queryKey through the mutator when one is configured and is not a hook, so the final key matches what the query hook writes. Hook-shaped mutators are skipped because the invalidate helper is a plain async function and cannot call hooks.
…-labs#3190) Add a react-query test config that combines useInvalidate with a queryOptions mutator that prepends a tenant key, plus the mutator fixture itself. The fixture mirrors the minimal repro from the issue so the generated output is exercised end-to-end by the snapshot suite.
…val-labs#3190) Capture the generated client for the new test config so future regressions in the invalidate-key resolution path are caught by the snapshot suite.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (24)
📝 WalkthroughWalkthroughThe PR refactors query-key generation in the code generator to centralize base key construction and apply query-option mutations during invalidation, ensuring Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
Summary
Fixes #3190.
When
query.queryOptionsis configured with a mutator that augmentsthe queryKey (for example, prepending a tenant prefix), generated
invalidateXxxhelpers ignored the mutator and calledqueryClient.invalidateQuerieswith the raw queryKey fromgetXxxQueryKey(). As a result, the keys never matched the keysthat the query hook actually wrote into the cache, and invalidation
silently no-op'd.
This change routes the queryKey passed to
invalidateQueriesthrough the queryOptions mutator when one is configured and is not a
hook. The hook case is intentionally skipped because the generated
invalidateXxxhelpers are plain async functions and cannot legallyinvoke React/Vue hooks.
What changed
packages/query/src/query-generator.ts: derive a singleinvalidateQueryKeyExprthat respects bothquery.queryKey(
queryKeyMutator) andquery.queryOptions(queryOptionsMutator,non-hook only) and use it in the emitted
invalidateXxxbody.tests/mutators/custom-query-options.ts: new fixture that mirrorsthe mutator pattern from the issue
(
{ ...args, queryKey: [tenant, ...args.queryKey] }).tests/configs/react-query.config.ts: newuseInvalidateWithQueryOptionsMutatorcase wiring the fixturethrough the existing petstore spec.
tests/__snapshots__/react-query/use-invalidate-with-query-options-mutator/:generated baseline so future regressions are caught.
Behavior before vs after
Before:
After (with a queryOptions mutator like
(opts) => ({ ...opts, queryKey: ['tenant-abc', ...opts.queryKey] })):The no-mutator output is byte-identical to before (verified against
the existing
use-invalidatesnapshot), so consumers without aqueryOptions mutator are unaffected.
Test plan
bun vitest run(2303 passed; the 3 unrelatedresolve-versionfailures also fail on master with no changes,see notes below).
bun run test:snapshots(3739 passed; 21 new snapshotswritten for the new case).
bun run lint(24 successful).bun run typecheck(12 successful, full recompile aftercache miss).
bun run build(12 successful).tests/generated/react-query/use-invalidatevstests/generated/react-query/use-invalidate-with-query-options-mutatorto confirm only the new case routes through the mutator.
Notes for reviewers
@orval/core/src/utils/resolve-version.test.tsreproduce on aclean master checkout in this environment; they depend on locally
installed
@faker-js/fakerandremedapackages and are nottouched by this PR.
setQueryDataKeyExpr/getQueryDataKeyExpr(they only honorqueryKeyMutator, notqueryOptionsMutator). That is left as a deliberate follow-upto keep this PR scoped to the bug reported in useInvalidate does not contain mutator query keys #3190.
Summary by CodeRabbit
Release Notes
New Features
Refactor
Tests