11import {
22 DestroyRef ,
33 ENVIRONMENT_INITIALIZER ,
4+ InjectionToken ,
45 PLATFORM_ID ,
56 computed ,
67 effect ,
@@ -25,10 +26,19 @@ import type {
2526 * for the entire application. You can use `provideQueryClient` to provide a
2627 * different `QueryClient` instance for a part of the application.
2728 * @param queryClient - the `QueryClient` instance to provide.
28- * @public
29+ * @returns a provider object that can be used to provide the `QueryClient` instance.
2930 */
30- export function provideQueryClient ( queryClient : QueryClient ) {
31- return { provide : QueryClient , useValue : queryClient }
31+ export function provideQueryClient (
32+ queryClient : QueryClient | InjectionToken < QueryClient > ,
33+ ) : Provider {
34+ return {
35+ provide : QueryClient ,
36+ useFactory : ( ) => {
37+ return queryClient instanceof InjectionToken
38+ ? inject ( queryClient )
39+ : queryClient
40+ } ,
41+ }
3242}
3343
3444/**
@@ -83,15 +93,14 @@ export function provideQueryClient(queryClient: QueryClient) {
8393 * }
8494 * )
8595 * ```
86- * @param queryClient - A `QueryClient` instance.
96+ * @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient` .
8797 * @param features - Optional features to configure additional Query functionality.
8898 * @returns A set of providers to set up TanStack Query.
89- * @public
9099 * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
91100 * @see withDevtools
92101 */
93102export function provideTanStackQuery (
94- queryClient : QueryClient ,
103+ queryClient : QueryClient | InjectionToken < QueryClient > ,
95104 ...features : Array < QueryFeatures >
96105) : EnvironmentProviders {
97106 return makeEnvironmentProviders ( [
@@ -100,10 +109,16 @@ export function provideTanStackQuery(
100109 // Do not use provideEnvironmentInitializer while Angular < v19 is supported
101110 provide : ENVIRONMENT_INITIALIZER ,
102111 multi : true ,
103- useValue : ( ) => {
104- queryClient . mount ( )
105- // Unmount the query client on application destroy
106- inject ( DestroyRef ) . onDestroy ( ( ) => queryClient . unmount ( ) )
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+ }
107122 } ,
108123 } ,
109124 features . map ( ( feature ) => feature . ɵproviders ) ,
0 commit comments