Skip to content

Query mutation options in core - #10973

Open
benjavicente wants to merge 3 commits into
TanStack:mainfrom
benjavicente:query-mutation-options-in-core
Open

Query mutation options in core#10973
benjavicente wants to merge 3 commits into
TanStack:mainfrom
benjavicente:query-mutation-options-in-core

Conversation

@benjavicente

@benjavicente benjavicente commented Jun 21, 2026

Copy link
Copy Markdown

🎯 Changes

Adds queryOptions and mutationOptions to the core library.

This was requested at #10735 and #9258 discussions. Having those function in core is useful for defining shared query options for a framework agnostic layer, or for server side usage. Specially for using query keys for type-safe interactions with the core imperative API.

Most of the frameworks accept the shared interface from core without changes. The exception is Vue, that require a small change in the interface since MaybeRefOrGetterwasn't compatible. I added runtime test for Vue to demonstrate that it works with the core query options, with the same shape of () => coreOptions as Solid/Angular/Lit/Svelte.

About docs: There is a lot of outdated docs, so I excluded the doc generation in the PR. I didn't add docs mentioning that core has this new functions.

Pending questions:

  • Is CoreQueryOptions and CoreMutationOptions ok names for the interface the new queryOptions and mutationOptions requires? The core library already has QueryOptions and MutationOptions. The new CoreQueryOptions and CoreMutationOptions are the equivalent of QueryOptions and MutationOptions exported by each individual adapter. The new types can't replace the base options alredy defined in core.
  • Should CoreQueryOptions and CoreMutationOptions be excluded from the re-export from core? How? Each adapter exports queryOptions and mutationOptions, so a consumer can't import the implementation defined in core if the consumer hasn't installed @tanstack/query-core directly, but they can consume CoreQueryOptions and CoreMutationOptions.
  • I'm not happy on how the useQuery interface of Vue had to be extended, there is probably something that can be done there to simplify the types.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

Release Notes

  • New Features
    • Added framework-agnostic queryOptions and mutationOptions helpers for reusable query and mutation configurations.
    • Improved type inference when sharing these options across supported frameworks, including query data, mutation data, variables, and mutation inputs.

@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 92bb7245-a330-49a9-bcca-b8be0113341e

📥 Commits

Reviewing files that changed from the base of the PR and between 2215bb0 and 51a48d8.

📒 Files selected for processing (22)
  • .changeset/quiet-cooks-sparkle.md
  • packages/angular-query-experimental/src/__tests__/mutation-options.test-d.ts
  • packages/angular-query-experimental/src/__tests__/query-options.test-d.ts
  • packages/lit-query/src/tests/type-inference.test.ts
  • packages/preact-query/src/__tests__/mutationOptions.test-d.tsx
  • packages/preact-query/src/__tests__/queryOptions.test-d.tsx
  • packages/query-core/src/__tests__/mutationOptions.test-d.tsx
  • packages/query-core/src/__tests__/queryOptions.test-d.tsx
  • packages/query-core/src/index.ts
  • packages/query-core/src/mutationOptions.ts
  • packages/query-core/src/queryOptions.ts
  • packages/react-query/src/__tests__/mutationOptions.test-d.tsx
  • packages/react-query/src/__tests__/queryOptions.test-d.tsx
  • packages/solid-query/src/__tests__/mutationOptions.test-d.tsx
  • packages/solid-query/src/__tests__/queryOptions.test-d.tsx
  • packages/svelte-query/tests/createQuery/createQuery.test-d.ts
  • packages/svelte-query/tests/mutationOptions/mutationOptions.test-d.ts
  • packages/vue-query/src/__tests__/mutationOptions.test-d.ts
  • packages/vue-query/src/__tests__/queryOptions.test-d.ts
  • packages/vue-query/src/__tests__/useQuery.test.ts
  • packages/vue-query/src/useBaseQuery.ts
  • packages/vue-query/src/useQuery.ts
🚧 Files skipped from review as they are similar to previous changes (22)
  • packages/solid-query/src/tests/queryOptions.test-d.tsx
  • packages/angular-query-experimental/src/tests/query-options.test-d.ts
  • packages/vue-query/src/useBaseQuery.ts
  • packages/react-query/src/tests/mutationOptions.test-d.tsx
  • packages/svelte-query/tests/createQuery/createQuery.test-d.ts
  • packages/solid-query/src/tests/mutationOptions.test-d.tsx
  • packages/preact-query/src/tests/queryOptions.test-d.tsx
  • packages/vue-query/src/tests/mutationOptions.test-d.ts
  • packages/preact-query/src/tests/mutationOptions.test-d.tsx
  • packages/query-core/src/index.ts
  • .changeset/quiet-cooks-sparkle.md
  • packages/react-query/src/tests/queryOptions.test-d.tsx
  • packages/query-core/src/tests/mutationOptions.test-d.tsx
  • packages/svelte-query/tests/mutationOptions/mutationOptions.test-d.ts
  • packages/vue-query/src/tests/useQuery.test.ts
  • packages/angular-query-experimental/src/tests/mutation-options.test-d.ts
  • packages/vue-query/src/tests/queryOptions.test-d.ts
  • packages/query-core/src/tests/queryOptions.test-d.tsx
  • packages/query-core/src/mutationOptions.ts
  • packages/lit-query/src/tests/type-inference.test.ts
  • packages/vue-query/src/useQuery.ts
  • packages/query-core/src/queryOptions.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

