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

injectquery.md

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