Skip to content

Commit 4b0f79f

Browse files
authored
fix(angular-query): allow providing on element injectors (#9134)
1 parent b1e0c9f commit 4b0f79f

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

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

+28-30
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ import {
66
computed,
77
effect,
88
inject,
9-
makeEnvironmentProviders,
109
} from '@angular/core'
1110
import { QueryClient, onlineManager } from '@tanstack/query-core'
1211
import { isPlatformBrowser } from '@angular/common'
1312
import { isDevMode } from './util/is-dev-mode/is-dev-mode'
1413
import { noop } from './util'
15-
import type { EnvironmentProviders, Provider } from '@angular/core'
14+
import type { Provider } from '@angular/core'
1615
import type {
1716
DevtoolsButtonPosition,
1817
DevtoolsErrorType,
@@ -23,9 +22,10 @@ import type {
2322
/**
2423
* Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
2524
* {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
26-
* for the entire application. You can use `provideQueryClient` to provide a
27-
* different `QueryClient` instance for a part of the application.
28-
* @param queryClient - the `QueryClient` instance to provide.
25+
* for the entire application. Internally it calls `provideQueryClient`.
26+
* You can use `provideQueryClient` to provide a different `QueryClient` instance for a part
27+
* of the application or for unit testing purposes.
28+
* @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
2929
* @returns a provider object that can be used to provide the `QueryClient` instance.
3030
*/
3131
export function provideQueryClient(
@@ -34,9 +34,14 @@ export function provideQueryClient(
3434
return {
3535
provide: QueryClient,
3636
useFactory: () => {
37-
return queryClient instanceof InjectionToken
38-
? inject(queryClient)
39-
: queryClient
37+
const client =
38+
queryClient instanceof InjectionToken
39+
? inject(queryClient)
40+
: queryClient
41+
// Unmount the query client on injector destroy
42+
inject(DestroyRef).onDestroy(() => client.unmount())
43+
client.mount()
44+
return client
4045
},
4146
}
4247
}
@@ -93,6 +98,17 @@ export function provideQueryClient(
9398
* }
9499
* )
95100
* ```
101+
*
102+
* **Example: using an InjectionToken**
103+
*
104+
* ```ts
105+
* export const MY_QUERY_CLIENT = new InjectionToken('', {
106+
* factory: () => new QueryClient(),
107+
* })
108+
*
109+
* // In a lazy loaded route or lazy loaded component's providers array:
110+
* providers: [provideTanStackQuery(MY_QUERY_CLIENT)]
111+
* ```
96112
* @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
97113
* @param features - Optional features to configure additional Query functionality.
98114
* @returns A set of providers to set up TanStack Query.
@@ -102,27 +118,11 @@ export function provideQueryClient(
102118
export function provideTanStackQuery(
103119
queryClient: QueryClient | InjectionToken<QueryClient>,
104120
...features: Array<QueryFeatures>
105-
): EnvironmentProviders {
106-
return makeEnvironmentProviders([
121+
): Array<Provider> {
122+
return [
107123
provideQueryClient(queryClient),
108-
{
109-
// Do not use provideEnvironmentInitializer while Angular < v19 is supported
110-
provide: ENVIRONMENT_INITIALIZER,
111-
multi: true,
112-
useFactory: () => {
113-
const client =
114-
queryClient instanceof InjectionToken
115-
? inject(queryClient)
116-
: queryClient
117-
return () => {
118-
client.mount()
119-
// Unmount the query client on application destroy
120-
inject(DestroyRef).onDestroy(() => client.unmount())
121-
}
122-
},
123-
},
124124
features.map((feature) => feature.ɵproviders),
125-
])
125+
]
126126
}
127127

128128
/**
@@ -135,9 +135,7 @@ export function provideTanStackQuery(
135135
* @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
136136
* @deprecated Use `provideTanStackQuery` instead.
137137
*/
138-
export function provideAngularQuery(
139-
queryClient: QueryClient,
140-
): EnvironmentProviders {
138+
export function provideAngularQuery(queryClient: QueryClient): Array<Provider> {
141139
return provideTanStackQuery(queryClient)
142140
}
143141

0 commit comments

Comments
 (0)