-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat(react-query): add mutationOptions #8960
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TkDodo
merged 43 commits into
TanStack:main
from
Ubinquitous:feature/react-query-mutation-options
Jul 9, 2025
Merged
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
596896d
feat(react-query): add mutationOptions
Ubinquitous ff15e5d
test(react-query): add DataTag test case
Ubinquitous ea54b58
Merge branch 'main' into feature/react-query-mutation-options
TkDodo 2972edd
fix(react-query): remove unnecessary types from mutation
Ubinquitous 08a5026
fix(react-query): remove unncessary type overload
Ubinquitous f3b74c0
Merge branch 'main' into feature/react-query-mutation-options
manudeli a4560d3
Merge branch 'main' into feature/react-query-mutation-options
manudeli 6889638
chore(react-query): add mutationOptions to barrel file
Ubinquitous b844dee
Merge branch 'main' into feature/react-query-mutation-options
manudeli e61227d
Merge branch 'main' into feature/react-query-mutation-options
manudeli 33d3e9f
fix(react-query): fix test eslint issue
Ubinquitous fd7b9f9
docs(react-query): add more examples
Ubinquitous 6ee8c76
Merge branch 'main' into feature/react-query-mutation-options
manudeli 299a19f
Merge branch 'main' into feature/react-query-mutation-options
manudeli 2622902
Merge branch 'main' into feature/react-query-mutation-options
TkDodo 9ded37d
test(react-query): add more test cases
Ubinquitous 48d867b
chore(react-query): Change mutaitonKey to required
Ubinquitous 05f4fc0
fix(react-query): fix test code type error
Ubinquitous b202d6e
test(react-query): add testcase when used with other mutation util
Ubinquitous 167fb8c
fix(react-query): fix error test code and avoid use deprecateed method
Ubinquitous 2b85c72
fix(react-query): fix error test code and avoid use deprecateed method
Ubinquitous df3545a
fix(react-query): fix import detect error
Ubinquitous 08769ac
fix(react-query): fix import detect error
Ubinquitous 36a8af1
fix(react-query): add function overload
Ubinquitous 22f5ed2
test(react-query): fix mutation options test code
Ubinquitous 1661565
Update docs/framework/react/typescript.md
TkDodo d7587ba
Merge branch 'main' into feature/react-query-mutation-options
TkDodo 7ee1f5a
Update docs/framework/react/reference/mutationOptions.md
TkDodo d682a88
Update docs/framework/react/typescript.md
manudeli f32a7e0
fix: update mutationOptions type definition to allow optional mutatio…
manudeli 0729167
ci: apply automated fixes
autofix-ci[bot] 8730872
Merge branch 'main' into feature/react-query-mutation-options
manudeli ac2d73f
Merge branch 'main' into feature/react-query-mutation-options
TkDodo 8929c6a
Merge branch 'main' into feature/react-query-mutation-options
manudeli 4adc529
Merge branch 'main' into feature/react-query-mutation-options
manudeli 98e3662
Merge branch 'main' into feature/react-query-mutation-options
manudeli 2f7eb30
chore: add Nick-Lucas as co-author
manudeli 144dcd5
Merge branch 'main' into feature/react-query-mutation-options
manudeli fe35750
Merge branch 'main' into feature/react-query-mutation-options
manudeli 49f5c39
test(react-query): fix test code and add used with useMutation withou…
Ubinquitous 5b73933
Merge branch 'main' into feature/react-query-mutation-options
TkDodo bc48f1a
Update packages/react-query/src/__tests__/mutationOptions.test-d.tsx
TkDodo 357269f
docs: add mutationOptions to sidebar config
TkDodo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
id: mutationOptions | ||
title: mutationOptions | ||
--- | ||
|
||
```tsx | ||
mutationOptions({ | ||
mutationFn, | ||
...options, | ||
}) | ||
``` | ||
|
||
**Options** | ||
|
||
You can generally pass everything to `mutationOptions` that you can also pass to [`useMutation`](./useMutation.md). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -239,6 +239,24 @@ const data = queryClient.getQueryData<Group[]>(['groups']) | |||||||||||||||||||||||||||||||||||||||||||||||||
[//]: # 'TypingQueryOptions' | ||||||||||||||||||||||||||||||||||||||||||||||||||
[//]: # 'Materials' | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
## Typing Mutation Options | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Similarly to `queryOptions`, you can use `mutationOptions` to extract mutation options into a separate function: | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
```ts | ||||||||||||||||||||||||||||||||||||||||||||||||||
function useGroupPostMutation() { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const queryClient = useQueryClient() | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return mutationOptions({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
mutationKey: ['groups'], | ||||||||||||||||||||||||||||||||||||||||||||||||||
mutationFn: executeGroups, | ||||||||||||||||||||||||||||||||||||||||||||||||||
onSuccess: () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
queryClient.invalidateQueries({ queryKey: ['posts'] }) | ||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth highlighting in the documentation that mutationOptions can be reused across different interfaces—such as useMutation, useIsMutating, and queryClient.isMutating.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
## Further Reading | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
For tips and tricks around type inference, have a look at [React Query and TypeScript](./community/tkdodos-blog.md#6-react-query-and-typescript) from | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
23 changes: 23 additions & 0 deletions
23
packages/react-query/src/__tests__/mutationOptions.test-d.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, expectTypeOf, it } from 'vitest' | ||
import { mutationOptions } from '../mutationOptions' | ||
|
||
describe('mutationOptions', () => { | ||
it('should not allow excess properties', () => { | ||
return mutationOptions({ | ||
mutationFn: () => Promise.resolve(5), | ||
mutationKey: ['key'], | ||
// @ts-expect-error this is a good error, because onMutates does not exist! | ||
onMutates: 1000, | ||
}) | ||
}) | ||
|
||
it('should infer types for callbacks', () => { | ||
return mutationOptions({ | ||
mutationFn: () => Promise.resolve(5), | ||
mutationKey: ['key'], | ||
onSuccess: (data) => { | ||
expectTypeOf(data).toEqualTypeOf<number>() | ||
}, | ||
}) | ||
}) | ||
}) | ||
TkDodo marked this conversation as resolved.
Show resolved
Hide resolved
|
14 changes: 14 additions & 0 deletions
14
packages/react-query/src/__tests__/mutationOptions.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { mutationOptions } from '../mutationOptions' | ||
import type { UseMutationOptions } from '../types' | ||
|
||
describe('mutationOptions', () => { | ||
it('should return the object received as a parameter without any modification.', () => { | ||
const object: UseMutationOptions = { | ||
mutationKey: ['key'], | ||
mutationFn: () => Promise.resolve(5), | ||
} as const | ||
|
||
expect(mutationOptions(object)).toStrictEqual(object) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import type { | ||
DataTag, | ||
DefaultError, | ||
InitialDataFunction, | ||
MutationFunction, | ||
OmitKeyof, | ||
SkipToken, | ||
} from '@tanstack/query-core' | ||
import type { UseMutationOptions } from './types' | ||
|
||
export type UndefinedInitialDataOptions< | ||
TMutationFnData = unknown, | ||
TError = DefaultError, | ||
TData = void, | ||
TMutationKey = unknown, | ||
> = UseMutationOptions<TMutationFnData, TError, TData, TMutationKey> & { | ||
initialData?: | ||
| undefined | ||
| InitialDataFunction<NonUndefinedGuard<TMutationFnData>> | ||
| NonUndefinedGuard<TMutationFnData> | ||
} | ||
|
||
export type UnusedSkipTokenOptions< | ||
TMutationFnData = unknown, | ||
TError = DefaultError, | ||
TData = void, | ||
TMutationKey = unknown, | ||
> = OmitKeyof< | ||
UseMutationOptions<TMutationFnData, TError, TData, TMutationKey>, | ||
'mutationFn' | ||
> & { | ||
mutationFn?: Exclude< | ||
UseMutationOptions< | ||
TMutationFnData, | ||
TError, | ||
TData, | ||
TMutationKey | ||
>['mutationFn'], | ||
SkipToken | undefined | ||
TkDodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
> | ||
} | ||
|
||
type NonUndefinedGuard<T> = T extends undefined ? never : T | ||
|
||
export type DefinedInitialDataOptions< | ||
TMutationFnData = unknown, | ||
TError = DefaultError, | ||
TData = void, | ||
TMutationKey = unknown, | ||
> = Omit< | ||
UseMutationOptions<TMutationFnData, TError, TData, TMutationKey>, | ||
'mutationFn' | ||
> & { | ||
initialData: | ||
TkDodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| NonUndefinedGuard<TMutationFnData> | ||
| (() => NonUndefinedGuard<TMutationFnData>) | ||
mutationFn?: MutationFunction<TMutationFnData, TMutationKey> | ||
} | ||
|
||
export function mutationOptions< | ||
TMutationFnData = unknown, | ||
TError = DefaultError, | ||
TData = void, | ||
TMutationKey = unknown, | ||
>( | ||
options: DefinedInitialDataOptions< | ||
TMutationFnData, | ||
TError, | ||
TData, | ||
TMutationKey | ||
>, | ||
): DefinedInitialDataOptions<TMutationFnData, TError, TData, TMutationKey> & { | ||
mutationKey: DataTag<TMutationKey, TMutationFnData, TError> | ||
TkDodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
export function mutationOptions< | ||
TMutationFnData = unknown, | ||
TError = DefaultError, | ||
TData = void, | ||
TMutationKey = unknown, | ||
>( | ||
options: UnusedSkipTokenOptions<TMutationFnData, TError, TData, TMutationKey>, | ||
): UnusedSkipTokenOptions<TMutationFnData, TError, TData, TMutationKey> & { | ||
mutationKey: DataTag<TMutationKey, TMutationFnData, TError> | ||
} | ||
|
||
export function mutationOptions< | ||
TMutationFnData = unknown, | ||
TError = DefaultError, | ||
TData = void, | ||
TMutationKey = unknown, | ||
>( | ||
options: UndefinedInitialDataOptions< | ||
TMutationFnData, | ||
TError, | ||
TData, | ||
TMutationKey | ||
>, | ||
): UndefinedInitialDataOptions<TMutationFnData, TError, TData, TMutationKey> & { | ||
mutationKey: DataTag<TMutationKey, TMutationFnData, TError> | ||
} | ||
|
||
export function mutationOptions(options: unknown) { | ||
TkDodo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return options | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.