Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

injectquery.md

329 lines7.5 KB

1---
2id: injectQuery
3title: injectQuery
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8# Function: injectQuery()
9​
10Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
11​
12**Basic example**
13​
14```ts
15class ServiceOrComponent {
16 query = injectQuery(() => ({
17 queryKey: ['repoData'],
18 queryFn: () =>
19 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
20 }))
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​
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 }))
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>(
59 injectQueryFn,
60 options?,
61): DefinedCreateQueryResult<TData, TError>
62```
63​
64Defined in: [inject-query.ts:65](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L65)
65​
66Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
67​
68**Basic example**
69​
70```ts
71class ServiceOrComponent {
72 query = injectQuery(() => ({
73 queryKey: ['repoData'],
74 queryFn: () =>
75 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
76 }))
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​
86```ts
87class ServiceOrComponent {
88 filter = signal('')
89​
90 todosQuery = injectQuery(() => ({
91 queryKey: ['todos', this.filter()],
92 queryFn: () => fetchTodos(this.filter()),
93 // Signals can be combined with expressions
94 enabled: !!this.filter(),
95 }))
97```
98​
99### Type Parameters
100​
101β€’ **TQueryFnData** = `unknown`
102​
103β€’ **TError** = `Error`
104​
105β€’ **TData** = `TQueryFnData`
106​
107β€’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
108​
109### Parameters
110​
111#### injectQueryFn
112​
113() => [`DefinedInitialDataOptions`](../../type-aliases/definedinitialdataoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
114​
115A function that returns query options.
116​
117#### options?
118​
119[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
120​
121Additional configuration
122​
123### Returns
124​
125[`DefinedCreateQueryResult`](../../type-aliases/definedcreatequeryresult.md)\<`TData`, `TError`\>
126​
127The query result.
128​
129The query result.
130​
131### Param
132​
133A function that returns query options.
134​
135### Param
136​
137Additional configuration
138​
139### See
140​
141https://tanstack.com/query/latest/docs/framework/angular/guides/queries
142​
143### See
144​
145https://tanstack.com/query/latest/docs/framework/angular/guides/queries
146​
147## Call Signature
148​
149```ts
150function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
151 injectQueryFn,
152 options?,
153): CreateQueryResult<TData, TError>
154```
155​
156Defined in: [inject-query.ts:116](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L116)
157​
158Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
159​
160**Basic example**
161​
162```ts
163class ServiceOrComponent {
164 query = injectQuery(() => ({
165 queryKey: ['repoData'],
166 queryFn: () =>
167 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
168 }))
170```
171​
172Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
173In the example below, the query will be automatically enabled and executed when the filter signal changes
174to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
175​
176**Reactive example**
177​
178```ts
179class ServiceOrComponent {
180 filter = signal('')
181​
182 todosQuery = injectQuery(() => ({
183 queryKey: ['todos', this.filter()],
184 queryFn: () => fetchTodos(this.filter()),
185 // Signals can be combined with expressions
186 enabled: !!this.filter(),
187 }))
189```
190​
191### Type Parameters
192​
193β€’ **TQueryFnData** = `unknown`
194​
195β€’ **TError** = `Error`
196​
197β€’ **TData** = `TQueryFnData`
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​
221The query result.
222​
223### Param
224​
225A function that returns query options.
226​
227### Param
228​
229Additional configuration
230​
231### See
232​
233https://tanstack.com/query/latest/docs/framework/angular/guides/queries
234​
235### See
236​
237https://tanstack.com/query/latest/docs/framework/angular/guides/queries
238​
239## Call Signature
240​
241```ts
242function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
243 injectQueryFn,
244 options?,
245): CreateQueryResult<TData, TError>
246```
247​
248Defined in: [inject-query.ts:167](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L167)
249​
250Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
251​
252**Basic example**
253​
254```ts
255class ServiceOrComponent {
256 query = injectQuery(() => ({
257 queryKey: ['repoData'],
258 queryFn: () =>
259 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
260 }))
262```
263​
264Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
265In the example below, the query will be automatically enabled and executed when the filter signal changes
266to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
267​
268**Reactive example**
269​
270```ts
271class ServiceOrComponent {
272 filter = signal('')
273​
274 todosQuery = injectQuery(() => ({
275 queryKey: ['todos', this.filter()],
276 queryFn: () => fetchTodos(this.filter()),
277 // Signals can be combined with expressions
278 enabled: !!this.filter(),
279 }))
281```
282​
283### Type Parameters
284​
285β€’ **TQueryFnData** = `unknown`
286​
287β€’ **TError** = `Error`
288​
289β€’ **TData** = `TQueryFnData`
290​
291β€’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
292​
293### Parameters
294​
295#### injectQueryFn
296​
297() => [`CreateQueryOptions`](../../interfaces/createqueryoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
298​
299A function that returns query options.
300​
301#### options?
302​
303[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
304​
305Additional configuration
306​
307### Returns
308​
309[`CreateQueryResult`](../../type-aliases/createqueryresult.md)\<`TData`, `TError`\>
310​
311The query result.
312​
313The query result.
314​
315### Param
316​
317A function that returns query options.
318​
319### Param
320​
321Additional configuration
322​
323### See
324​
325https://tanstack.com/query/latest/docs/framework/angular/guides/queries
326​
327### See
328​
329https://tanstack.com/query/latest/docs/framework/angular/guides/queries