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