Open History to see the last commit
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
32}
33β
34/**
35 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
36 *
37 * **Basic example**
38 * ```ts
39 * import { lastValueFrom } from 'rxjs'
40 *
41 * class ServiceOrComponent {
42 * query = injectQuery(() => ({
43 * queryKey: ['repoData'],
44 * queryFn: () =>
45 * lastValueFrom(
46 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
47 * ),
48 * }))
49 * }
50 * ```
51 *
52 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
53 * In the example below, the query will be automatically enabled and executed when the filter signal changes
54 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
55 *
56 * **Reactive example**
57 * ```ts
58 * class ServiceOrComponent {
59 * filter = signal('')
60 *
61 * todosQuery = injectQuery(() => ({
62 * queryKey: ['todos', this.filter()],
63 * queryFn: () => fetchTodos(this.filter()),
64 * // Signals can be combined with expressions
65 * enabled: !!this.filter(),
66 * }))
67 * }
68 * ```
69 * @param injectQueryFn - A function that returns query options.
70 * @param options - Additional configuration
71 * @returns The query result.
72 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
73 */
74export function injectQuery<
75 TQueryFnData = unknown,
76 TError = DefaultError,
77 TData = TQueryFnData,
78 TQueryKey extends QueryKey = QueryKey,
79>(
80 injectQueryFn: () => DefinedInitialDataOptions<
81 TQueryFnData,
82 TError,
83 TData,
84 TQueryKey
85 >,
86 options?: InjectQueryOptions,
87): DefinedCreateQueryResult<TData, TError>
88β
89/**
90 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
91 *
92 * **Basic example**
93 * ```ts
94 * import { lastValueFrom } from 'rxjs'
95 *
96 * class ServiceOrComponent {
97 * query = injectQuery(() => ({
98 * queryKey: ['repoData'],
99 * queryFn: () =>
100 * lastValueFrom(
101 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
102 * ),
103 * }))
104 * }
105 * ```
106 *
107 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
108 * In the example below, the query will be automatically enabled and executed when the filter signal changes
109 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
110 *
111 * **Reactive example**
112 * ```ts
113 * class ServiceOrComponent {
114 * filter = signal('')
115 *
116 * todosQuery = injectQuery(() => ({
117 * queryKey: ['todos', this.filter()],
118 * queryFn: () => fetchTodos(this.filter()),
119 * // Signals can be combined with expressions
120 * enabled: !!this.filter(),
121 * }))
122 * }
123 * ```
124 * @param injectQueryFn - A function that returns query options.
125 * @param options - Additional configuration
126 * @returns The query result.
127 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
128 */
129export function injectQuery<
130 TQueryFnData = unknown,
131 TError = DefaultError,
132 TData = TQueryFnData,
133 TQueryKey extends QueryKey = QueryKey,
134>(
135 injectQueryFn: () => UndefinedInitialDataOptions<
136 TQueryFnData,
137 TError,
138 TData,
139 TQueryKey
140 >,
141 options?: InjectQueryOptions,
142): CreateQueryResult<TData, TError>
143β
144/**
145 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
146 *
147 * **Basic example**
148 * ```ts
149 * import { lastValueFrom } from 'rxjs'
150 *
151 * class ServiceOrComponent {
152 * query = injectQuery(() => ({
153 * queryKey: ['repoData'],
154 * queryFn: () =>
155 * lastValueFrom(
156 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
157 * ),
158 * }))
159 * }
160 * ```
161 *
162 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
163 * In the example below, the query will be automatically enabled and executed when the filter signal changes
164 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
165 *
166 * **Reactive example**
167 * ```ts
168 * class ServiceOrComponent {
169 * filter = signal('')
170 *
171 * todosQuery = injectQuery(() => ({
172 * queryKey: ['todos', this.filter()],
173 * queryFn: () => fetchTodos(this.filter()),
174 * // Signals can be combined with expressions
175 * enabled: !!this.filter(),
176 * }))
177 * }
178 * ```
179 * @param injectQueryFn - A function that returns query options.
180 * @param options - Additional configuration
181 * @returns The query result.
182 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
183 */
184export function injectQuery<
185 TQueryFnData = unknown,
186 TError = DefaultError,
187 TData = TQueryFnData,
188 TQueryKey extends QueryKey = QueryKey,
189>(
190 injectQueryFn: () => CreateQueryOptions<
191 TQueryFnData,
192 TError,
193 TData,
194 TQueryKey
195 >,
196 options?: InjectQueryOptions,
197): CreateQueryResult<TData, TError>
198β
199/**
200 * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
201 *
202 * **Basic example**
203 * ```ts
204 * import { lastValueFrom } from 'rxjs'
205 *
206 * class ServiceOrComponent {
207 * query = injectQuery(() => ({
208 * queryKey: ['repoData'],
209 * queryFn: () =>
210 * lastValueFrom(
211 * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
212 * ),
213 * }))
214 * }
215 * ```
216 *
217 * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
218 * In the example below, the query will be automatically enabled and executed when the filter signal changes
219 * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
220 *
221 * **Reactive example**
222 * ```ts
223 * class ServiceOrComponent {
224 * filter = signal('')
225 *
226 * todosQuery = injectQuery(() => ({
227 * queryKey: ['todos', this.filter()],
228 * queryFn: () => fetchTodos(this.filter()),
229 * // Signals can be combined with expressions
230 * enabled: !!this.filter(),
231 * }))
232 * }
233 * ```
234 * @param injectQueryFn - A function that returns query options.
235 * @param options - Additional configuration
236 * @returns The query result.
237 * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries
238 */
239export function injectQuery(
240 injectQueryFn: () => CreateQueryOptions,
241 options?: InjectQueryOptions,
242) {
243 !options?.injector && assertInInjectionContext(injectQuery)
244 return runInInjectionContext(options?.injector ?? inject(Injector), () =>
245 createBaseQuery(injectQueryFn, QueryObserver, methodsToExclude),
246 ) as unknown as CreateQueryResult
247}
248β
249const methodsToExclude: Array<MethodKeys<QueryObserverResult>> = ['refetch']