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