@@ -6,13 +6,12 @@ import {
6
6
computed ,
7
7
effect ,
8
8
inject ,
9
- makeEnvironmentProviders ,
10
9
} from '@angular/core'
11
10
import { QueryClient , onlineManager } from '@tanstack/query-core'
12
11
import { isPlatformBrowser } from '@angular/common'
13
12
import { isDevMode } from './util/is-dev-mode/is-dev-mode'
14
13
import { noop } from './util'
15
- import type { EnvironmentProviders , Provider } from '@angular/core'
14
+ import type { Provider } from '@angular/core'
16
15
import type {
17
16
DevtoolsButtonPosition ,
18
17
DevtoolsErrorType ,
@@ -23,9 +22,10 @@ import type {
23
22
/**
24
23
* Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
25
24
* {@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`.
29
29
* @returns a provider object that can be used to provide the `QueryClient` instance.
30
30
*/
31
31
export function provideQueryClient (
@@ -34,9 +34,14 @@ export function provideQueryClient(
34
34
return {
35
35
provide : QueryClient ,
36
36
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
40
45
} ,
41
46
}
42
47
}
@@ -93,6 +98,17 @@ export function provideQueryClient(
93
98
* }
94
99
* )
95
100
* ```
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
+ * ```
96
112
* @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
97
113
* @param features - Optional features to configure additional Query functionality.
98
114
* @returns A set of providers to set up TanStack Query.
@@ -102,27 +118,11 @@ export function provideQueryClient(
102
118
export function provideTanStackQuery (
103
119
queryClient : QueryClient | InjectionToken < QueryClient > ,
104
120
...features : Array < QueryFeatures >
105
- ) : EnvironmentProviders {
106
- return makeEnvironmentProviders ( [
121
+ ) : Array < Provider > {
122
+ return [
107
123
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
- } ,
124
124
features . map ( ( feature ) => feature . ɵproviders ) ,
125
- ] )
125
+ ]
126
126
}
127
127
128
128
/**
@@ -135,9 +135,7 @@ export function provideTanStackQuery(
135
135
* @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
136
136
* @deprecated Use `provideTanStackQuery` instead.
137
137
*/
138
- export function provideAngularQuery (
139
- queryClient : QueryClient ,
140
- ) : EnvironmentProviders {
138
+ export function provideAngularQuery ( queryClient : QueryClient ) : Array < Provider > {
141
139
return provideTanStackQuery ( queryClient )
142
140
}
143
141
0 commit comments