Skip to content

Commit 556e837

Browse files
authored
Add context as callback to useMutation mutate function (#12959)
1 parent 1c82eaf commit 556e837

File tree

5 files changed

+505
-3
lines changed

5 files changed

+505
-3
lines changed

.api-reports/api-report-react.api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,9 @@ export namespace useMutation {
639639
}
640640
]) => Promise<ApolloClient.MutateResult<MaybeMasked<TData>>>;
641641
// (undocumented)
642-
export type MutationFunctionOptions<TData = unknown, TVariables extends OperationVariables = OperationVariables, TCache extends ApolloCache = ApolloCache> = Options<TData, TVariables, TCache>;
642+
export type MutationFunctionOptions<TData = unknown, TVariables extends OperationVariables = OperationVariables, TCache extends ApolloCache = ApolloCache> = Options<TData, TVariables, TCache> & {
643+
context?: DefaultContext | ((hookContext: DefaultContext | undefined) => DefaultContext);
644+
};
643645
// (undocumented)
644646
export interface Options<TData = unknown, TVariables extends OperationVariables = OperationVariables, TCache extends ApolloCache = ApolloCache, TConfiguredVariables extends Partial<TVariables> = Partial<TVariables>> {
645647
awaitRefetchQueries?: boolean;

.changeset/big-flowers-move.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
"@apollo/client": minor
3+
---
4+
5+
You can now provide a callback function as the `context` option on the `mutate` function returned by `useMutation`. The callback function is called with the value of the `context` option provided to the `useMutation` hook. This is useful if you'd like to merge the context object provided to the `useMutation` hook with a value provided to the `mutate` function.
6+
7+
8+
```ts
9+
function MyComponent() {
10+
const [mutate, result] = useMutation(MUTATION, {
11+
context: { foo: true }
12+
});
13+
14+
async function runMutation() {
15+
await mutate({
16+
// sends context as { foo: true, bar: true }
17+
context: (hookContext) => ({ ...hookContext, bar: true })
18+
});
19+
}
20+
21+
// ...
22+
}
23+
```

docs/source/data/mutations.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,31 @@ When using TypeScript, you might see an error related to a missing variable when
156156

157157
</Note>
158158

159+
<MinVersion version="4.1">
160+
161+
##### Merging `context` from the hook and `mutate` function
162+
163+
</MinVersion>
164+
165+
Due to option precedence, `context` provided to the `mutate` function overrides `context` provided to the `useMutation` hook. In some cases, you might want to merge the `context` value provided to the hook with a value available at the time you execute the `mutate` function.
166+
167+
You accomplish this by using a callback function for the `context` option provided to the `mutate` function. The callback function is called with the `context` value provided to the hook, allowing you to merge them together.
168+
169+
```ts
170+
addTodo({
171+
context: (hookContext) => ({
172+
...hookContext,
173+
myCustomValue: true,
174+
}),
175+
});
176+
```
177+
178+
<Note>
179+
180+
Your callback function is not required to merge the context values together. The `context` value sent to the link chain is the value returned from the function which makes it possible to change the `context` value in any way you wish, such as omitting a property from the hook context.
181+
182+
</Note>
183+
159184
### Tracking mutation status
160185

161186
In addition to a mutate function, the `useMutation` hook returns an object that represents the current state of the mutation's execution. The fields of this object include booleans that indicate whether the mutate function has been `called` and whether the mutation's result is currently `loading`.

0 commit comments

Comments
 (0)