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