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