Open History to see the last commit
1---
2id: injectQuery
3title: injectQuery
4---
5β
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7β
8# Function: injectQuery()
9β
10Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
11β
12**Basic example**
13β
14```ts
15class ServiceOrComponent {
16 query = injectQuery(() => ({
17 queryKey: ['repoData'],
18 queryFn: () =>
19 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
20 }))
21}
22```
23β
24Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
25In the example below, the query will be automatically enabled and executed when the filter signal changes
26to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
27β
28**Reactive example**
29β
30```ts
31class ServiceOrComponent {
32 filter = signal('')
33β
34 todosQuery = injectQuery(() => ({
35 queryKey: ['todos', this.filter()],
36 queryFn: () => fetchTodos(this.filter()),
37 // Signals can be combined with expressions
38 enabled: !!this.filter(),
39 }))
40}
41```
42β
43## Param
44β
45A function that returns query options.
46β
47## Param
48β
49Additional configuration
50β
51## See
52β
53https://tanstack.com/query/latest/docs/framework/angular/guides/queries
54β
55## Call Signature
56β
57```ts
58function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
59 injectQueryFn,
60 options?,
61): DefinedCreateQueryResult<TData, TError>
62```
63β
64Defined in: [inject-query.ts:65](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L65)
65β
66Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
67β
68**Basic example**
69β
70```ts
71class ServiceOrComponent {
72 query = injectQuery(() => ({
73 queryKey: ['repoData'],
74 queryFn: () =>
75 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
76 }))
77}
78```
79β
80Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
81In the example below, the query will be automatically enabled and executed when the filter signal changes
82to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
83β
84**Reactive example**
85β
86```ts
87class ServiceOrComponent {
88 filter = signal('')
89β
90 todosQuery = injectQuery(() => ({
91 queryKey: ['todos', this.filter()],
92 queryFn: () => fetchTodos(this.filter()),
93 // Signals can be combined with expressions
94 enabled: !!this.filter(),
95 }))
96}
97```
98β
99### Type Parameters
100β
101β’ **TQueryFnData** = `unknown`
102β
103β’ **TError** = `Error`
104β
105β’ **TData** = `TQueryFnData`
106β
107β’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
108β
109### Parameters
110β
111#### injectQueryFn
112β
113() => [`DefinedInitialDataOptions`](../../type-aliases/definedinitialdataoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
114β
115A function that returns query options.
116β
117#### options?
118β
119[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
120β
121Additional configuration
122β
123### Returns
124β
125[`DefinedCreateQueryResult`](../../type-aliases/definedcreatequeryresult.md)\<`TData`, `TError`\>
126β
127The query result.
128β
129The query result.
130β
131### Param
132β
133A function that returns query options.
134β
135### Param
136β
137Additional configuration
138β
139### See
140β
141https://tanstack.com/query/latest/docs/framework/angular/guides/queries
142β
143### See
144β
145https://tanstack.com/query/latest/docs/framework/angular/guides/queries
146β
147## Call Signature
148β
149```ts
150function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
151 injectQueryFn,
152 options?,
153): CreateQueryResult<TData, TError>
154```
155β
156Defined in: [inject-query.ts:116](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L116)
157β
158Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
159β
160**Basic example**
161β
162```ts
163class ServiceOrComponent {
164 query = injectQuery(() => ({
165 queryKey: ['repoData'],
166 queryFn: () =>
167 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
168 }))
169}
170```
171β
172Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
173In the example below, the query will be automatically enabled and executed when the filter signal changes
174to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
175β
176**Reactive example**
177β
178```ts
179class ServiceOrComponent {
180 filter = signal('')
181β
182 todosQuery = injectQuery(() => ({
183 queryKey: ['todos', this.filter()],
184 queryFn: () => fetchTodos(this.filter()),
185 // Signals can be combined with expressions
186 enabled: !!this.filter(),
187 }))
188}
189```
190β
191### Type Parameters
192β
193β’ **TQueryFnData** = `unknown`
194β
195β’ **TError** = `Error`
196β
197β’ **TData** = `TQueryFnData`
198β
199β’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
200β
201### Parameters
202β
203#### injectQueryFn
204β
205() => [`UndefinedInitialDataOptions`](../../type-aliases/undefinedinitialdataoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
206β
207A function that returns query options.
208β
209#### options?
210β
211[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
212β
213Additional configuration
214β
215### Returns
216β
217[`CreateQueryResult`](../../type-aliases/createqueryresult.md)\<`TData`, `TError`\>
218β
219The query result.
220β
221The query result.
222β
223### Param
224β
225A function that returns query options.
226β
227### Param
228β
229Additional configuration
230β
231### See
232β
233https://tanstack.com/query/latest/docs/framework/angular/guides/queries
234β
235### See
236β
237https://tanstack.com/query/latest/docs/framework/angular/guides/queries
238β
239## Call Signature
240β
241```ts
242function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
243 injectQueryFn,
244 options?,
245): CreateQueryResult<TData, TError>
246```
247β
248Defined in: [inject-query.ts:167](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L167)
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