Open History to see the last commit
1import { DestroyRef, InjectionToken, inject } from '@angular/core'
2import { QueryClient } from '@tanstack/query-core'
3import type { Provider } from '@angular/core'
4​
5/**
6 * Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the
7 * {@link https://tanstack.com/query/latest/docs/reference/QueryClient|QueryClient}
8 * for the entire application. Internally it calls `provideQueryClient`.
9 * You can use `provideQueryClient` to provide a different `QueryClient` instance for a part
10 * of the application or for unit testing purposes.
11 * @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
12 * @returns a provider object that can be used to provide the `QueryClient` instance.
13 */
14export function provideQueryClient(
15 queryClient: QueryClient | InjectionToken<QueryClient>,
16): Provider {
17 return {
18 provide: QueryClient,
19 useFactory: () => {
20 const client =
21 queryClient instanceof InjectionToken
22 ? inject(queryClient)
23 : queryClient
24 // Unmount the query client on injector destroy
25 inject(DestroyRef).onDestroy(() => client.unmount())
26 client.mount()
27 return client
28 },
29 }
30}
31​
32/**
33 * Sets up providers necessary to enable TanStack Query functionality for Angular applications.
34 *
35 * Allows to configure a `QueryClient` and optional features such as developer tools.
36 *
37 * **Example - standalone**
38 *
39 * ```ts
40 * import {
41 * provideTanStackQuery,
42 * QueryClient,
43 * } from '@tanstack/angular-query-experimental'
44 *
45 * bootstrapApplication(AppComponent, {
46 * providers: [provideTanStackQuery(new QueryClient())],
47 * })
48 * ```
49 *
50 * **Example - NgModule-based**
51 *
52 * ```ts
53 * import {
54 * provideTanStackQuery,
55 * QueryClient,
56 * } from '@tanstack/angular-query-experimental'
57 *
58 * @NgModule({
59 * declarations: [AppComponent],
60 * imports: [BrowserModule],
61 * providers: [provideTanStackQuery(new QueryClient())],
62 * bootstrap: [AppComponent],
63 * })
64 * export class AppModule {}
65 * ```
66 *
67 * You can also enable optional developer tools by adding `withDevtools`. By
68 * default the tools will then be loaded when your app is in development mode.
69 * ```ts
70 * import {
71 * provideTanStackQuery,
72 * withDevtools
73 * QueryClient,
74 * } from '@tanstack/angular-query-experimental'
75 *
76 * bootstrapApplication(AppComponent,
77 * {
78 * providers: [
79 * provideTanStackQuery(new QueryClient(), withDevtools())
80 * ]
81 * }
82 * )
83 * ```
84 *
85 * **Example: using an InjectionToken**
86 *
87 * ```ts
88 * export const MY_QUERY_CLIENT = new InjectionToken('', {
89 * factory: () => new QueryClient(),
90 * })
91 *
92 * // In a lazy loaded route or lazy loaded component's providers array:
93 * providers: [provideTanStackQuery(MY_QUERY_CLIENT)]
94 * ```
95 * Using an InjectionToken for the QueryClient is an advanced optimization which allows TanStack Query to be absent from the main application bundle.
96 * This can be beneficial if you want to include TanStack Query on lazy loaded routes only while still sharing a `QueryClient`.
97 *
98 * Note that this is a small optimization and for most applications it's preferable to provide the `QueryClient` in the main application config.
99 * @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
100 * @param features - Optional features to configure additional Query functionality.
101 * @returns A set of providers to set up TanStack Query.
102 * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
103 * @see withDevtools
104 */
105export function provideTanStackQuery(
106 queryClient: QueryClient | InjectionToken<QueryClient>,
107 ...features: Array<QueryFeatures>
108): Array<Provider> {
109 return [
110 provideQueryClient(queryClient),
111 features.map((feature) => feature.ɵproviders),
112 ]
113}
114​
115/**
116 * Sets up providers necessary to enable TanStack Query functionality for Angular applications.
117 *
118 * Allows to configure a `QueryClient`.
119 * @param queryClient - A `QueryClient` instance.
120 * @returns A set of providers to set up TanStack Query.
121 * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
122 * @deprecated Use `provideTanStackQuery` instead.
123 */
124export function provideAngularQuery(queryClient: QueryClient): Array<Provider> {
125 return provideTanStackQuery(queryClient)
126}
127​
128const queryFeatures = ['Devtools', 'PersistQueryClient'] as const
129​
130type QueryFeatureKind = (typeof queryFeatures)[number]
131​
132/**
133 * Helper type to represent a Query feature.
134 */
135export interface QueryFeature<TFeatureKind extends QueryFeatureKind> {
136 ɵkind: TFeatureKind
137 ɵproviders: Array<Provider>
138}
139​
140/**
141 * Helper function to create an object that represents a Query feature.
142 * @param kind -
143 * @param providers -
144 * @returns A Query feature.
145 */
146export function queryFeature<TFeatureKind extends QueryFeatureKind>(
147 kind: TFeatureKind,
148 providers: Array<Provider>,
149): QueryFeature<TFeatureKind> {
150 return { ɵkind: kind, ɵproviders: providers }
151}
152​
153/**
154 * A type alias that represents a feature which enables developer tools.
155 * The type is used to describe the return value of the `withDevtools` function.
156 * @see {@link withDevtools}
157 */
158export type DevtoolsFeature = QueryFeature<'Devtools'>
159​
160/**
161 * A type alias that represents a feature which enables persistence.
162 * The type is used to describe the return value of the `withPersistQueryClient` function.
163 */
164export type PersistQueryClientFeature = QueryFeature<'PersistQueryClient'>
165​
166/**
167 * A type alias that represents all Query features available for use with `provideTanStackQuery`.
168 * Features can be enabled by adding special functions to the `provideTanStackQuery` call.
169 * See documentation for each symbol to find corresponding function name. See also `provideTanStackQuery`
170 * documentation on how to use those functions.
171 * @see {@link provideTanStackQuery}
172 */
173export type QueryFeatures = DevtoolsFeature | PersistQueryClientFeature