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