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

injectQuery.md

243 lines6.9 KB

1---
2id: injectQuery
3title: injectQuery
4---
5​
6## Call Signature
7​
8```ts
9function injectQuery<TQueryFnData, TError, TData, TQueryKey>(optionsFn: () => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedCreateQueryResult<TData, TError>;
10```
11​
12Defined in: [packages/angular-query/src/inject-query.ts:61](https://github.com/TanStack/query/blob/main/packages/angular-query/src/inject-query.ts#L61)
13​
14This overload is selected when `initialData` is set on the options returned by `optionsFn`, so the
15resulting `data` signal is never `undefined` (unless a `select` changes `TData` to include `undefined`).
16​
17### Type Parameters
18​
19#### TQueryFnData
20​
21`TQueryFnData` = `unknown`
22​
23#### TError
24​
25`TError` = `Error`
26​
27#### TData
28​
29`TData` = `TQueryFnData`
30​
31#### TQueryKey
32​
33`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
34​
35### Parameters
36​
37#### optionsFn
38​
39() => [`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
40​
41A function returning the [DefinedInitialDataOptions](../type-aliases/DefinedInitialDataOptions.md) to use β€” everything you
42can pass to `injectQuery`, with `initialData` set. Similar to `computed` from Angular, this function runs
43in the reactive context, so signals read inside it (in `queryKey`, `enabled`, etc.) drive the query.
44​
45### Returns
46​
47[`DefinedCreateQueryResult`](../type-aliases/DefinedCreateQueryResult.md)\<`TData`, `TError`\>
48​
49The query result, typed so that `data` is never `undefined` (unless a `select` changes `TData` to
50include `undefined`).
51​
52### See
53​
54 - https://tanstack.com/query/latest/docs/framework/angular/guides/queries
55 - [queryOptions](queryOptions.md) to share these options between `injectQuery` and imperative APIs like
56`queryClient.query`.
57​
58### Example
59​
60```angular-ts
61@Component({
62 selector: 'posts',
63 template: `
64 <!-- `postsQuery.data()` is `Post[]`, never `undefined`, thanks to `initialData` β€” even if a
65 refetch fails, so the list stays visible alongside the error. -->
66 @if (postsQuery.isError()) {
67 <span>Error: {{ postsQuery.error()?.message }}</span>
68 }
69 <ul>
70 @for (post of postsQuery.data(); track post.id) {
71 <li>{{ post.title }}</li>
72 }
73 </ul>
74 `,
75})
76export class Posts {
77 readonly postsQuery = injectQuery(() => ({
78 queryKey: ['posts'],
79 queryFn: fetchPosts,
80 initialData: [],
81 }))
83```
84​
85## Call Signature
86​
87```ts
88function injectQuery<TQueryFnData, TError, TData, TQueryKey>(optionsFn: () => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): CreateQueryResult<TData, TError>;
89```
90​
91Defined in: [packages/angular-query/src/inject-query.ts:147](https://github.com/TanStack/query/blob/main/packages/angular-query/src/inject-query.ts#L147)
92​
93Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.
94​
95### Type Parameters
96​
97#### TQueryFnData
98​
99`TQueryFnData` = `unknown`
100​
101#### TError
102​
103`TError` = `Error`
104​
105#### TData
106​
107`TData` = `TQueryFnData`
108​
109#### TQueryKey
110​
111`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
112​
113### Parameters
114​
115#### optionsFn
116​
117() => [`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
118​
119A function returning the [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β€” everything
120you can pass to `injectQuery`. Similar to `computed` from Angular, this function runs in the reactive
121context, so signals read inside it (in `queryKey`, `enabled`, etc.) drive the query.
122​
123### Returns
124​
125[`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<`TData`, `TError`\>
126​
127The query result. `status()` is `'pending'` if there is no cached data to display, `'error'` if
128the last fetch attempt failed, or `'success'` if the query has data to display. `isPending`/`isSuccess`/
129`isError` are type-guard methods for convenience.
130​
131### See
132​
133 - https://tanstack.com/query/latest/docs/framework/angular/guides/queries
134 - [queryOptions](queryOptions.md) to share these options between `injectQuery` and imperative APIs like
135`queryClient.query`.
136​
137### Examples
138​
139```angular-ts
140@Component({
141 selector: 'posts',
142 template: `
143 @if (postsQuery.isPending()) {
144 Loading...
145 } @else if (postsQuery.isError()) {
146 <span>Error: {{ postsQuery.error()?.message }}</span>
147 } @else {
148 <ul>
149 @for (post of postsQuery.data(); track post.id) {
150 <li>{{ post.title }}</li>
151 }
152 </ul>
153 }
154 `,
155})
156export class Posts {
157 readonly postsQuery = injectQuery(() => ({
158 queryKey: ['posts'],
159 queryFn: fetchPosts,
160 }))
162```
163​
164Similar to `computed` from Angular, the function passed to `injectQuery` runs in the reactive context. In
165the example below, the query is automatically enabled and executed when the filter signal changes to a
166truthy value. When the filter signal changes back to a falsy value, the query is disabled.
167```angular-ts
168@Component({
169 selector: 'posts',
170 template: `
171 <input [ngModel]="filter()" (ngModelChange)="filter.set($event)" />
172 @if (postsQuery.isPending()) {
173 Loading...
174 } @else if (postsQuery.isError()) {
175 <span>Error: {{ postsQuery.error()?.message }}</span>
176 } @else {
177 <ul>
178 @for (post of postsQuery.data(); track post.id) {
179 <li>{{ post.title }}</li>
180 }
181 </ul>
182 }
183 `,
184})
185export class Posts {
186 readonly filter = signal('')
187​
188 readonly postsQuery = injectQuery(() => ({
189 queryKey: ['posts', this.filter()],
190 queryFn: () => fetchPosts(this.filter()),
191 enabled: !!this.filter(),
192 }))
194```
195​
196## Call Signature
197​
198```ts
199function injectQuery<TQueryFnData, TError, TData, TQueryKey>(optionsFn: () => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>): CreateQueryResult<TData, TError>;
200```
201​
202Defined in: [packages/angular-query/src/inject-query.ts:171](https://github.com/TanStack/query/blob/main/packages/angular-query/src/inject-query.ts#L171)
203​
204This overload accepts the general [CreateQueryOptions](../type-aliases/CreateQueryOptions.md) shape rather than the `initialData`-aware
205overloads above, so whether `data` is defined can't be inferred from the call site β€” useful when wrapping
206`injectQuery` in your own helper function that forwards caller-provided options.
207​
208### Type Parameters
209​
210#### TQueryFnData
211​
212`TQueryFnData` = `unknown`
213​
214#### TError
215​
216`TError` = `Error`
217​
218#### TData
219​
220`TData` = `TQueryFnData`
221​
222#### TQueryKey
223​
224`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
225​
226### Parameters
227​
228#### optionsFn
229​
230() => [`CreateQueryOptions`](../type-aliases/CreateQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
231​
232A function that returns query options. Similar to `computed` from Angular, this
233function runs in the reactive context, so signals read inside it drive the query.
234​
235### Returns
236​
237[`CreateQueryResult`](../type-aliases/CreateQueryResult.md)\<`TData`, `TError`\>
238​
239The query result.
240​
241### See
242​
243https://tanstack.com/query/latest/docs/framework/angular/guides/queries