Skip to content

Commit cb89bfe

Browse files
committed
chore: rename to onMutateResult
1 parent 1c5a820 commit cb89bfe

File tree

9 files changed

+144
-107
lines changed

9 files changed

+144
-107
lines changed

docs/framework/angular/guides/mutations.md

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,17 @@ mutation = injectMutation(() => ({
9393
onMutate: (variables, context) => {
9494
// A mutation is about to happen!
9595

96-
// Optionally return a scope containing data to use when for example rolling back
96+
// Optionally return a result containing data to use when for example rolling back
9797
return { id: 1 }
9898
},
99-
onError: (error, variables, scope, context) => {
99+
onError: (error, variables, onMutateResult, context) => {
100100
// An error happened!
101101
console.log(`rolling back optimistic update with id ${scope.id}`)
102102
},
103-
onSuccess: (data, variables, scope, context) => {
103+
onSuccess: (data, variables, onMutateResult, context) => {
104104
// Boom baby!
105105
},
106-
onSettled: (data, error, variables, scope, context) => {
106+
onSettled: (data, error, variables, onMutateResult, context) => {
107107
// Error or success... doesn't matter!
108108
},
109109
}))
@@ -130,25 +130,25 @@ mutation = injectMutation(() => ({
130130
```ts
131131
mutation = injectMutation(() => ({
132132
mutationFn: addTodo,
133-
onSuccess: (data, variables, scope, context) => {
133+
onSuccess: (data, variables, onMutateResult, context) => {
134134
// I will fire first
135135
},
136-
onError: (error, variables, scope, context) => {
136+
onError: (error, variables, onMutateResult, context) => {
137137
// I will fire first
138138
},
139-
onSettled: (data, error, variables, scope, context) => {
139+
onSettled: (data, error, variables, onMutateResult, context) => {
140140
// I will fire first
141141
},
142142
}))
143143

144144
mutation.mutate(todo, {
145-
onSuccess: (data, variables, scope, context) => {
145+
onSuccess: (data, variables, onMutateResult, context) => {
146146
// I will fire second!
147147
},
148-
onError: (error, variables, scope, context) => {
148+
onError: (error, variables, onMutateResult, context) => {
149149
// I will fire second!
150150
},
151-
onSettled: (data, error, variables, scope, context) => {
151+
onSettled: (data, error, variables, onMutateResult, context) => {
152152
// I will fire second!
153153
},
154154
})
@@ -161,15 +161,15 @@ mutation.mutate(todo, {
161161
export class Example {
162162
mutation = injectMutation(() => ({
163163
mutationFn: addTodo,
164-
onSuccess: (data, variables, scope, context) => {
164+
onSuccess: (data, variables, onMutateResult, context) => {
165165
// Will be called 3 times
166166
},
167167
}))
168168

169169
doMutations() {
170170
;['Todo 1', 'Todo 2', 'Todo 3'].forEach((todo) => {
171171
this.mutation.mutate(todo, {
172-
onSuccess: (data, variables, scope, context) => {
172+
onSuccess: (data, variables, onMutateResult, context) => {
173173
// Will execute only once, for the last mutation (Todo 3),
174174
// regardless which mutation resolves first
175175
},
@@ -227,16 +227,18 @@ queryClient.setMutationDefaults(['addTodo'], {
227227
// Return scope with the optimistic todo
228228
return { optimisticTodo }
229229
},
230-
onSuccess: (result, variables, scope, context) => {
230+
onSuccess: (result, variables, onMutateResult, context) => {
231231
// Replace optimistic todo in the todos list with the result
232232
context.client.setQueryData(['todos'], (old) =>
233-
old.map((todo) => (todo.id === scope.optimisticTodo.id ? result : todo)),
233+
old.map((todo) =>
234+
todo.id === onMutateResult.optimisticTodo.id ? result : todo,
235+
),
234236
)
235237
},
236-
onError: (error, variables, scope, context) => {
238+
onError: (error, variables, onMutateResult, context) => {
237239
// Remove optimistic todo from the todos list
238240
context.client.setQueryData(['todos'], (old) =>
239-
old.filter((todo) => todo.id !== scope.optimisticTodo.id),
241+
old.filter((todo) => todo.id !== onMutateResult.optimisticTodo.id),
240242
)
241243
},
242244
retry: 3,

docs/framework/angular/guides/optimistic-updates.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,16 @@ updateTodo = injectMutation(() => ({
9898
// Optimistically update to the new value
9999
context.client.setQueryData(['todos'], (old) => [...old, newTodo])
100100

101-
// Return a scope object with the snapshotted value
101+
// Return a result object with the snapshotted value
102102
return { previousTodos }
103103
},
104104
// If the mutation fails,
105-
// use the scope returned from onMutate to roll back
106-
onError: (err, newTodo, scope, context) => {
107-
context.client.setQueryData(['todos'], scope.previousTodos)
105+
// use the result returned from onMutate to roll back
106+
onError: (err, newTodo, onMutateResult, context) => {
107+
context.client.setQueryData(['todos'], onMutateResult.previousTodos)
108108
},
109109
// Always refetch after error or success:
110-
onSettled: (data, error, variables, scope, context) => {
110+
onSettled: (data, error, variables, onMutateResult, context) => {
111111
context.client.invalidateQueries({ queryKey: ['todos'] })
112112
},
113113
}))
@@ -133,15 +133,18 @@ updateTodo = injectMutation(() => ({
133133
// Optimistically update to the new value
134134
context.client.setQueryData(['todos', newTodo.id], newTodo)
135135

136-
// Return a scope with the previous and new todo
136+
// Return a result with the previous and new todo
137137
return { previousTodo, newTodo }
138138
},
139-
// If the mutation fails, use the scope we returned above
140-
onError: (err, newTodo, scope, context) => {
141-
context.client.setQueryData(['todos', scope.newTodo.id], scope.previousTodo)
139+
// If the mutation fails, use the result we returned above
140+
onError: (err, newTodo, onMutateResult, context) => {
141+
context.client.setQueryData(
142+
['todos', onMutateResult.newTodo.id],
143+
onMutateResult.previousTodo,
144+
)
142145
},
143146
// Always refetch after error or success:
144-
onSettled: (newTodo, error, variables, scope, context) => {
147+
onSettled: (newTodo, error, variables, onMutateResult, context) => {
145148
context.client.invalidateQueries({ queryKey: ['todos', newTodo.id] })
146149
},
147150
}))
@@ -154,7 +157,7 @@ updateTodo = injectMutation(() => ({
154157
injectMutation({
155158
mutationFn: updateTodo,
156159
// ...
157-
onSettled: (newTodo, error, variables, scope) => {
160+
onSettled: (newTodo, error, variables, onMutateResult, context) => {
158161
if (error) {
159162
// do something
160163
}

docs/framework/react/guides/mutations.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ useMutation({
149149
// Optionally return a scope containing data to use when for example rolling back
150150
return { id: 1 }
151151
},
152-
onError: (error, variables, scope, context) => {
152+
onError: (error, variables, onMutateResult, context) => {
153153
// An error happened!
154-
console.log(`rolling back optimistic update with id ${scope.id}`)
154+
console.log(`rolling back optimistic update with id ${onMutateResult.id}`)
155155
},
156-
onSuccess: (data, variables, scope, context) => {
156+
onSuccess: (data, variables, onMutateResult, context) => {
157157
// Boom baby!
158158
},
159-
onSettled: (data, error, variables, scope, context) => {
159+
onSettled: (data, error, variables, onMutateResult, context) => {
160160
// Error or success... doesn't matter!
161161
},
162162
})
@@ -189,25 +189,25 @@ You might find that you want to **trigger additional callbacks** beyond the ones
189189
```tsx
190190
useMutation({
191191
mutationFn: addTodo,
192-
onSuccess: (data, variables, scope, context) => {
192+
onSuccess: (data, variables, onMutateResult, context) => {
193193
// I will fire first
194194
},
195-
onError: (error, variables, scope, context) => {
195+
onError: (error, variables, onMutateResult, context) => {
196196
// I will fire first
197197
},
198-
onSettled: (data, error, variables, scope, context) => {
198+
onSettled: (data, error, variables, onMutateResult, context) => {
199199
// I will fire first
200200
},
201201
})
202202

203203
mutate(todo, {
204-
onSuccess: (data, variables, scope, context) => {
204+
onSuccess: (data, variables, onMutateResult, context) => {
205205
// I will fire second!
206206
},
207-
onError: (error, variables, scope, context) => {
207+
onError: (error, variables, onMutateResult, context) => {
208208
// I will fire second!
209209
},
210-
onSettled: (data, error, variables, scope, context) => {
210+
onSettled: (data, error, variables, onMutateResult, context) => {
211211
// I will fire second!
212212
},
213213
})
@@ -226,15 +226,15 @@ There is a slight difference in handling `onSuccess`, `onError` and `onSettled`
226226
```tsx
227227
useMutation({
228228
mutationFn: addTodo,
229-
onSuccess: (data, variables, scope, context) => {
229+
onSuccess: (data, variables, onMutateResult, context) => {
230230
// Will be called 3 times
231231
},
232232
})
233233

234234
const todos = ['Todo 1', 'Todo 2', 'Todo 3']
235235
todos.forEach((todo) => {
236236
mutate(todo, {
237-
onSuccess: (data, variables, scope, context) => {
237+
onSuccess: (data, variables, onMutateResult, context) => {
238238
// Will execute only once, for the last mutation (Todo 3),
239239
// regardless which mutation resolves first
240240
},
@@ -304,19 +304,21 @@ queryClient.setMutationDefaults(['addTodo'], {
304304
// Add optimistic todo to todos list
305305
context.client.setQueryData(['todos'], (old) => [...old, optimisticTodo])
306306

307-
// Return scope with the optimistic todo
307+
// Return onMutateResult with the optimistic todo
308308
return { optimisticTodo }
309309
},
310-
onSuccess: (result, variables, scope, context) => {
310+
onSuccess: (result, variables, onMutateResult, context) => {
311311
// Replace optimistic todo in the todos list with the result
312312
context.client.setQueryData(['todos'], (old) =>
313-
old.map((todo) => (todo.id === scope.optimisticTodo.id ? result : todo)),
313+
old.map((todo) =>
314+
todo.id === onMutateResult.optimisticTodo.id ? result : todo,
315+
),
314316
)
315317
},
316-
onError: (error, variables, scope, context) => {
318+
onError: (error, variables, onMutateResult, context) => {
317319
// Remove optimistic todo from the todos list
318320
context.client.setQueryData(['todos'], (old) =>
319-
old.filter((todo) => todo.id !== scope.optimisticTodo.id),
321+
old.filter((todo) => todo.id !== onMutateResult.optimisticTodo.id),
320322
)
321323
},
322324
retry: 3,

docs/framework/react/guides/optimistic-updates.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,16 @@ useMutation({
110110
// Optimistically update to the new value
111111
context.client.setQueryData(['todos'], (old) => [...old, newTodo])
112112

113-
// Return a scope object with the snapshotted value
113+
// Return a result with the snapshotted value
114114
return { previousTodos }
115115
},
116116
// If the mutation fails,
117-
// use the scope returned from onMutate to roll back
118-
onError: (err, newTodo, scope, context) => {
119-
context.client.setQueryData(['todos'], scope.previousTodos)
117+
// use the result returned from onMutate to roll back
118+
onError: (err, newTodo, onMutateResult, context) => {
119+
context.client.setQueryData(['todos'], onMutateResult.previousTodos)
120120
},
121121
// Always refetch after error or success:
122-
onSettled: (data, error, variables, scope, context) =>
122+
onSettled: (data, error, variables, onMutateResult, context) =>
123123
context.client.invalidateQueries({ queryKey: ['todos'] }),
124124
})
125125
```
@@ -145,15 +145,18 @@ useMutation({
145145
// Optimistically update to the new value
146146
context.client.setQueryData(['todos', newTodo.id], newTodo)
147147

148-
// Return a scope with the previous and new todo
148+
// Return a result with the previous and new todo
149149
return { previousTodo, newTodo }
150150
},
151-
// If the mutation fails, use the scope we returned above
152-
onError: (err, newTodo, scope, context) => {
153-
context.client.setQueryData(['todos', scope.newTodo.id], scope.previousTodo)
151+
// If the mutation fails, use the result we returned above
152+
onError: (err, newTodo, onMutateResult, context) => {
153+
context.client.setQueryData(
154+
['todos', onMutateResult.newTodo.id],
155+
onMutateResult.previousTodo,
156+
)
154157
},
155158
// Always refetch after error or success:
156-
onSettled: (newTodo, error, variables, scope, context) =>
159+
onSettled: (newTodo, error, variables, onMutateResult, context) =>
157160
context.client.invalidateQueries({ queryKey: ['todos', newTodo.id] }),
158161
})
159162
```
@@ -168,7 +171,7 @@ You can also use the `onSettled` function in place of the separate `onError` and
168171
useMutation({
169172
mutationFn: updateTodo,
170173
// ...
171-
onSettled: async (newTodo, error, variables, scope) => {
174+
onSettled: async (newTodo, error, variables, onMutateResult, context) => {
172175
if (error) {
173176
// do something
174177
}

docs/framework/react/reference/useMutation.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ mutate(variables, {
6969
- This function will fire before the mutation function is fired and is passed the same variables the mutation function would receive
7070
- Useful to perform optimistic updates to a resource in hopes that the mutation succeeds
7171
- The value returned from this function will be passed to both the `onError` and `onSettled` functions in the event of a mutation failure and can be useful for rolling back optimistic updates.
72-
- `onSuccess: (data: TData, variables: TVariables, scope: TScope | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown`
72+
- `onSuccess: (data: TData, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown`
7373
- Optional
7474
- This function will fire when the mutation is successful and will be passed the mutation's result.
7575
- If a promise is returned, it will be awaited and resolved before proceeding
76-
- `onError: (err: TError, variables: TVariables, scope: TScope | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown`
76+
- `onError: (err: TError, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown`
7777
- Optional
7878
- This function will fire if the mutation encounters an error and will be passed the error.
7979
- If a promise is returned, it will be awaited and resolved before proceeding
80-
- `onSettled: (data: TData, error: TError, variables: TVariables, scope: TScope | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown`
80+
- `onSettled: (data: TData, error: TError, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => Promise<unknown> | unknown`
8181
- Optional
8282
- This function will fire when the mutation is either successfully fetched or encounters an error and be passed either the data or error
8383
- If a promise is returned, it will be awaited and resolved before proceeding
@@ -114,15 +114,15 @@ mutate(variables, {
114114
- `variables: TVariables`
115115
- Optional
116116
- The variables object to pass to the `mutationFn`.
117-
- `onSuccess: (data: TData, variables: TVariables, scope: TScope | undefined, context: MutationFunctionContext) => void`
117+
- `onSuccess: (data: TData, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void`
118118
- Optional
119119
- This function will fire when the mutation is successful and will be passed the mutation's result.
120120
- Void function, the returned value will be ignored
121-
- `onError: (err: TError, variables: TVariables, scope: TScope | undefined, context: MutationFunctionContext) => void`
121+
- `onError: (err: TError, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void`
122122
- Optional
123123
- This function will fire if the mutation encounters an error and will be passed the error.
124124
- Void function, the returned value will be ignored
125-
- `onSettled: (data: TData | undefined, error: TError | null, variables: TVariables, scope: TScope | undefined, context: MutationFunctionContext) => void`
125+
- `onSettled: (data: TData | undefined, error: TError | null, variables: TVariables, onMutateResult: TOnMutateResult | undefined, context: MutationFunctionContext) => void`
126126
- Optional
127127
- This function will fire when the mutation is either successfully fetched or encounters an error and be passed either the data or error
128128
- Void function, the returned value will be ignored

examples/react/optimistic-updates-cache/src/pages/index.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,17 @@ function Example() {
7171
return { previousTodos }
7272
},
7373
// If the mutation fails,
74-
// use the scope returned from onMutate to roll back
75-
onError: (err, variables, scope, context) => {
76-
if (scope?.previousTodos) {
77-
context.client.setQueryData<Todos>(['todos'], scope.previousTodos)
74+
// use the result returned from onMutate to roll back
75+
onError: (err, variables, onMutateResult, context) => {
76+
if (onMutateResult?.previousTodos) {
77+
context.client.setQueryData<Todos>(
78+
['todos'],
79+
onMutateResult.previousTodos,
80+
)
7881
}
7982
},
8083
// Always refetch after error or success:
81-
onSettled: (data, error, variables, scope, context) =>
84+
onSettled: (data, error, variables, onMutateResult, context) =>
8285
context.client.invalidateQueries({ queryKey: ['todos'] }),
8386
})
8487

packages/angular-query-experimental/src/__tests__/mutation-options.test-d.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ describe('mutationOptions', () => {
6464
mutationFn: () => Promise.resolve(5),
6565
mutationKey: ['key'],
6666
onMutate: () => {
67-
return { name: 'scope' }
67+
return { name: 'onMutateResult' }
6868
},
69-
onSuccess: (_data, _variables, scope) => {
70-
expectTypeOf(scope).toEqualTypeOf<{ name: string } | undefined>()
69+
onSuccess: (_data, _variables, onMutateResult) => {
70+
expectTypeOf(onMutateResult).toEqualTypeOf<
71+
{ name: string } | undefined
72+
>()
7173
},
7274
})
7375
})
@@ -82,13 +84,13 @@ describe('mutationOptions', () => {
8284
onMutate: (_variables, context) => {
8385
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
8486
},
85-
onSuccess: (_data, _variables, _scope, context) => {
87+
onSuccess: (_data, _variables, _onMutateResult, context) => {
8688
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
8789
},
88-
onError: (_error, _variables, _scope, context) => {
90+
onError: (_error, _variables, _onMutateResult, context) => {
8991
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
9092
},
91-
onSettled: (_data, _error, _variables, _scope, context) => {
93+
onSettled: (_data, _error, _variables, _onMutateResult, context) => {
9294
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
9395
},
9496
})

0 commit comments

Comments
 (0)