Skip to content

Commit d96376b

Browse files
joshuaellisTkDodo
andcommitted
chore: pr amends
Co-Authored-By: Dominik Dorfmeister <[email protected]>
1 parent 416a3fe commit d96376b

File tree

5 files changed

+56
-33
lines changed

5 files changed

+56
-33
lines changed

docs/framework/angular/guides/mutations.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ mutation = injectMutation(() => ({
9696
// Optionally return a scope containing data to use when for example rolling back
9797
return { id: 1 }
9898
},
99-
onError: (error, variables, scope) => {
99+
onError: (error, variables, scope, context) => {
100100
// An error happened!
101101
console.log(`rolling back optimistic update with id ${scope.id}`)
102102
},
103-
onSuccess: (data, variables, scope) => {
103+
onSuccess: (data, variables, scope, context) => {
104104
// Boom baby!
105105
},
106-
onSettled: (data, error, variables, scope) => {
106+
onSettled: (data, error, variables, scope, 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) => {
133+
onSuccess: (data, variables, scope, context) => {
134134
// I will fire first
135135
},
136-
onError: (error, variables, scope) => {
136+
onError: (error, variables, scope, context) => {
137137
// I will fire first
138138
},
139-
onSettled: (data, error, variables, scope) => {
139+
onSettled: (data, error, variables, scope, context) => {
140140
// I will fire first
141141
},
142142
}))
143143

144144
mutation.mutate(todo, {
145-
onSuccess: (data, variables, scope) => {
145+
onSuccess: (data, variables, scope, context) => {
146146
// I will fire second!
147147
},
148-
onError: (error, variables, scope) => {
148+
onError: (error, variables, scope, context) => {
149149
// I will fire second!
150150
},
151-
onSettled: (data, error, variables, scope) => {
151+
onSettled: (data, error, variables, scope, 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) => {
164+
onSuccess: (data, variables, scope, 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) => {
172+
onSuccess: (data, variables, scope, context) => {
173173
// Will execute only once, for the last mutation (Todo 3),
174174
// regardless which mutation resolves first
175175
},

docs/framework/react/guides/mutations.md

Lines changed: 11 additions & 11 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) => {
152+
onError: (error, variables, scope, context) => {
153153
// An error happened!
154154
console.log(`rolling back optimistic update with id ${scope.id}`)
155155
},
156-
onSuccess: (data, variables, scope) => {
156+
onSuccess: (data, variables, scope, context) => {
157157
// Boom baby!
158158
},
159-
onSettled: (data, error, variables, scope) => {
159+
onSettled: (data, error, variables, scope, 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) => {
192+
onSuccess: (data, variables, scope, context) => {
193193
// I will fire first
194194
},
195-
onError: (error, variables, scope) => {
195+
onError: (error, variables, scope, context) => {
196196
// I will fire first
197197
},
198-
onSettled: (data, error, variables, scope) => {
198+
onSettled: (data, error, variables, scope, context) => {
199199
// I will fire first
200200
},
201201
})
202202

203203
mutate(todo, {
204-
onSuccess: (data, variables, scope) => {
204+
onSuccess: (data, variables, scope, context) => {
205205
// I will fire second!
206206
},
207-
onError: (error, variables, scope) => {
207+
onError: (error, variables, scope, context) => {
208208
// I will fire second!
209209
},
210-
onSettled: (data, error, variables, scope) => {
210+
onSettled: (data, error, variables, scope, 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) => {
229+
onSuccess: (data, variables, scope, 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) => {
237+
onSuccess: (data, variables, scope, context) => {
238238
// Will execute only once, for the last mutation (Todo 3),
239239
// regardless which mutation resolves first
240240
},

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) => Promise<unknown> | unknown`
72+
- `onSuccess: (data: TData, variables: TVariables, scope: TScope | 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) => Promise<unknown> | unknown`
76+
- `onError: (err: TError, variables: TVariables, scope: TScope | 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) => Promise<unknown> | unknown`
80+
- `onSettled: (data: TData, error: TError, variables: TVariables, scope: TScope | 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) => void`
117+
- `onSuccess: (data: TData, variables: TVariables, scope: TScope | 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) => void`
121+
- `onError: (err: TError, variables: TVariables, scope: TScope | 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) => void`
125+
- `onSettled: (data: TData | undefined, error: TError | null, variables: TVariables, scope: TScope | 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

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '..'
99
import type {
1010
DefaultError,
11+
MutationFunctionContext,
1112
MutationState,
1213
WithRequired,
1314
} from '@tanstack/query-core'
@@ -58,15 +59,37 @@ describe('mutationOptions', () => {
5859
})
5960
})
6061

61-
it('should infer context type correctly', () => {
62+
it('should infer scope type correctly', () => {
6263
mutationOptions<number, DefaultError, void, { name: string }>({
6364
mutationFn: () => Promise.resolve(5),
6465
mutationKey: ['key'],
6566
onMutate: () => {
66-
return { name: 'context' }
67+
return { name: 'scope' }
68+
},
69+
onSuccess: (_data, _variables, scope) => {
70+
expectTypeOf(scope).toEqualTypeOf<{ name: string } | undefined>()
71+
},
72+
})
73+
})
74+
75+
it('should infer context type correctly', () => {
76+
mutationOptions<number>({
77+
mutationFn: (_variables, context) => {
78+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
79+
return Promise.resolve(5)
80+
},
81+
mutationKey: ['key'],
82+
onMutate: (_variables, context) => {
83+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
84+
},
85+
onSuccess: (_data, _variables, _scope, context) => {
86+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
87+
},
88+
onError: (_error, _variables, _scope, context) => {
89+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
6790
},
68-
onSuccess: (_data, _variables, context) => {
69-
expectTypeOf(context).toEqualTypeOf<{ name: string }>()
91+
onSettled: (_data, _error, _variables, _scope, context) => {
92+
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
7093
},
7194
})
7295
})

packages/query-core/src/mutation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface MutationState<
3030
TScope = unknown,
3131
> {
3232
/**
33-
* @deprecated This will be removed in the next major version. Use the `scope` property instead.
33+
* @deprecated `context` was renamed to `scope`. This will be removed in the next major version. Use the `scope` property instead.
3434
*/
3535
context: TScope | undefined
3636
scope: TScope | undefined

0 commit comments

Comments
 (0)