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

providers.ts

65 lines1.6 KB

1import {
2 DestroyRef,
3 ENVIRONMENT_INITIALIZER,
4 inject,
5 makeEnvironmentProviders,
6} from '@angular/core'
7import { provideQueryClient } from './inject-query-client'
8import type { EnvironmentProviders } from '@angular/core'
9import type { QueryClient } from '@tanstack/query-core'
10​
11/**
12 * Sets up providers necessary to enable TanStack Query functionality for Angular applications.
13 *
14 * Allows to configure a `QueryClient`.
15 *
16 * **Example - standalone**
17 *
18 * ```ts
19 * import {
20 * provideAngularQuery,
21 * QueryClient,
22 * } from '@tanstack/angular-query-experimental'
23 *
24 * bootstrapApplication(AppComponent, {
25 * providers: [provideAngularQuery(new QueryClient())],
26 * })
27 * ```
28 *
29 * **Example - NgModule-based**
30 *
31 * ```ts
32 * import {
33 * provideAngularQuery,
34 * QueryClient,
35 * } from '@tanstack/angular-query-experimental'
36 *
37 * @NgModule({
38 * declarations: [AppComponent],
39 * imports: [BrowserModule],
40 * providers: [provideAngularQuery(new QueryClient())],
41 * bootstrap: [AppComponent],
42 * })
43 * export class AppModule {}
44 * ```
45 * @param queryClient - A `QueryClient` instance.
46 * @returns A set of providers to set up TanStack Query.
47 * @public
48 * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start
49 */
50export function provideAngularQuery(
51 queryClient: QueryClient,
52): EnvironmentProviders {
53 return makeEnvironmentProviders([
54 provideQueryClient(queryClient),
55 {
56 provide: ENVIRONMENT_INITIALIZER,
57 multi: true,
58 useValue: () => {
59 queryClient.mount()
60 // Unmount the query client on application destroy
61 inject(DestroyRef).onDestroy(() => queryClient.unmount())
62 },
63 },
64 ])