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