Skip to content

feat(react-query): add mutationOptions #4

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/framework/react/reference/mutationOptions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
Copy link

Choose a reason for hiding this comment

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

Documentation Issue: The documentation in docs/framework/react/reference/mutationOptions.md is minimal compared to the implementation complexity. This could make it harder for developers to understand the full capabilities and proper usage patterns of this utility.

Consider enhancing the documentation with:

  1. Type parameter explanations
  2. Return value details
  3. Usage examples showing different scenarios
  4. Relationship to useMutation and how they work together

For example:

---
id: mutationOptions
title: mutationOptions
---

`mutationOptions` allows you to define mutation options separately from the `useMutation` hook, enabling better type inference and code organization.

```tsx
const options = mutationOptions({
 mutationKey: ['todos'],
 mutationFn: (newTodo) => axios.post('/todos', newTodo),
 onSuccess: (data) => {
   // data is properly typed based on mutationFn return type
 }
})

// Later use with useMutation
const mutation = useMutation(options)

Type Parameters

  • TMutationFnData - The type the mutation function returns
  • TError - The error type
  • TData - The input data type
  • TMutationKey - The mutation key type

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).
18 changes: 18 additions & 0 deletions docs/framework/react/typescript.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'] })
},
})
}
```

## 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
Expand Down
51 changes: 51 additions & 0 deletions packages/react-query/src/__tests__/mutationOptions.test-d.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expectTypeOf, it } from 'vitest'
import { dataTagSymbol } from '@tanstack/query-core'
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>()
},
})
})

it('should tag the mutationKey with the result type of the MutationFn', () => {
const { mutationKey } = mutationOptions({
mutationKey: ['key'],
mutationFn: () => Promise.resolve(5),
})

expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<number>()
})

it('should tag the mutationKey with unknown if there is no mutationFn', () => {
const { mutationKey } = mutationOptions({
mutationKey: ['key'],
})

expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<unknown>()
})

it('should tag the mutationKey with the result type of the MutationFn if onSuccess is used', () => {
const { mutationKey } = mutationOptions({
mutationKey: ['key'],
mutationFn: () => Promise.resolve(5),
onSuccess: () => {},
})

expectTypeOf(mutationKey[dataTagSymbol]).toEqualTypeOf<number>()
})
})
14 changes: 14 additions & 0 deletions packages/react-query/src/__tests__/mutationOptions.test.tsx
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)
})
})
105 changes: 105 additions & 0 deletions packages/react-query/src/mutationOptions.ts
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
>
}

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:
| 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>
}

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) {
return options
}
Loading