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