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

injectquery.md

333 lines7.4 KB

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 }))
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 }))
39```
40​
41## Param
42​
43A function that returns query options.
44​
45## Param
46​
47Additional configuration
48​
49## See
50​
51https://tanstack.com/query/latest/docs/framework/angular/guides/queries
52​
53## Call Signature
54​
55```ts
56function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
57 injectQueryFn,
58 options?,
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 }))
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 }))
93```
94​
95### Type Parameters
96​
97β€’ **TQueryFnData** = `unknown`
98​
99β€’ **TError** = `Error`
100​
101β€’ **TData** = `TQueryFnData`
102​
103β€’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
104​
105### Parameters
106​
107#### injectQueryFn
108​
109() => [`DefinedInitialDataOptions`](../../type-aliases/definedinitialdataoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
110​
111A function that returns query options.
112​
113#### options?
114​
115[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
116​
117Additional configuration
118​
119### Returns
120​
121[`DefinedCreateQueryResult`](../../type-aliases/definedcreatequeryresult.md)\<`TData`, `TError`\>
122​
123The query result.
124​
125The query result.
126​
127### Param
128​
129A function that returns query options.
130​
131### Param
132​
133Additional configuration
134​
135### See
136​
137https://tanstack.com/query/latest/docs/framework/angular/guides/queries
138​
139### See
140​
141https://tanstack.com/query/latest/docs/framework/angular/guides/queries
142​
143### Defined in
144​
145[inject-query.ts:66](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L66)
146​
147## Call Signature
148​
149```ts
150function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
151 injectQueryFn,
152 options?,
153): CreateQueryResult<TData, TError>
154```
155​
156Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
157​
158**Basic example**
159​
160```ts
161class ServiceOrComponent {
162 query = injectQuery(() => ({
163 queryKey: ['repoData'],
164 queryFn: () =>
165 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
166 }))
168```
169​
170Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
171In the example below, the query will be automatically enabled and executed when the filter signal changes
172to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
173​
174**Reactive example**
175​
176```ts
177class ServiceOrComponent {
178 filter = signal('')
179​
180 todosQuery = injectQuery(() => ({
181 queryKey: ['todos', this.filter()],
182 queryFn: () => fetchTodos(this.filter()),
183 // Signals can be combined with expressions
184 enabled: !!this.filter(),
185 }))
187```
188​
189### Type Parameters
190​
191β€’ **TQueryFnData** = `unknown`
192​
193β€’ **TError** = `Error`
194​
195β€’ **TData** = `TQueryFnData`
196​
197β€’ **TQueryKey** _extends_ readonly `unknown`[] = readonly `unknown`[]
198​
199### Parameters
200​
201#### injectQueryFn
202​
203() => [`UndefinedInitialDataOptions`](../../type-aliases/undefinedinitialdataoptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
204​
205A function that returns query options.
206​
207#### options?
208​
209[`InjectQueryOptions`](../../interfaces/injectqueryoptions.md)
210​
211Additional configuration
212​
213### Returns
214​
215[`CreateQueryResult`](../../type-aliases/createqueryresult.md)\<`TData`, `TError`\>
216​
217The query result.
218​
219The query result.
220​
221### Param
222​
223A function that returns query options.
224​
225### Param
226​
227Additional configuration
228​
229### See
230​
231https://tanstack.com/query/latest/docs/framework/angular/guides/queries
232​
233### See
234​
235https://tanstack.com/query/latest/docs/framework/angular/guides/queries
236​
237### Defined in
238​
239[inject-query.ts:118](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L118)
240​
241## Call Signature
242​
243```ts
244function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
245 injectQueryFn,
246 options?,
247): CreateQueryResult<TData, TError>
248```
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
330​
331### Defined in
332​
333[inject-query.ts:170](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L170)