Skip to content

feat(@tanstack/query): generate mutation keys for mutation options#3858

Open
slmnsh wants to merge 6 commits into
hey-api:mainfrom
slmnsh:main
Open

feat(@tanstack/query): generate mutation keys for mutation options#3858
slmnsh wants to merge 6 commits into
hey-api:mainfrom
slmnsh:main

Conversation

@slmnsh
Copy link
Copy Markdown

@slmnsh slmnsh commented May 9, 2026

Fixes #3845

@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 9, 2026

⚠️ No Changeset found

Latest commit: 708410a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link
Copy Markdown

vercel Bot commented May 9, 2026

@slmnsh is attempting to deploy a commit to the Hey API Team on Vercel.

A member of the Team first needs to authorize it.

@dosubot dosubot Bot added size:XXL This PR changes 1000+ lines, ignoring generated files. feature 🚀 Feature request. labels May 9, 2026
@slmnsh
Copy link
Copy Markdown
Author

slmnsh commented May 9, 2026

@mrlubos Can you check?

@codecov
Copy link
Copy Markdown

codecov Bot commented May 9, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 39.49%. Comparing base (1721aa6) to head (708410a).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3858      +/-   ##
==========================================
- Coverage   39.58%   39.49%   -0.09%     
==========================================
  Files         532      533       +1     
  Lines       19581    19636      +55     
  Branches     5835     5837       +2     
