Skip to content

Commit affe145

Browse files
committed
docs: add mutation-options.md
1 parent b015044 commit affe145

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

docs/.vitepress/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export default defineConfig({
7676
{ text: "useSuspenseQuery", link: "/use-suspense-query" },
7777
{ text: "useInfiniteQuery", link: "/use-infinite-query" },
7878
{ text: "queryOptions", link: "/query-options" },
79+
{ text: "mutationOptions", link: "/mutation-options" },
7980
],
8081
},
8182
{
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: mutationOptions
3+
---
4+
5+
# {{ $frontmatter.title }}
6+
7+
The `mutationOptions` method lets you build type-safe [Mutation Options](https://tanstack.com/query/latest/docs/framework/react/reference/mutationOptions) that plug directly into React Query APIs.
8+
9+
Use it whenever you want to call `$api` mutations but still provide your own `useMutation` (or other mutation consumer) configuration. The helper wires up the correct `mutationKey`, generates a fetcher that calls your OpenAPI endpoint, and preserves the inferred `data` and `error` types.
10+
11+
## Examples
12+
13+
Rewriting the [useMutation example](use-mutation#example) to use `mutationOptions`.
14+
15+
::: code-group
16+
17+
```tsx [src/app.tsx]
18+
import { useMutation } from '@tanstack/react-query';
19+
import { $api } from './api';
20+
21+
export const App = () => {
22+
const updateUser = useMutation(
23+
$api.mutationOptions('patch', '/users/{user_id}', {
24+
onSuccess: (data, variables) => {
25+
console.log('Updated', variables.params?.path?.user_id, data.firstname);
26+
},
27+
})
28+
);
29+
30+
return (
31+
<button
32+
onClick={() =>
33+
updateUser.mutate({
34+
params: { path: { user_id: 5 } },
35+
body: { firstname: 'John' },
36+
})
37+
}
38+
>
39+
Update
40+
</button>
41+
);
42+
};
43+
```
44+
45+
```ts [src/api.ts]
46+
import createFetchClient from 'openapi-fetch';
47+
import createClient from 'openapi-react-query';
48+
import type { paths } from './my-openapi-3-schema'; // generated by openapi-typescript
49+
50+
const fetchClient = createFetchClient<paths>({
51+
baseUrl: 'https://myapi.dev/v1/',
52+
});
53+
export const $api = createClient(fetchClient);
54+
```
55+
56+
:::
57+
58+
::: info Good to Know
59+
60+
`$api.useMutation` uses the same fetcher and key contract as `mutationOptions`. Reach for `mutationOptions` when you need to share mutation configuration across components or call React Query utilities such as `queryClient.mutationDefaults`.
61+
62+
:::
63+
64+
## API
65+
66+
```tsx
67+
const options = $api.mutationOptions(method, path, mutationOptions);
68+
```
69+
70+
**Arguments**
71+
72+
- `method` **(required)**
73+
- HTTP method of the OpenAPI operation.
74+
- Also used as the first element of the mutation key.
75+
- `path` **(required)**
76+
- Pathname of the OpenAPI operation.
77+
- Must be valid for the given method in your generated schema.
78+
- Used as the second element of the mutation key.
79+
- `mutationOptions`
80+
- Optional `UseMutationOptions` for React Query.
81+
- You can set callbacks (`onSuccess`, `onSettled`, …), retry behaviour, and every option except `mutationKey` and `mutationFn` (those are provided for you).
82+
83+
**Returns**
84+
85+
- [Mutation Options](https://tanstack.com/query/latest/docs/framework/react/reference/mutationOptions)
86+
- `mutationKey` is `[method, path]`.
87+
- `mutationFn` is a strongly typed fetcher that calls `openapi-fetch` with your `init` payload.
88+
- `data` and `error` types match the OpenAPI schema, so `variables` inside callbacks are typed as the request shape.

0 commit comments

Comments
 (0)