Adds framework-agnostic queryOptions and mutationOptions helpers to @tanstack/query-core. Exports their types and functions, updates Vue Query to accept core query option factories, and adds type-inference tests across framework adapters.

Changes

Framework-agnostic queryOptions and mutationOptions

Layer / File(s) Summary
Core option helper implementation
packages/query-core/src/queryOptions.ts, packages/query-core/src/mutationOptions.ts, packages/query-core/src/index.ts, .changeset/quiet-cooks-sparkle.md
Adds typed query and mutation helpers with passthrough runtime implementations. Exports the helpers and their core option types. Adds a minor changeset.
Core type-inference tests
packages/query-core/src/__tests__/queryOptions.test-d.tsx, packages/query-core/src/__tests__/mutationOptions.test-d.tsx
Tests query-key tagging, QueryClient data typing, initial-data variants, skipToken, mutationKey handling, and _defaulted exclusion.
Vue Query core option integration
packages/vue-query/src/useBaseQuery.ts, packages/vue-query/src/useQuery.ts
Allows option factories that return CoreQueryOptions. Resolves factory results before creating defaulted query options.
Vue Query compatibility tests
packages/vue-query/src/__tests__/queryOptions.test-d.ts, packages/vue-query/src/__tests__/mutationOptions.test-d.ts, packages/vue-query/src/__tests__/useQuery.test.ts
Tests core option inference, tagged query keys, direct-option rejection, reactive updates, and mutation option compatibility.
Framework adapter type-inference tests
packages/angular-query-experimental/src/__tests__/..., packages/lit-query/src/tests/..., packages/preact-query/src/__tests__/..., packages/react-query/src/__tests__/..., packages/solid-query/src/__tests__/..., packages/svelte-query/tests/...
Verifies core query and mutation options across Angular, Lit, Preact, React, Solid, and Svelte adapters.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Merge Risk: ⚪ Minimal · up to 51a48

This change adds shared query and mutation option helpers with associated type and framework coverage; no actionable merge-blocking risk remains beyond normal checks and review.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant CoreHelpers
  participant FrameworkAdapter
  participant QueryObserver
  Caller->>CoreHelpers: create queryOptions or mutationOptions
  CoreHelpers-->>Caller: typed options with preserved inference
  Caller->>FrameworkAdapter: pass core options
  FrameworkAdapter->>QueryObserver: create observer with options
  QueryObserver-->>Caller: typed query or mutation result
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 21 files. (1 skipped: 1 unsupported.) Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The description explains the core helpers, framework impact, testing, release impact, and open design questions; required checklist items are complete.
Title check ✅ Passed The title clearly identifies the main change: adding query and mutation options to the core library.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

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

export { queryOptions } from './queryOptions'
export type { CoreQueryOptions } from './queryOptions'
export { mutationOptions } from './mutationOptions'
export type { CoreMutationOptions } from './mutationOptions'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

since adapters re-export the core and then export their own things, they won’t have access to the queryOptions from the core, right? is that on purpose?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes. I don't think someone wanting to define shared query options for multiple frameworks would import from a framework specific package.

Comment on lines +58 to +61
const { data } = useQuery(options)
expectTypeOf(data).toEqualTypeOf<
{ id: string; title: string } | undefined
>()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

with this, what’s the difference between useQuery(queryOptions) and useQuery(coreQueryOption) ?

in other words, if they are compatible, why would e.g. the react adapter need a separate queryOptions and not just re-export and let users use the one from the core?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

queryOptions from each library could extend the coreQueryOption, like with deferStream in Solid or RefOrGetter pattern in Vue.

@benjavicente

Copy link
Copy Markdown
Author

I avoided changing the types of the adapters to reuse the new types in core to keep the changes of this PR small (only test changes for most adapters). The code query options could be something that adapters completely reuse.

@TkDodo

TkDodo commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

The code query options could be something that adapters completely reuse.

I guess you mean re-use on type-level, because at runtime, they all don’t do anything?

@benjavicente

Copy link
Copy Markdown
Author

The code query options could be something that adapters completely reuse.

I guess you mean re-use on type-level, because at runtime, they all don’t do anything?

Yes

@TkDodo

TkDodo commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

okay, let’s do it 🔥 . can you fix the conflicts ?

@benjavicente
benjavicente force-pushed the query-mutation-options-in-core branch from ff39c7b to 51a48d8 Compare August 22, 2026 14:30
@coderabbitai

coderabbitai Bot commented Aug 22, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

@benjavicente
benjavicente requested a review from TkDodo August 22, 2026 15:54
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.

2 participants