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