Skip to content

Commit a509096

Browse files
authored
Merge pull request #53 from guendev/yyy
refactor(useMutation): remove `onOptimistic` hook and related documentation
2 parents a3af1b5 + e08ac78 commit a509096

File tree

4 files changed

+7
-73
lines changed

4 files changed

+7
-73
lines changed

.changeset/fifty-months-cough.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@vue3-apollo/core": patch
3+
"@vue3-apollo/nuxt": patch
4+
---
5+
6+
refactor(useMutation): remove `onOptimistic` hook and related documentation

packages/core/src/composables/useMutation.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,6 @@ export function useMutation<
9393

9494
const onDone = createEventHook<[TData, HookContext]>()
9595
const onError = createEventHook<[ErrorLike, HookContext]>()
96-
/**
97-
* Event hook that fires when an optimistic response is provided for the mutation.
98-
* This is useful to react immediately to UI updates that occur before the
99-
* actual network response is received.
100-
*/
101-
const onOptimistic = createEventHook<[TData, HookContext]>()
10296

10397
useApolloTracking({
10498
state: loading,
@@ -114,16 +108,6 @@ export function useMutation<
114108
called.value = true
115109

116110
try {
117-
// If an optimistic response was provided, notify listeners.
118-
// This event is intended to reflect the immediate optimistic update phase.
119-
const optimisticSource = mutateOptions?.optimisticResponse ?? options?.optimisticResponse
120-
if (optimisticSource) {
121-
const optimisticData = typeof optimisticSource === 'function'
122-
? (optimisticSource as (vars?: TVariables) => TData)(variables)
123-
: optimisticSource
124-
void onOptimistic.trigger(optimisticData as TData, { client })
125-
}
126-
127111
const result = await client.mutate<TData, TVariables, TCache>({
128112
mutation: toValue(document),
129113
variables: variables as TVariables ?? undefined,
@@ -243,20 +227,6 @@ export function useMutation<
243227
*/
244228
onError: onError.on,
245229

246-
/**
247-
* Event hook that fires when an optimistic response is provided for the mutation.
248-
* Useful to react to immediate UI updates before the real response arrives.
249-
*
250-
* @example
251-
* ```ts
252-
* onOptimistic((optimisticData) => {
253-
* // e.g. set a temporary flag or log optimistic changes
254-
* console.log('Optimistic update:', optimisticData)
255-
* })
256-
* ```
257-
*/
258-
onOptimistic: onOptimistic.on,
259-
260230
/**
261231
* Reset the mutation state to initial values.
262232
* Clears data, error, loading, and called flags.

packages/docs/composables/useMutation.md

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const {
2424
loading,
2525
mutate,
2626
onDone,
27-
onOptimistic,
2827
} = useMutation(CREATE_POST)
2928

3029
async function submit() {
@@ -40,10 +39,6 @@ async function submit() {
4039
onDone((payload) => {
4140
console.log('Created post:', payload)
4241
})
43-
44-
onOptimistic((payload) => {
45-
console.log('Optimistic create:', payload)
46-
})
4742
```
4843

4944
## API
@@ -57,7 +52,6 @@ const {
5752
called,
5853
onDone,
5954
onError,
60-
onOptimistic,
6155
reset,
6256
} = useMutation(document, options)
6357
```
@@ -94,25 +88,6 @@ const {
9488
context.client.clearStore()
9589
})
9690
```
97-
- **`onOptimistic((optimisticData, context) => {})`** – Fires when an `optimisticResponse` is provided for the mutation (either via base `options` or per‑call `mutateOptions`). Use this to react immediately to optimistic UI updates before the real network response arrives. The `context` contains the active Apollo client.
98-
```ts
99-
const { mutate, onOptimistic } = useMutation(CREATE_POST, {
100-
optimisticResponse: (vars) => ({
101-
createPost: {
102-
__typename: 'Post',
103-
id: -1,
104-
...vars
105-
}
106-
})
107-
})
108-
109-
onOptimistic((optimisticData, context) => {
110-
// Example: mark a list item as "pending" using a temporary id
111-
console.log('Optimistic:', optimisticData)
112-
// You may also access the client if you need manual cache tweaks
113-
// context.client.cache.modify(...)
114-
})
115-
```
11691
- **`reset()`** – clear `data`, `error`, `loading`, and `called` back to initial state.
11792

11893
## Options
@@ -125,16 +100,3 @@ Pass as the second argument to `useMutation`.
125100
- **Apollo mutate options** (except `mutation` & `variables`, which are provided): `refetchQueries`, `awaitRefetchQueries`, `optimisticResponse`, `update`, `context`, etc.
126101
- When `optimisticResponse` is provided, the `onOptimistic` hook will fire with the optimistic data.
127102
- **`clientId`** (from `UseBaseOption`) – target a specific Apollo client if you registered multiple.
128-
129-
## Why both `onOptimistic` and `onDone`?
130-
131-
- `onOptimistic` runs as soon as an `optimisticResponse` is applied. Use it to:
132-
- provide instant feedback (e.g., add a temporary item, disable buttons)
133-
- annotate UI as pending (temporary ids, spinners)
134-
- optionally tweak cache immediately if needed
135-
- `onDone` runs after the real network response succeeds. Use it to:
136-
- finalize UI with authoritative data from the server
137-
- perform navigation, toasts, or follow‑up actions that should only happen on actual success
138-
- reconcile or replace any optimistic placeholders (e.g., swap temp id with real id)
139-
140-
These two hooks let you clearly separate the optimistic phase from the confirmed success phase, leading to predictable UI flows.

packages/web/src/App.vue

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@ const { error, result } = useQuery(PostsDocument, vars, {
2626
2727
const title = ref('')
2828
29-
const { loading, mutate, onDone: onUpdatedPost, onOptimistic } = useMutation(UpdatePostDocument)
29+
const { loading, mutate, onDone: onUpdatedPost } = useMutation(UpdatePostDocument)
3030
3131
onUpdatedPost((data) => {
3232
console.warn('onUpdatedPost', data)
3333
})
3434
35-
onOptimistic((data) => {
36-
console.warn('onOptimistic', data)
37-
})
38-
3935
const { data } = useFragment(computed(() => PostDetailFragmentDoc), {
4036
from: {
4137
__typename: 'Post',

0 commit comments

Comments
 (0)