Skip to content

Commit c85be41

Browse files
joshuaellistravikk
andcommitted
feat(query-core): add context to mutationfn
Co-Authored-By: travikk <[email protected]>
1 parent 43049c5 commit c85be41

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+459
-478
lines changed

docs/framework/angular/guides/mutations.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,20 @@ export class TodosComponent {
9090
```ts
9191
mutation = injectMutation(() => ({
9292
mutationFn: addTodo,
93-
onMutate: (variables) => {
93+
onMutate: (variables, context) => {
9494
// A mutation is about to happen!
9595

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

144144
mutation.mutate(todo, {
145-
onSuccess: (data, variables, context) => {
145+
onSuccess: (data, variables, scope) => {
146146
// I will fire second!
147147
},
148-
onError: (error, variables, context) => {
148+
onError: (error, variables, scope) => {
149149
// I will fire second!
150150
},
151-
onSettled: (data, error, variables, context) => {
151+
onSettled: (data, error, variables, scope) => {
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, context) => {
164+
onSuccess: (data, variables, scope) => {
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, context) => {
172+
onSuccess: (data, variables, scope) => {
173173
// Will execute only once, for the last mutation (Todo 3),
174174
// regardless which mutation resolves first
175175
},
@@ -214,31 +214,29 @@ const queryClient = new QueryClient()
214214
// Define the "addTodo" mutation
215215
queryClient.setMutationDefaults(['addTodo'], {
216216
mutationFn: addTodo,
217-
onMutate: async (variables) => {
217+
onMutate: async (variables, context) => {
218218
// Cancel current queries for the todos list
219-
await queryClient.cancelQueries({ queryKey: ['todos'] })
219+
await context.client.cancelQueries({ queryKey: ['todos'] })
220220

221221
// Create optimistic todo
222222
const optimisticTodo = { id: uuid(), title: variables.title }
223223

224224
// Add optimistic todo to todos list
225-
queryClient.setQueryData(['todos'], (old) => [...old, optimisticTodo])
225+
context.client.setQueryData(['todos'], (old) => [...old, optimisticTodo])
226226

227-
// Return context with the optimistic todo
228-
return { optimisticTodo }
227+
// Return scope with the optimistic todo
228+
return { optimisticTodo, client: context.client }
229229
},
230-
onSuccess: (result, variables, context) => {
230+
onSuccess: (result, variables, scope) => {
231231
// Replace optimistic todo in the todos list with the result
232-
queryClient.setQueryData(['todos'], (old) =>
233-
old.map((todo) =>
234-
todo.id === context.optimisticTodo.id ? result : todo,
235-
),
232+
scope.client.setQueryData(['todos'], (old) =>
233+
old.map((todo) => (todo.id === scope.optimisticTodo.id ? result : todo)),
236234
)
237235
},
238-
onError: (error, variables, context) => {
236+
onError: (error, variables, scope) => {
239237
// Remove optimistic todo from the todos list
240-
queryClient.setQueryData(['todos'], (old) =>
241-
old.filter((todo) => todo.id !== context.optimisticTodo.id),
238+
scope.client.setQueryData(['todos'], (old) =>
239+
old.filter((todo) => todo.id !== scope.optimisticTodo.id),
242240
)
243241
},
244242
retry: 3,

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

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,28 @@ queryClient = inject(QueryClient)
8787
updateTodo = injectMutation(() => ({
8888
mutationFn: updateTodo,
8989
// When mutate is called:
90-
onMutate: async (newTodo) => {
90+
onMutate: async (newTodo, context) => {
9191
// Cancel any outgoing refetches
9292
// (so they don't overwrite our optimistic update)
93-
await this.queryClient.cancelQueries({ queryKey: ['todos'] })
93+
await context.client.cancelQueries({ queryKey: ['todos'] })
9494

9595
// Snapshot the previous value
96-
const previousTodos = client.getQueryData(['todos'])
96+
const previousTodos = context.client.getQueryData(['todos'])
9797

9898
// Optimistically update to the new value
99-
this.queryClient.setQueryData(['todos'], (old) => [...old, newTodo])
99+
context.client.setQueryData(['todos'], (old) => [...old, newTodo])
100100

101-
// Return a context object with the snapshotted value
102-
return { previousTodos }
101+
// Return a scope object with the snapshotted value
102+
return { previousTodos, client: context.client }
103103
},
104104
// If the mutation fails,
105-
// use the context returned from onMutate to roll back
106-
onError: (err, newTodo, context) => {
107-
client.setQueryData(['todos'], context.previousTodos)
105+
// use the scope returned from onMutate to roll back
106+
onError: (err, newTodo, scope) => {
107+
scope.client.setQueryData(['todos'], scope.previousTodos)
108108
},
109109
// Always refetch after error or success:
110-
onSettled: () => {
111-
this.queryClient.invalidateQueries({ queryKey: ['todos'] })
110+
onSettled: (data, error, variables, scope) => {
111+
scope.client.invalidateQueries({ queryKey: ['todos'] })
112112
},
113113
}))
114114
```
@@ -122,30 +122,27 @@ queryClient = inject(QueryClient)
122122
updateTodo = injectMutation(() => ({
123123
mutationFn: updateTodo,
124124
// When mutate is called:
125-
onMutate: async (newTodo) => {
125+
onMutate: async (newTodo, context) => {
126126
// Cancel any outgoing refetches
127127
// (so they don't overwrite our optimistic update)
128-
await this.queryClient.cancelQueries({ queryKey: ['todos', newTodo.id] })
128+
await context.client.cancelQueries({ queryKey: ['todos', newTodo.id] })
129129

130130
// Snapshot the previous value
131-
const previousTodo = this.queryClient.getQueryData(['todos', newTodo.id])
131+
const previousTodo = context.client.getQueryData(['todos', newTodo.id])
132132

133133
// Optimistically update to the new value
134-
this.queryClient.setQueryData(['todos', newTodo.id], newTodo)
134+
context.client.setQueryData(['todos', newTodo.id], newTodo)
135135

136-
// Return a context with the previous and new todo
137-
return { previousTodo, newTodo }
136+
// Return a scope with the previous and new todo
137+
return { previousTodo, newTodo, client: context.client }
138138
},
139-
// If the mutation fails, use the context we returned above
140-
onError: (err, newTodo, context) => {
141-
this.queryClient.setQueryData(
142-
['todos', context.newTodo.id],
143-
context.previousTodo,
144-
)
139+
// If the mutation fails, use the scope we returned above
140+
onError: (err, newTodo, scope) => {
141+
scope.client.setQueryData(['todos', scope.newTodo.id], scope.previousTodo)
145142
},
146143
// Always refetch after error or success:
147-
onSettled: (newTodo) => {
148-
this.queryClient.invalidateQueries({ queryKey: ['todos', newTodo.id] })
144+
onSettled: (newTodo, error, variables, scope) => {
145+
scope.client.invalidateQueries({ queryKey: ['todos', newTodo.id] })
149146
},
150147
}))
151148
```
@@ -157,7 +154,7 @@ updateTodo = injectMutation(() => ({
157154
injectMutation({
158155
mutationFn: updateTodo,
159156
// ...
160-
onSettled: (newTodo, error, variables, context) => {
157+
onSettled: (newTodo, error, variables, scope) => {
161158
if (error) {
162159
// do something
163160
}

docs/framework/angular/reference/functions/injectmutation.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ title: injectMutation
66
# Function: injectMutation()
77

88
```ts
9-
function injectMutation<TData, TError, TVariables, TContext>(
9+
function injectMutation<TData, TError, TVariables, TScope>(
1010
injectMutationFn,
1111
options?,
12-
): CreateMutationResult<TData, TError, TVariables, TContext>
12+
): CreateMutationResult<TData, TError, TVariables, TScope>
1313
```
1414

1515
Injects a mutation: an imperative function that can be invoked which typically performs server side effects.
@@ -24,13 +24,13 @@ Unlike queries, mutations are not run automatically.
2424

2525
**TVariables** = `void`
2626

27-
**TContext** = `unknown`
27+
**TScope** = `unknown`
2828

2929
## Parameters
3030

3131
### injectMutationFn
3232

33-
() => [`CreateMutationOptions`](../../interfaces/createmutationoptions.md)\<`TData`, `TError`, `TVariables`, `TContext`\>
33+
() => [`CreateMutationOptions`](../../interfaces/createmutationoptions.md)\<`TData`, `TError`, `TVariables`, `TScope`\>
3434

3535
A function that returns mutation options.
3636

@@ -42,7 +42,7 @@ Additional configuration
4242

4343
## Returns
4444

45-
[`CreateMutationResult`](../../type-aliases/createmutationresult.md)\<`TData`, `TError`, `TVariables`, `TContext`\>
45+
[`CreateMutationResult`](../../type-aliases/createmutationresult.md)\<`TData`, `TError`, `TVariables`, `TScope`\>
4646

4747
The mutation.
4848

docs/framework/angular/reference/functions/mutationoptions.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ title: mutationOptions
66
# Function: mutationOptions()
77

88
```ts
9-
function mutationOptions<TData, TError, TVariables, TContext>(
9+
function mutationOptions<TData, TError, TVariables, TScope>(
1010
options,
11-
): CreateMutationOptions<TData, TError, TVariables, TContext>
11+
): CreateMutationOptions<TData, TError, TVariables, TScope>
1212
```
1313

1414
Allows to share and re-use mutation options in a type-safe way.
@@ -46,19 +46,19 @@ mutation.mutate({ title: 'New Title' })
4646

4747
**TVariables** = `void`
4848

49-
**TContext** = `unknown`
49+
**TScope** = `unknown`
5050

5151
## Parameters
5252

5353
### options
5454

55-
`MutationObserverOptions`\<`TData`, `TError`, `TVariables`, `TContext`\>
55+
`MutationObserverOptions`\<`TData`, `TError`, `TVariables`, `TScope`\>
5656

5757
The mutation options.
5858

5959
## Returns
6060

61-
[`CreateMutationOptions`](../../interfaces/createmutationoptions.md)\<`TData`, `TError`, `TVariables`, `TContext`\>
61+
[`CreateMutationOptions`](../../interfaces/createmutationoptions.md)\<`TData`, `TError`, `TVariables`, `TScope`\>
6262

6363
Mutation options.
6464

docs/framework/angular/reference/interfaces/basemutationnarrowing.md

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ id: BaseMutationNarrowing
33
title: BaseMutationNarrowing
44
---
55

6-
# Interface: BaseMutationNarrowing\<TData, TError, TVariables, TContext\>
6+
# Interface: BaseMutationNarrowing\<TData, TError, TVariables, TScope\>
77

88
## Type Parameters
99

@@ -13,7 +13,7 @@ title: BaseMutationNarrowing
1313

1414
**TVariables** = `unknown`
1515

16-
**TContext** = `unknown`
16+
**TScope** = `unknown`
1717

1818
## Properties
1919

@@ -25,17 +25,12 @@ isError: SignalFunction<
2525
TData,
2626
TError,
2727
TVariables,
28-
TContext,
28+
TScope,
2929
Override<
30-
MutationObserverErrorResult<TData, TError, TVariables, TContext>,
31-
{ mutate: CreateMutateFunction<TData, TError, TVariables, TContext> }
30+
MutationObserverErrorResult<TData, TError, TVariables, TScope>,
31+
{ mutate: CreateMutateFunction<TData, TError, TVariables, TScope> }
3232
> & {
33-
mutateAsync: CreateMutateAsyncFunction<
34-
TData,
35-
TError,
36-
TVariables,
37-
TContext
38-
>
33+
mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TScope>
3934
}
4035
>
4136
>
@@ -55,17 +50,12 @@ isIdle: SignalFunction<
5550
TData,
5651
TError,
5752
TVariables,
58-
TContext,
53+
TScope,
5954
Override<
60-
MutationObserverIdleResult<TData, TError, TVariables, TContext>,
61-
{ mutate: CreateMutateFunction<TData, TError, TVariables, TContext> }
55+
MutationObserverIdleResult<TData, TError, TVariables, TScope>,
56+
{ mutate: CreateMutateFunction<TData, TError, TVariables, TScope> }
6257
> & {
63-
mutateAsync: CreateMutateAsyncFunction<
64-
TData,
65-
TError,
66-
TVariables,
67-
TContext
68-
>
58+
mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TScope>
6959
}
7060
>
7161
>
@@ -85,17 +75,12 @@ isPending: SignalFunction<
8575
TData,
8676
TError,
8777
TVariables,
88-
TContext,
78+
TScope,
8979
Override<
90-
MutationObserverLoadingResult<TData, TError, TVariables, TContext>,
91-
{ mutate: CreateMutateFunction<TData, TError, TVariables, TContext> }
80+
MutationObserverLoadingResult<TData, TError, TVariables, TScope>,
81+
{ mutate: CreateMutateFunction<TData, TError, TVariables, TScope> }
9282
> & {
93-
mutateAsync: CreateMutateAsyncFunction<
94-
TData,
95-
TError,
96-
TVariables,
97-
TContext
98-
>
83+
mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TScope>
9984
}
10085
>
10186
>
@@ -115,17 +100,12 @@ isSuccess: SignalFunction<
115100
TData,
116101
TError,
117102
TVariables,
118-
TContext,
103+
TScope,
119104
Override<
120-
MutationObserverSuccessResult<TData, TError, TVariables, TContext>,
121-
{ mutate: CreateMutateFunction<TData, TError, TVariables, TContext> }
105+
MutationObserverSuccessResult<TData, TError, TVariables, TScope>,
106+
{ mutate: CreateMutateFunction<TData, TError, TVariables, TScope> }
122107
> & {
123-
mutateAsync: CreateMutateAsyncFunction<
124-
TData,
125-
TError,
126-
TVariables,
127-
TContext
128-
>
108+
mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TScope>
129109
}
130110
>
131111
>

0 commit comments

Comments
 (0)