Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

providers.ts

170 lines6.0 KB

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 *
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 */
22export function provideQueryClient(
23 queryClient: QueryClient | InjectionToken<QueryClient>,
24): Provider {
25 return {
26 provide: QueryClient,
27 useFactory: () => {
28 const client =
29 queryClient instanceof InjectionToken
30 ? inject(queryClient)
31 : queryClient
32 // Unmount the query client on injector destroy
33 inject(DestroyRef).onDestroy(() => client.unmount())
34 client.mount()
35 return client
36 },
37 }
39​
40/**
41 * Sets up providers necessary to enable TanStack Query functionality for Angular applications. Allows
42 * configuring a `QueryClient` and optional features such as developer tools.
43 *
44 * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
45 * @see {@link withDevtools}
46 * @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
47 * @param features - Optional features to configure additional Query functionality.
48 * @returns A set of providers to set up TanStack Query.
49 *
50 * @example
51 * ```ts
52 * import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query-experimental'
53 *
54 * bootstrapApplication(AppComponent, {
55 * providers: [provideTanStackQuery(new QueryClient())],
56 * })
57 * ```
58 *
59 * @example
60 * The same, in an `NgModule`-based application:
61 * ```ts
62 * import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query-experimental'
63 *
64 * @NgModule({
65 * declarations: [AppComponent],
66 * imports: [BrowserModule],
67 * providers: [provideTanStackQuery(new QueryClient())],
68 * bootstrap: [AppComponent],
69 * })
70 * export class AppModule {}
71 * ```
72 *
73 * @example
74 * Enabling optional developer tools by adding `withDevtools` — by default, the tools are then loaded when
75 * your app is in development mode:
76 * ```ts
77 * import {
78 * provideTanStackQuery,
79 * withDevtools,
80 * QueryClient,
81 * } from '@tanstack/angular-query-experimental'
82 *
83 * bootstrapApplication(AppComponent, {
84 * providers: [provideTanStackQuery(new QueryClient(), withDevtools())],
85 * })
86 * ```
87 *
88 * @example
89 * Using an `InjectionToken` for the `QueryClient` — an advanced optimization that lets TanStack Query be
90 * absent from the main application bundle, useful for including it on lazy-loaded routes only while still
91 * sharing a `QueryClient`. This is a small optimization; for most applications it's preferable to provide
92 * the `QueryClient` in the main application config, as in the examples above:
93 * ```ts
94 * export const MY_QUERY_CLIENT = new InjectionToken('', {
95 * factory: () => new QueryClient(),
96 * })
97 *
98 * // In a lazy loaded route or lazy loaded component's providers array:
99 * providers: [provideTanStackQuery(MY_QUERY_CLIENT)]
100 * ```
101 */
102export function provideTanStackQuery(
103 queryClient: QueryClient | InjectionToken<QueryClient>,
104 ...features: Array<QueryFeatures>
105): Array<Provider> {
106 return [
107 provideQueryClient(queryClient),
108 features.map((feature) => feature.ɵproviders),
109 ]
111​
112/**
113 * Sets up providers necessary to enable TanStack Query functionality for Angular applications.
114 *
115 * Allows configuring a `QueryClient`.
116 * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
117 * @param queryClient - A `QueryClient` instance.
118 * @returns A set of providers to set up TanStack Query.
119 * @deprecated Use `provideTanStackQuery` instead.
120 */
121export function provideAngularQuery(queryClient: QueryClient): Array<Provider> {
122 return provideTanStackQuery(queryClient)
124​
125const queryFeatures = ['Devtools', 'PersistQueryClient'] as const
126​
127type QueryFeatureKind = (typeof queryFeatures)[number]
128​
129/**
130 * Helper type to represent a Query feature.
131 */
132export interface QueryFeature<TFeatureKind extends QueryFeatureKind> {
133 ɵkind: TFeatureKind
134 ɵproviders: Array<Provider>
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 }
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