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

injectQuery.md

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