Open History to see the last commit
1---
2id: injectQuery
3title: injectQuery
4---
5β
6# Function: injectQuery()
7β
8Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
9β
10**Basic example**
11β
12```ts
13class ServiceOrComponent {
14 query = injectQuery(() => ({
15 queryKey: ['repoData'],
16 queryFn: () =>
17 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
18 }))
19}
20```
21β
22Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
23In the example below, the query will be automatically enabled and executed when the filter signal changes
24to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
25β
26**Reactive example**
27β
28```ts
29class ServiceOrComponent {
30 filter = signal('')
31β
32 todosQuery = injectQuery(() => ({
33 queryKey: ['todos', this.filter()],
34 queryFn: () => fetchTodos(this.filter()),
35 // Signals can be combined with expressions
36 enabled: !!this.filter(),
37 }))
38}
39```
40β
41## Param
42β
43A function that returns query options.
44β
45## Param
46β
47Additional configuration
48β
49## See
50β
51https://tanstack.com/query/latest/docs/framework/angular/guides/queries
52β
53## Call Signature
54β
55```ts
56function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
57 injectQueryFn,
58 options?,
59): DefinedCreateQueryResult<TData, TError>
60```
61β
62Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
63β
64**Basic example**
65β
66```ts
67class ServiceOrComponent {
68 query = injectQuery(() => ({
69 queryKey: ['repoData'],
70 queryFn: () =>
71 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
72 }))
73}
74```
75β
76Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
77In the example below, the query will be automatically enabled and executed when the filter signal changes
78to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
79β
80**Reactive example**
81β
82```ts
83class ServiceOrComponent {
84 filter = signal('')
85β
86 todosQuery = injectQuery(() => ({
87 queryKey: ['todos', this.filter()],
88 queryFn: () => fetchTodos(this.filter()),
89 // Signals can be combined with expressions
90 enabled: !!this.filter(),
91 }))
92}
93```
94β
95### Type Parameters
96β
97β’ **TQueryFnData** = `unknown`
98β
99β’ **TError** = `Error`
100β
101β’ **TData** = `TQueryFnData`
102β
103β’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
104β
105### Parameters
106β
107#### injectQueryFn
108β
109() => [`DefinedInitialDataOptions`](../../type-aliases/definedinitialdataoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
110β
111A function that returns query options.
112β
113#### options?
114β
115[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
116β
117Additional configuration
118β
119### Returns
120β
121[`DefinedCreateQueryResult`](../../type-aliases/definedcreatequeryresult.md)\<`TData`, `TError`\>
122β
123The query result.
124β
125The query result.
126β
127### Param
128β
129A function that returns query options.
130β
131### Param
132β
133Additional configuration
134β
135### See
136β
137https://tanstack.com/query/latest/docs/framework/angular/guides/queries
138β
139### See
140β
141https://tanstack.com/query/latest/docs/framework/angular/guides/queries
142β
143### Defined in
144β
145[inject-query.ts:66](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L66)
146β
147## Call Signature
148β
149```ts
150function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
151 injectQueryFn,
152 options?,
153): CreateQueryResult<TData, TError>
154```
155β
156Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
157β
158**Basic example**
159β
160```ts
161class ServiceOrComponent {
162 query = injectQuery(() => ({
163 queryKey: ['repoData'],
164 queryFn: () =>
165 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
166 }))
167}
168```
169β
170Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
171In the example below, the query will be automatically enabled and executed when the filter signal changes
172to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
173β
174**Reactive example**
175β
176```ts
177class ServiceOrComponent {
178 filter = signal('')
179β
180 todosQuery = injectQuery(() => ({
181 queryKey: ['todos', this.filter()],
182 queryFn: () => fetchTodos(this.filter()),
183 // Signals can be combined with expressions
184 enabled: !!this.filter(),
185 }))
186}
187```
188β
189### Type Parameters
190β
191β’ **TQueryFnData** = `unknown`
192β
193β’ **TError** = `Error`
194β
195β’ **TData** = `TQueryFnData`
196β
197β’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
198β
199### Parameters
200β
201#### injectQueryFn
202β
203() => [`UndefinedInitialDataOptions`](../../type-aliases/undefinedinitialdataoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
204β
205A function that returns query options.
206β
207#### options?
208β
209[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
210β
211Additional configuration
212β
213### Returns
214β
215[`CreateQueryResult`](../../type-aliases/createqueryresult.md)\<`TData`, `TError`\>
216β
217The query result.
218β
219The query result.
220β
221### Param
222β
223A function that returns query options.
224β
225### Param
226β
227Additional configuration
228β
229### See
230β
231https://tanstack.com/query/latest/docs/framework/angular/guides/queries
232β
233### See
234β
235https://tanstack.com/query/latest/docs/framework/angular/guides/queries
236β
237### Defined in
238β
239[inject-query.ts:118](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L118)
240β
241## Call Signature
242β
243```ts
244function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
245 injectQueryFn,
246 options?,
247): CreateQueryResult<TData, TError>
248```
249β
250Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
251β
252**Basic example**
253β
254```ts
255class ServiceOrComponent {
256 query = injectQuery(() => ({
257 queryKey: ['repoData'],
258 queryFn: () =>
259 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
260 }))
261}
262```
263β
264Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
265In the example below, the query will be automatically enabled and executed when the filter signal changes
266to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
267β
268**Reactive example**
269β
270```ts
271class ServiceOrComponent {
272 filter = signal('')
273β
274 todosQuery = injectQuery(() => ({
275 queryKey: ['todos', this.filter()],
276 queryFn: () => fetchTodos(this.filter()),
277 // Signals can be combined with expressions
278 enabled: !!this.filter(),
279 }))
280}
281```
282β
283### Type Parameters
284β
285β’ **TQueryFnData** = `unknown`
286β
287β’ **TError** = `Error`
288β
289β’ **TData** = `TQueryFnData`
290β
291β’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
292β
293### Parameters
294β
295#### injectQueryFn
296β
297() => [`CreateQueryOptions`](../../interfaces/createqueryoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
298β
299A function that returns query options.
300β
301#### options?
302β
303[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
304β
305Additional configuration
306β
307### Returns
308β
309[`CreateQueryResult`](../../type-aliases/createqueryresult.md)\<`TData`, `TError`\>
310β
311The query result.
312β
313The query result.
314β
315### Param
316β
317A function that returns query options.
318β
319### Param
320β
321Additional configuration
322β
323### See
324β
325https://tanstack.com/query/latest/docs/framework/angular/guides/queries
326β
327### See
328β
329https://tanstack.com/query/latest/docs/framework/angular/guides/queries
330β
331### Defined in
332β
333[inject-query.ts:170](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L170)