Skip to content

Commit 4aff846

Browse files
authored
fix(angular-query): remove queryClient parameter from callback functions (#8307)
1 parent fa77f32 commit 4aff846

File tree

8 files changed

+46
-71
lines changed

8 files changed

+46
-71
lines changed

docs/framework/angular/guides/invalidations-from-mutations.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export class TodosComponent {
2828
queryClient = inject(QueryClient)
2929

3030
// When this mutation succeeds, invalidate any queries with the `todos` or `reminders` query key
31-
mutation = injectMutation((client) => ({
31+
mutation = injectMutation(() => ({
3232
mutationFn: addTodo,
3333
onSuccess: () => {
34-
client.invalidateQueries({ queryKey: ['todos'] })
35-
client.invalidateQueries({ queryKey: ['reminders'] })
34+
this.queryClient.invalidateQueries({ queryKey: ['todos'] })
35+
this.queryClient.invalidateQueries({ queryKey: ['reminders'] })
3636

3737
// OR use the queryClient that is injected into the component
3838
// this.queryClient.invalidateQueries({ queryKey: ['todos'] })

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,21 @@ mutationState = injectMutationState<string>(() => ({
8282
[//]: # 'Example'
8383

8484
```ts
85-
updateTodo = injectMutation((client) => ({
85+
queryClient = inject(QueryClient)
86+
87+
updateTodo = injectMutation(() => ({
8688
mutationFn: updateTodo,
8789
// When mutate is called:
8890
onMutate: async (newTodo) => {
8991
// Cancel any outgoing refetches
9092
// (so they don't overwrite our optimistic update)
91-
await client.cancelQueries({ queryKey: ['todos'] })
93+
await this.queryClient.cancelQueries({ queryKey: ['todos'] })
9294

9395
// Snapshot the previous value
9496
const previousTodos = client.getQueryData(['todos'])
9597

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

99101
// Return a context object with the snapshotted value
100102
return { previousTodos }
@@ -106,7 +108,7 @@ updateTodo = injectMutation((client) => ({
106108
},
107109
// Always refetch after error or success:
108110
onSettled: () => {
109-
client.invalidateQueries({ queryKey: ['todos'] })
111+
this.queryClient.invalidateQueries({ queryKey: ['todos'] })
110112
},
111113
}))
112114
```
@@ -115,30 +117,35 @@ updateTodo = injectMutation((client) => ({
115117
[//]: # 'Example2'
116118

117119
```ts
118-
updateTodo = injectMutation((client) => ({
120+
queryClient = inject(QueryClient)
121+
122+
updateTodo = injectMutation(() => ({
119123
mutationFn: updateTodo,
120124
// When mutate is called:
121125
onMutate: async (newTodo) => {
122126
// Cancel any outgoing refetches
123127
// (so they don't overwrite our optimistic update)
124-
await client.cancelQueries({ queryKey: ['todos', newTodo.id] })
128+
await this.queryClient.cancelQueries({ queryKey: ['todos', newTodo.id] })
125129

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

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

132136
// Return a context with the previous and new todo
133137
return { previousTodo, newTodo }
134138
},
135139
// If the mutation fails, use the context we returned above
136140
onError: (err, newTodo, context) => {
137-
client.setQueryData(['todos', context.newTodo.id], context.previousTodo)
141+
this.queryClient.setQueryData(
142+
['todos', context.newTodo.id],
143+
context.previousTodo,
144+
)
138145
},
139146
// Always refetch after error or success:
140147
onSettled: (newTodo) => {
141-
client.invalidateQueries({ queryKey: ['todos', newTodo.id] })
148+
this.queryClient.invalidateQueries({ queryKey: ['todos', newTodo.id] })
142149
},
143150
}))
144151
```

docs/framework/angular/quick-start.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,10 @@ export class TodosComponent {
7777
queryFn: () => this.todoService.getTodos(),
7878
}))
7979
80-
mutation = injectMutation((client) => ({
80+
mutation = injectMutation(() => ({
8181
mutationFn: (todo: Todo) => this.todoService.addTodo(todo),
8282
onSuccess: () => {
83-
// Invalidate and refetch by using the client directly
84-
client.invalidateQueries({ queryKey: ['todos'] })
85-
86-
// OR use the queryClient that is injected into the component
87-
// this.queryClient.invalidateQueries({ queryKey: ['todos'] })
83+
this.queryClient.invalidateQueries({ queryKey: ['todos'] })
8884
},
8985
}))
9086

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,6 @@ describe('injectMutation', () => {
3636
vi.useRealTimers()
3737
})
3838

39-
describe('callback helpers', () => {
40-
test('can access client from options callback', async () => {
41-
const mutation = TestBed.runInInjectionContext(() => {
42-
return injectMutation((client) => ({
43-
mutationFn: () => {
44-
expect(client).toBe(queryClient)
45-
return Promise.resolve()
46-
},
47-
}))
48-
})
49-
50-
mutation.mutate()
51-
vi.advanceTimersByTime(1)
52-
expect(mutation.status()).toBe('pending')
53-
})
54-
})
55-
5639
test('should be in idle state initially', () => {
5740
const mutation = TestBed.runInInjectionContext(() => {
5841
return injectMutation(() => ({

packages/angular-query-experimental/src/create-base-query.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ export function createBaseQuery<
3030
TQueryData,
3131
TQueryKey extends QueryKey,
3232
>(
33-
optionsFn: (
34-
client: QueryClient,
35-
) => CreateBaseQueryOptions<
33+
optionsFn: () => CreateBaseQueryOptions<
3634
TQueryFnData,
3735
TError,
3836
TData,
@@ -54,9 +52,7 @@ export function createBaseQuery<
5452
* are preserved and can keep being applied after signal changes
5553
*/
5654
const defaultedOptionsSignal = computed(() => {
57-
const options = runInInjectionContext(injector, () =>
58-
optionsFn(queryClient),
59-
)
55+
const options = runInInjectionContext(injector, () => optionsFn())
6056
const defaultedOptions = queryClient.defaultQueryOptions(options)
6157
defaultedOptions._optimisticResults = 'optimistic'
6258
return defaultedOptions

packages/angular-query-experimental/src/inject-infinite-query.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { Injector } from '@angular/core'
55
import type {
66
DefaultError,
77
InfiniteData,
8-
QueryClient,
98
QueryKey,
109
QueryObserver,
1110
} from '@tanstack/query-core'
@@ -34,9 +33,7 @@ export function injectInfiniteQuery<
3433
TQueryKey extends QueryKey = QueryKey,
3534
TPageParam = unknown,
3635
>(
37-
optionsFn: (
38-
client: QueryClient,
39-
) => DefinedInitialDataInfiniteOptions<
36+
optionsFn: () => DefinedInitialDataInfiniteOptions<
4037
TQueryFnData,
4138
TError,
4239
TData,
@@ -61,9 +58,7 @@ export function injectInfiniteQuery<
6158
TQueryKey extends QueryKey = QueryKey,
6259
TPageParam = unknown,
6360
>(
64-
optionsFn: (
65-
client: QueryClient,
66-
) => UndefinedInitialDataInfiniteOptions<
61+
optionsFn: () => UndefinedInitialDataInfiniteOptions<
6762
TQueryFnData,
6863
TError,
6964
TData,
@@ -88,9 +83,7 @@ export function injectInfiniteQuery<
8883
TQueryKey extends QueryKey = QueryKey,
8984
TPageParam = unknown,
9085
>(
91-
optionsFn: (
92-
client: QueryClient,
93-
) => CreateInfiniteQueryOptions<
86+
optionsFn: () => CreateInfiniteQueryOptions<
9487
TQueryFnData,
9588
TError,
9689
TData,
@@ -110,7 +103,7 @@ export function injectInfiniteQuery<
110103
* @public
111104
*/
112105
export function injectInfiniteQuery(
113-
optionsFn: (client: QueryClient) => CreateInfiniteQueryOptions,
106+
optionsFn: () => CreateInfiniteQueryOptions,
114107
injector?: Injector,
115108
) {
116109
return assertInjector(injectInfiniteQuery, injector, () =>

packages/angular-query-experimental/src/inject-mutation.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ export function injectMutation<
4040
TVariables = void,
4141
TContext = unknown,
4242
>(
43-
optionsFn: (
44-
client: QueryClient,
45-
) => CreateMutationOptions<TData, TError, TVariables, TContext>,
43+
optionsFn: () => CreateMutationOptions<TData, TError, TVariables, TContext>,
4644
injector?: Injector,
4745
): CreateMutationResult<TData, TError, TVariables, TContext> {
4846
return assertInjector(injectMutation, injector, () => {
@@ -58,7 +56,7 @@ export function injectMutation<
5856
TError,
5957
TVariables,
6058
TContext
61-
>(queryClient, optionsFn(queryClient))
59+
>(queryClient, optionsFn())
6260
const mutate: CreateMutateFunction<
6361
TData,
6462
TError,
@@ -70,9 +68,7 @@ export function injectMutation<
7068

7169
effect(() => {
7270
observer.setOptions(
73-
runInInjectionContext(currentInjector, () =>
74-
optionsFn(queryClient),
75-
),
71+
runInInjectionContext(currentInjector, () => optionsFn()),
7672
)
7773
})
7874

packages/angular-query-experimental/src/inject-query.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { QueryObserver } from '@tanstack/query-core'
22
import { assertInjector } from './util/assert-injector/assert-injector'
33
import { createBaseQuery } from './create-base-query'
44
import type { Injector } from '@angular/core'
5-
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
5+
import type { DefaultError, QueryKey } from '@tanstack/query-core'
66
import type {
77
CreateQueryOptions,
88
CreateQueryResult,
@@ -56,9 +56,12 @@ export function injectQuery<
5656
TData = TQueryFnData,
5757
TQueryKey extends QueryKey = QueryKey,
5858
>(
59-
optionsFn: (
60-
client: QueryClient,
61-
) => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
59+
optionsFn: () => DefinedInitialDataOptions<
60+
TQueryFnData,
61+
TError,
62+
TData,
63+
TQueryKey
64+
>,
6265
injector?: Injector,
6366
): DefinedCreateQueryResult<TData, TError>
6467

@@ -105,9 +108,12 @@ export function injectQuery<
105108
TData = TQueryFnData,
106109
TQueryKey extends QueryKey = QueryKey,
107110
>(
108-
optionsFn: (
109-
client: QueryClient,
110-
) => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
111+
optionsFn: () => UndefinedInitialDataOptions<
112+
TQueryFnData,
113+
TError,
114+
TData,
115+
TQueryKey
116+
>,
111117
injector?: Injector,
112118
): CreateQueryResult<TData, TError>
113119

@@ -154,9 +160,7 @@ export function injectQuery<
154160
TData = TQueryFnData,
155161
TQueryKey extends QueryKey = QueryKey,
156162
>(
157-
optionsFn: (
158-
client: QueryClient,
159-
) => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
163+
optionsFn: () => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
160164
injector?: Injector,
161165
): CreateQueryResult<TData, TError>
162166

@@ -198,7 +202,7 @@ export function injectQuery<
198202
* @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
199203
*/
200204
export function injectQuery(
201-
optionsFn: (client: QueryClient) => CreateQueryOptions,
205+
optionsFn: () => CreateQueryOptions,
202206
injector?: Injector,
203207
) {
204208
return assertInjector(injectQuery, injector, () =>

0 commit comments

Comments
 (0)