==========================================
+ Hits         7751     7756       +5     
- Misses       9582     9630      +48     
- Partials     2248     2250       +2     
Flag Coverage Δ
unittests 39.49% <ø> (-0.09%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor

@pullfrog pullfrog Bot left a comment

Choose a reason for hiding this comment

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

Important

Consider wiring up mutationKeys.enabled, regenerating examples, adding a changeset, and documenting the new option before merging. The implementation faithfully mirrors the existing queryKey machinery and the snapshots are consistent across all six framework plugins, but the new public config has a dead flag and no test coverage beyond the default cell.

TL;DR — Adds a mutationKeys plugin config and emits a per-operation <name>MutationKey() factory plus a shared MutationKey<TOptions> type and createMutationKey helper for every Tanstack Query plugin (angular/preact/react/solid/svelte/vue). Every mutationOptions(...) result now carries a mutationKey tuple, mirroring how queryOptions already carries a queryKey. Resolves #3845.

Key changes

  • Add shared mutation-key emitters — new mutationKey.ts parallels queryKey.ts with createMutationKeyType, createMutationKeyFunction, and mutationKeyStatement.
  • Wire mutation key into mutationOptions — emit a <op>MutationKey factory and add mutationKey: <op>MutationKey(options) to the returned options object.
  • Add mutationKeys user config — six framework types.ts/config.ts files gain mutationKeys: boolean | NameTransformer | { case, enabled, name, tags } with defaults { enabled: true, name: '{{name}}MutationKey', tags: false }.
  • Regenerate 86 tanstack-query snapshots — every existing fixture under packages/openapi-ts-tests/tanstack-query/v5/__snapshots__/** now contains the new type, helper, and per-operation key factory.

Summary | 92 files | 1 commit | base: main1285e02


mutationKeys.enabled is silently ignored

Before: users could opt out of queryKeys/infiniteQueryKeys only by knowing the existing flags don't actually gate emission either.
After: mutationKeys adds a third user-visible enabled flag with the same problem — mutationKeys: false and { enabled: false } parse correctly through the mappers and resolver, but no consumer ever reads plugin.config.mutationKeys.enabled.

createMutationOptions in packages/openapi-ts/src/plugins/@tanstack/query-core/v5/mutationOptions.ts unconditionally calls mutationKeyStatement(...) and assigns .prop('mutationKey', $(symbolMutationKey).call('options')) regardless of the flag. mutationKey.ts only reads config.tags. A user setting mutationKeys: false still gets the full MutationKey type, the createMutationKey helper, every <op>MutationKey() export, and the mutationKey field on every mutation. Either gate the emissions on plugin.config.mutationKeys.enabled (matching the precedent in packages/openapi-ts/src/plugins/@pinia/colada/queryOptions.ts:48) or drop the flag from the type and resolver. The same sin exists for queryKeys.enabled in this plugin today, but this PR adds a fresh instance.

mutationOptions.ts · mutationKey.ts · react-query/types.ts


Test matrix only covers the default cell

Before: a four-axis config (enabled, name, tags, case) shipped with no behavioural verification.
After: the same is still true — 86 snapshot diffs all use defaults, so three of the four axes are unproven.

A grep for a 3-arg createMutationKey('id', options, [ literal across every snapshot returns zero hits, so the tags: true branch in mutationKey.ts (which emits $.array().elements(...operation.tags) as the third arg) is never exercised. The name-builder scenario in packages/openapi-ts-tests/tanstack-query/v5/test/plugins.test.ts overrides name for infiniteQueryKeys, queryKeys, queryOptions, mutationOptions, etc. but conspicuously skips mutationKeys — the snapshot still emits fooPostMutationKey instead of a custom name. No scenario sets mutationKeys: false (which would have surfaced the dead-flag bug above), and no scenario overrides case. Adding three small scenarios — mutationKeys: false, mutationKeys: { tags: true }, and mutationKeys: { name: '{{name}}F' } — would lock in the contract documented in the JSDoc.

plugins.test.ts · mutationKey.ts


Examples not regenerated, so examples:check will fail in CI

Before: examples/openapi-ts-tanstack-{react,vue,svelte,angular-query-experimental}-query/src/client/@tanstack/*.gen.ts carry the old output.
After: they still do — a grep for MutationKey|mutationKey: under examples/ returns nothing.

Default mutationKeys: { enabled: true } means every regenerated tanstack example will diff. scripts/examples-check.sh calls git diff --quiet after running pnpm examples:generate and exits non-zero on any drift. Run pnpm examples:generate and commit the four updated @tanstack/*.gen.ts files before this PR can land green.

scripts/examples-check.sh · examples/openapi-ts-tanstack-react-query


Missing changeset and docs

Before: .changeset/ only contains the README, changelog.js, and config.json.
After: still no entry for this PR.

This repo uses Changesets — without a .md entry the next release won't bump or surface the new config in CHANGELOG. Add one at minor for @hey-api/openapi-ts. Separately, docs/openapi-ts/plugins/tanstack-query.md has no mention of mutationKey anywhere; the symmetrical Queries section documents queryKeys, the per-operation getXQueryKey() accessor, the tags sub-option, and case/name customization (around lines 282–446). Mirror that structure for mutations, and consider a note in docs/openapi-ts/migrating.md since every existing useMutation({ ...fooMutation() }) consumer now silently inherits a default mutationKey tuple where there used to be undefined.

docs/openapi-ts/plugins/tanstack-query.md · docs/openapi-ts/migrating.md


Smaller notes

  • The exported type MutationKey<TOptions extends Options> shadows @tanstack/react-query's own MutationKey type for any user who re-exports both from the same module. This already happens for QueryKey, so it's consistent rather than novel — but worth a doc callout. (Observable in every regenerated snapshot, e.g. packages/openapi-ts-tests/tanstack-query/v5/__snapshots__/2.0.x/plugins/asClass/@tanstack/react-query.gen.ts:57.)
  • The createMutationKey emission is guarded by plugin.querySymbol({ category: 'utility', resource: 'createMutationKey', tool: plugin.name }) but the MutationKey type is created in the same conditional. If anything else ever emits a MutationKey type symbol first, the guard would skip the helper while leaving a foreign type referenced via referenceSymbol on mutationOptions.ts:39. No current emitter does this; flagging only as a latent fragility shared with queryKey.
  • Positional-arg passing in createMutationKeyLiteral is correct — createMutationKey has signature (id, options?, tags?), distinct from createQueryKey's (id, options?, infinite?, tags?), so passing tagsArray as the third arg is right. Worth a comment if it ever causes confusion during review.

Pullfrog  | Fix all ➔Fix 👍s ➔View workflow run | Using Claude Opus𝕏

return data;
}
},
mutationKey: patchApiVbyApiVersionNoTagMutationKey(options)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This mutationKey line is emitted unconditionally in createMutationOptions (see packages/openapi-ts/src/plugins/@tanstack/query-core/v5/mutationOptions.ts:117). Setting mutationKeys: false or { enabled: false } does not suppress it because nothing reads plugin.config.mutationKeys.enabled. Gate this assignment (and the upstream mutationKeyStatement(...) + createMutationKeyType/createMutationKeyFunction emissions) on the enabled flag, or remove the flag from the public type.

fix(@tanstack/query): mutation option types when it has required
operation data
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 9, 2026

Open in StackBlitz

@hey-api/codegen-core

npm i https://pkg.pr.new/@hey-api/codegen-core@3858

@hey-api/json-schema-ref-parser

npm i https://pkg.pr.new/@hey-api/json-schema-ref-parser@3858

@hey-api/nuxt

npm i https://pkg.pr.new/@hey-api/nuxt@3858

@hey-api/openapi-ts

npm i https://pkg.pr.new/@hey-api/openapi-ts@3858

@hey-api/shared

npm i https://pkg.pr.new/@hey-api/shared@3858

@hey-api/spec-types

npm i https://pkg.pr.new/@hey-api/spec-types@3858

@hey-api/types

npm i https://pkg.pr.new/@hey-api/types@3858

@hey-api/vite-plugin

npm i https://pkg.pr.new/@hey-api/vite-plugin@3858

commit: 708410a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature 🚀 Feature request. size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generate mutationKey when generating MutationOptions

1 participant