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

inject-query.ts

129 lines3.4 KB

1import { QueryObserver } from '@tanstack/query-core'
2import {
3 Injector,
4 assertInInjectionContext,
5 inject,
6 runInInjectionContext,
7} from '@angular/core'
8import { createBaseQuery } from './create-base-query'
9import type { MethodKeys } from './signal-proxy'
10import type {
11 DefaultError,
12 QueryKey,
13 QueryObserverResult,
14} from '@tanstack/query-core'
15import type {
16 CreateQueryOptions,
17 CreateQueryResult,
18 DefinedCreateQueryResult,
19} from './types'
20import type {
21 DefinedInitialDataOptions,
22 UndefinedInitialDataOptions,
23} from './query-options'
24​
25export interface InjectQueryOptions {
26 /**
27 * The `Injector` in which to create the query.
28 *
29 * If this is not provided, the current injection context will be used instead (via `inject`).
30 */
31 injector?: Injector
33​
34export function injectQuery<
35 TQueryFnData = unknown,
36 TError = DefaultError,
37 TData = TQueryFnData,
38 TQueryKey extends QueryKey = QueryKey,
39>(
40 injectQueryFn: () => DefinedInitialDataOptions<
41 TQueryFnData,
42 TError,
43 TData,
44 TQueryKey
45 >,
46 options?: InjectQueryOptions,
47): DefinedCreateQueryResult<TData, TError>
48​
49export function injectQuery<
50 TQueryFnData = unknown,
51 TError = DefaultError,
52 TData = TQueryFnData,
53 TQueryKey extends QueryKey = QueryKey,
54>(
55 injectQueryFn: () => UndefinedInitialDataOptions<
56 TQueryFnData,
57 TError,
58 TData,
59 TQueryKey
60 >,
61 options?: InjectQueryOptions,
62): CreateQueryResult<TData, TError>
63​
64export function injectQuery<
65 TQueryFnData = unknown,
66 TError = DefaultError,
67 TData = TQueryFnData,
68 TQueryKey extends QueryKey = QueryKey,
69>(
70 injectQueryFn: () => CreateQueryOptions<
71 TQueryFnData,
72 TError,
73 TData,
74 TQueryKey
75 >,
76 options?: InjectQueryOptions,
77): CreateQueryResult<TData, TError>
78​
79/**
80 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
81 *
82 * **Basic example**
83 * ```ts
84 * import { lastValueFrom } from 'rxjs'
85 *
86 * class ServiceOrComponent {
87 * query = injectQuery(() => ({
88 * queryKey: ['repoData'],
89 * queryFn: () =>
90 * lastValueFrom(
91 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
92 * ),
93 * }))
94 * }
95 * ```
96 *
97 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
98 * In the example below, the query will be automatically enabled and executed when the filter signal changes
99 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
100 *
101 * **Reactive example**
102 * ```ts
103 * class ServiceOrComponent {
104 * filter = signal('')
105 *
106 * todosQuery = injectQuery(() => ({
107 * queryKey: ['todos', this.filter()],
108 * queryFn: () => fetchTodos(this.filter()),
109 * // Signals can be combined with expressions
110 * enabled: !!this.filter(),
111 * }))
112 * }
113 * ```
114 * @param injectQueryFn - A function that returns query options.
115 * @param options - Additional configuration.
116 * @returns The query result.
117 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
118 */
119export function injectQuery(
120 injectQueryFn: () => CreateQueryOptions,
121 options?: InjectQueryOptions,
122) {
123 !options?.injector && assertInInjectionContext(injectQuery)
124 return runInInjectionContext(options?.injector ?? inject(Injector), () =>
125 createBaseQuery(injectQueryFn, QueryObserver, methodsToExclude),
126 ) as unknown as CreateQueryResult
128​
129const methodsToExclude: Array<MethodKeys<QueryObserverResult>> = ['refetch']