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

injectQuery.md

308 lines7.2 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```ts
12import { lastValueFrom } from 'rxjs'
13​
14class ServiceOrComponent {
15 query = injectQuery(() => ({
16 queryKey: ['repoData'],
17 queryFn: () =>
18 lastValueFrom(
19 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
20 ),
21 }))
23```
24​
25Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
26In the example below, the query will be automatically enabled and executed when the filter signal changes
27to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
28​
29**Reactive example**
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>(injectQueryFn, options?): DefinedCreateQueryResult<TData, TError>;
59```
60​
61Defined in: [inject-query.ts:74](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L74)
62​
63Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
64​
65**Basic example**
66```ts
67import { lastValueFrom } from 'rxjs'
68​
69class ServiceOrComponent {
70 query = injectQuery(() => ({
71 queryKey: ['repoData'],
72 queryFn: () =>
73 lastValueFrom(
74 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
75 ),
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```ts
86class ServiceOrComponent {
87 filter = signal('')
88​
89 todosQuery = injectQuery(() => ({
90 queryKey: ['todos', this.filter()],
91 queryFn: () => fetchTodos(this.filter()),
92 // Signals can be combined with expressions
93 enabled: !!this.filter(),
94 }))
96```
97​
98### Type Parameters
99​
100#### TQueryFnData
101​
102`TQueryFnData` = `unknown`
103​
104#### TError
105​
106`TError` = `Error`
107​
108#### TData
109​
110`TData` = `TQueryFnData`
111​
112#### TQueryKey
113​
114`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
115​
116### Parameters
117​
118#### injectQueryFn
119​
120() => [`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
121​
122A function that returns query options.
123​
124#### options?
125​
126[`InjectQueryOptions`](../interfaces/InjectQueryOptions.md)
127​
128Additional configuration
129​
130### Returns
131​
132[`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<`TData`, `TError`\>
133​
134The query result.
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:129](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L129)
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
152import { lastValueFrom } from 'rxjs'
153​
154class ServiceOrComponent {
155 query = injectQuery(() => ({
156 queryKey: ['repoData'],
157 queryFn: () =>
158 lastValueFrom(
159 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
160 ),
161 }))
163```
164​
165Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
166In the example below, the query will be automatically enabled and executed when the filter signal changes
167to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
168​
169**Reactive example**
170```ts
171class ServiceOrComponent {
172 filter = signal('')
173​
174 todosQuery = injectQuery(() => ({
175 queryKey: ['todos', this.filter()],
176 queryFn: () => fetchTodos(this.filter()),
177 // Signals can be combined with expressions
178 enabled: !!this.filter(),
179 }))
181```
182​
183### Type Parameters
184​
185#### TQueryFnData
186​
187`TQueryFnData` = `unknown`
188​
189#### TError
190​
191`TError` = `Error`
192​
193#### TData
194​
195`TData` = `TQueryFnData`
196​
197#### TQueryKey
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​
221### See
222​
223https://tanstack.com/query/latest/docs/framework/angular/guides/queries
224​
225## Call Signature
226​
227```ts
228function injectQuery<TQueryFnData, TError, TData, TQueryKey>(injectQueryFn, options?): CreateQueryResult<TData, TError>;
229```
230​
231Defined in: [inject-query.ts:184](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-query.ts#L184)
232​
233Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
234​
235**Basic example**
236```ts
237import { lastValueFrom } from 'rxjs'
238​
239class ServiceOrComponent {
240 query = injectQuery(() => ({
241 queryKey: ['repoData'],
242 queryFn: () =>
243 lastValueFrom(
244 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
245 ),
246 }))
248```
249​
250Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
251In the example below, the query will be automatically enabled and executed when the filter signal changes
252to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
253​
254**Reactive example**
255```ts
256class ServiceOrComponent {
257 filter = signal('')
258​
259 todosQuery = injectQuery(() => ({
260 queryKey: ['todos', this.filter()],
261 queryFn: () => fetchTodos(this.filter()),
262 // Signals can be combined with expressions
263 enabled: !!this.filter(),
264 }))
266```
267​
268### Type Parameters
269​
270#### TQueryFnData
271​
272`TQueryFnData` = `unknown`
273​
274#### TError
275​
276`TError` = `Error`
277​
278#### TData
279​
280`TData` = `TQueryFnData`
281​
282#### TQueryKey
283​
284`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
285​
286### Parameters
287​
288#### injectQueryFn
289​
290() => [`CreateQueryOptions`](../type-aliases/CreateQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
291​
292A function that returns query options.
293​
294#### options?
295​
296[`InjectQueryOptions`](../interfaces/InjectQueryOptions.md)
297​
298Additional configuration
299​
300### Returns
301​
302[`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<`TData`, `TError`\>
303​
304The query result.
305​
306### See
307​
308https://tanstack.com/query/latest/docs/framework/angular/guides/queries