Skip to content

fix(tanstack-react-query): add prefix support for mutation options#7370

Open
tmkx wants to merge 1 commit into
trpc:mainfrom
tmkx:fix/query-mutation-key-prefix
Open

fix(tanstack-react-query): add prefix support for mutation options#7370
tmkx wants to merge 1 commit into
trpc:mainfrom
tmkx:fix/query-mutation-key-prefix

Conversation

@tmkx
Copy link
Copy Markdown
Contributor

@tmkx tmkx commented May 4, 2026

Closes #7369

🎯 Changes

Add prefix parameter to mutation options to support key prefixing from proxy/context level. This allows mutation key prefixes to be defined at a higher level while still allowing per-mutation overrides.

✅ Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed mutation key generation to properly use key-prefixing context from the proxy, ensuring mutations are keyed consistently even when no explicit prefix is provided.
  • Tests

    • Added test coverage for mutation key generation with proxy key-prefixing defaults.

Add prefix parameter to mutation options to support key prefixing
from proxy/context level. This allows mutation key prefixes to be
defined at a higher level while still allowing per-mutation overrides.
@tmkx tmkx requested a review from a team as a code owner May 4, 2026 16:33
@vercel
Copy link
Copy Markdown

vercel Bot commented May 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
next-prisma-starter Error Error May 4, 2026 4:34pm
og-image Ready Ready Preview, Comment May 4, 2026 4:34pm

Request Review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 4, 2026

📝 Walkthrough

Walkthrough

This PR fixes a bug where mutationOptions().mutationKey was missing the configured key prefix context. The fix threads the proxy's prefix value into the mutation options generator and uses it as a fallback when individual option overrides don't specify their own keyPrefix.

Changes

Key Prefix Threading

Layer / File(s) Summary
Type & Logic Updates
packages/tanstack-react-query/src/internals/mutationOptions.ts
trpcMutationOptions now accepts prefix: string | undefined parameter; mutationKey derivation updated to use opts?.keyPrefix ?? prefix instead of only opts?.keyPrefix.
Integration Wiring
packages/tanstack-react-query/src/internals/createOptionsProxy.ts
The mutationOptions generator now passes the proxy's prefix value into trpcMutationOptions(...) call.
Tests & Verification
packages/tanstack-react-query/test/mutationOptions.test.tsx
New test case verifies that mutation defaults registered via proxied keyPrefix are applied even when opts.keyPrefix is omitted.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • KATT
  • Nick-Lucas

Poem

🐰 A prefix went missing, oh what a shame!
The mutation key lost its proper name,
But now with a thread through the proxy's core,
The keyPrefix flows where it flowed before! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the primary change: adding prefix support to mutation options in the tanstack-react-query integration, which directly addresses the bug described in the linked issue.
Description check ✅ Passed The description includes the issue reference, a clear explanation of changes, and covers the checklist items. However, the documentation update is not yet completed as noted in the checklist.
Linked Issues check ✅ Passed The PR successfully addresses issue #7369 by implementing the prefix parameter in mutation options, ensuring mutationKey includes keyPrefix from proxy/context level, and adds tests to verify the behavior.
Out of Scope Changes check ✅ Passed All changes are scoped to the stated objective: updating mutation options to support prefix parameter in three files with coordinated changes and corresponding tests.

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

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

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/tanstack-react-query/src/internals/mutationOptions.ts (1)

111-114: Runtime fix is correct; the call site's static type for mutationKey remains inconsistent with mutationKey()

The opts?.keyPrefix ?? prefix fallback correctly aligns the runtime mutationKey value with what mutationKey() produces. However, DecorateMutationProcedure<TDef> declares:

mutationOptions: TRPCMutationOptions<TDef>; // TFeatureFlags defaults to DefaultFeatureFlags
mutationKey: () => TRPCMutationKey<TDef['featureFlags']['keyPrefix']>;

Because the second generic of TRPCMutationOptions defaults to DefaultFeatureFlags rather than TDef['featureFlags'], the TypeScript type of mutationOptions().mutationKey is TRPCMutationKey<DefaultFeatureFlags['keyPrefix']>, not TRPCMutationKey<TDef['featureFlags']['keyPrefix']>. This means consumers with a prefix-enabled proxy see a less-precise type from mutationOptions() than from mutationKey(), even though the runtime values are now identical.

The fix is a one-line change in createOptionsProxy.ts, DecorateMutationProcedure<TDef>:

♻️ Proposed type fix
-  mutationOptions: TRPCMutationOptions<TDef>;
+  mutationOptions: TRPCMutationOptions<TDef, TDef['featureFlags']>;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/tanstack-react-query/src/internals/mutationOptions.ts` around lines
111 - 114, DecorateMutationProcedure<TDef>'s mutationOptions type uses
TRPCMutationOptions with the default feature flags, causing
mutationOptions().mutationKey to be typed with DefaultFeatureFlags; update the
declaration in createOptionsProxy.ts so mutationOptions passes
TDef['featureFlags'] as the second generic (e.g. change mutationOptions:
TRPCMutationOptions<TDef> to mutationOptions: TRPCMutationOptions<TDef,
TDef['featureFlags']>) so its mutationKey type matches the existing
mutationKey(): TRPCMutationKey<TDef['featureFlags']['keyPrefix']>.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@packages/tanstack-react-query/src/internals/mutationOptions.ts`:
- Around line 111-114: DecorateMutationProcedure<TDef>'s mutationOptions type
uses TRPCMutationOptions with the default feature flags, causing
mutationOptions().mutationKey to be typed with DefaultFeatureFlags; update the
declaration in createOptionsProxy.ts so mutationOptions passes
TDef['featureFlags'] as the second generic (e.g. change mutationOptions:
TRPCMutationOptions<TDef> to mutationOptions: TRPCMutationOptions<TDef,
TDef['featureFlags']>) so its mutationKey type matches the existing
mutationKey(): TRPCMutationKey<TDef['featureFlags']['keyPrefix']>.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: d87ae420-4ba9-43ae-9b79-418798be8697

📥 Commits

Reviewing files that changed from the base of the PR and between 23c723c and 7e30fde.

📒 Files selected for processing (3)
  • packages/tanstack-react-query/src/internals/createOptionsProxy.ts
  • packages/tanstack-react-query/src/internals/mutationOptions.ts
  • packages/tanstack-react-query/test/mutationOptions.test.tsx

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.

bug(tanstack-react-query): mutationOptions doesn't contain keyPrefix

1 participant