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

injectQuery.md

279 lines6.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​
47The Angular injector to use.
48​
49## See
50​
51https://tanstack.com/query/latest/docs/framework/angular/guides/queries
52​
53## injectQuery(optionsFn, injector)
54​
55```ts
56function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
57 optionsFn,
58 injector?,
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_ `QueryKey` = `QueryKey`
104​
105### Parameters
106​
107• **optionsFn**
108​
109A function that returns query options.
110​
111• **injector?**: `Injector`
112​
113The Angular injector to use.
114​
115### Returns
116​
117[`DefinedCreateQueryResult`](DefinedCreateQueryResult.md)\<`TData`, `TError`\>
118​
119The query result.
120​
121### See
122​
123https://tanstack.com/query/latest/docs/framework/angular/guides/queries
124​
125### Defined in
126​
127[inject-query.ts:53](https://github.com/TanStack/query/blob/27861961bbb36e9bc25fcd45cff21b5645f02f9b/packages/angular-query-experimental/src/inject-query.ts#L53)
128​
129## injectQuery(optionsFn, injector)
130​
131```ts
132function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
133 optionsFn,
134 injector?,
135): CreateQueryResult<TData, TError>
136```
137​
138Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
139​
140**Basic example**
141​
142```ts
143class ServiceOrComponent {
144 query = injectQuery(() => ({
145 queryKey: ['repoData'],
146 queryFn: () =>
147 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
148 }))
150```
151​
152Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
153In the example below, the query will be automatically enabled and executed when the filter signal changes
154to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
155​
156**Reactive example**
157​
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** = `unknown`
174​
175• **TError** = `Error`
176​
177• **TData** = `TQueryFnData`
178​
179• **TQueryKey** _extends_ `QueryKey` = `QueryKey`
180​
181### Parameters
182​
183• **optionsFn**
184​
185A function that returns query options.
186​
187• **injector?**: `Injector`
188​
189The Angular injector to use.
190​
191### Returns
192​
193[`CreateQueryResult`](CreateQueryResult.md)\<`TData`, `TError`\>
194​
195The query result.
196​
197### See
198​
199https://tanstack.com/query/latest/docs/framework/angular/guides/queries
200​
201### Defined in
202​
203[inject-query.ts:102](https://github.com/TanStack/query/blob/27861961bbb36e9bc25fcd45cff21b5645f02f9b/packages/angular-query-experimental/src/inject-query.ts#L102)
204​
205## injectQuery(optionsFn, injector)
206​
207```ts
208function injectQuery<TQueryFnData, TError, TData, TQueryKey>(
209 optionsFn,
210 injector?,
211): CreateQueryResult<TData, TError>
212```
213​
214Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
215​
216**Basic example**
217​
218```ts
219class ServiceOrComponent {
220 query = injectQuery(() => ({
221 queryKey: ['repoData'],
222 queryFn: () =>
223 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
224 }))
226```
227​
228Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.
229In the example below, the query will be automatically enabled and executed when the filter signal changes
230to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.
231​
232**Reactive example**
233​
234```ts
235class ServiceOrComponent {
236 filter = signal('')
237​
238 todosQuery = injectQuery(() => ({
239 queryKey: ['todos', this.filter()],
240 queryFn: () => fetchTodos(this.filter()),
241 // Signals can be combined with expressions
242 enabled: !!this.filter(),
243 }))
245```
246​
247### Type Parameters
248​
249• **TQueryFnData** = `unknown`
250​
251• **TError** = `Error`
252​
253• **TData** = `TQueryFnData`
254​
255• **TQueryKey** _extends_ `QueryKey` = `QueryKey`
256​
257### Parameters
258​
259• **optionsFn**
260​
261A function that returns query options.
262​
263• **injector?**: `Injector`
264​
265The Angular injector to use.
266​
267### Returns
268​
269[`CreateQueryResult`](CreateQueryResult.md)\<`TData`, `TError`\>
270​
271The query result.
272​
273### See
274​
275https://tanstack.com/query/latest/docs/framework/angular/guides/queries
276​
277### Defined in
278​
279[inject-query.ts:151](https://github.com/TanStack/query/blob/27861961bbb36e9bc25fcd45cff21b5645f02f9b/packages/angular-query-experimental/src/inject-query.ts#L151)