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

injectQuery.md

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