Open History to see the last commit
1---
2id: queryOptions
3title: queryOptions
4---
5β
6## Overview
7β
8```ts
9function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
10function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
11function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
12```
13β
14- [`DefinedInitialDataOptions` β `DefinedInitialDataOptions & QueryKeyWithDataTag`](#call-signature-1): You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is the query key to generate options for.
15- [`UnusedSkipTokenOptions` β `UnusedSkipTokenOptions & QueryKeyWithDataTag`](#call-signature-2): You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is the query key to generate options for.
16- [`UndefinedInitialDataOptions` β `UndefinedInitialDataOptions & QueryKeyWithDataTag`](#call-signature-3): You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is the query key to generate options for.
17β
18See also: [Parameters](#parameters-summary) Β· [Returns](#returns-summary)
19β
20<a id="call-signature-1"></a>
21β
22## Call Signature
23β
24```ts
25function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
26```
27β
28Defined in: [packages/angular-query-experimental/src/query-options.ts:146](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/query-options.ts#L146)
29β
30You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options
31can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is
32required and is the query key to generate options for.
33β
34This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
35a `select` changes `TData` to include `undefined`).
36β
37### Type Parameters
38β
39#### TQueryFnData
40β
41`TQueryFnData` = `unknown`
42β
43#### TError
44β
45`TError` = `Error`
46β
47#### TData
48β
49`TData` = `TQueryFnData`
50β
51#### TQueryKey
52β
53`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
54β
55### Parameters
56β
57#### options
58β
59[`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
60β
61The [DefinedInitialDataOptions](../type-aliases/DefinedInitialDataOptions.md) to use β everything you can pass to `injectQuery`,
62with `initialData` set.
63β
64### Returns
65β
66[`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
67β
68The same options object, typed so that `queryKey` carries the inferred data type.
69β
70### See
71β
72 - [injectQuery](injectQuery.md) to run a query with these options.
73 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
74β
75### Example
76β
77```angular-ts
78import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'
79β
80export const postsOptions = queryOptions({
81 queryKey: ['posts'],
82 queryFn: fetchPosts,
83 initialData: [],
84})
85β
86@Component({
87 selector: 'posts',
88 template: `
89 <!-- `postsQuery.data()` is never `undefined`, thanks to `initialData` β even if a refetch
90 fails, so the list stays visible alongside the error. -->
91 @if (postsQuery.isError()) {
92 <span>Error: {{ postsQuery.error()?.message }}</span>
93 }
94 <ul>
95 @for (post of postsQuery.data(); track post.id) {
96 <li>{{ post.title }}</li>
97 }
98 </ul>
99 `,
100})
101export class Posts {
102 readonly postsQuery = injectQuery(() => postsOptions)
103}
104```
105β
106<a id="call-signature-2"></a>
107β
108## Call Signature
109β
110```ts
111function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
112```
113β
114Defined in: [packages/angular-query-experimental/src/query-options.ts:193](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/query-options.ts#L193)
115β
116You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options
117can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is
118required and is the query key to generate options for.
119β
120### Type Parameters
121β
122#### TQueryFnData
123β
124`TQueryFnData` = `unknown`
125β
126#### TError
127β
128`TError` = `Error`
129β
130#### TData
131β
132`TData` = `TQueryFnData`
133β
134#### TQueryKey
135β
136`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
137β
138### Parameters
139β
140#### options
141β
142[`UnusedSkipTokenOptions`](../type-aliases/UnusedSkipTokenOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
143β
144The [UnusedSkipTokenOptions](../type-aliases/UnusedSkipTokenOptions.md) to use β everything you can pass to `injectQuery`.
145β
146### Returns
147β
148[`UnusedSkipTokenOptions`](../type-aliases/UnusedSkipTokenOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
149β
150The same options object, typed so that `queryKey` carries the inferred data type.
151β
152### See
153β
154 - [injectQuery](injectQuery.md) to run a query with these options.
155 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
156β
157### Example
158β
159A parameterized factory, so the same options object can be reused per `id`:
160```angular-ts
161import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'
162β
163export const postOptions = (id: string) =>
164 queryOptions({
165 queryKey: ['post', id],
166 queryFn: () => fetchPost(id),
167 })
168β
169@Component({
170 selector: 'post',
171 template: `
172 @if (postQuery.isPending()) {
173 Loading...
174 } @else if (postQuery.isError()) {
175 <span>Error: {{ postQuery.error()?.message }}</span>
176 } @else {
177 <h1>{{ postQuery.data().title }}</h1>
178 }
179 `,
180})
181export class Post {
182 readonly id = signal('1')
183 readonly postQuery = injectQuery(() => postOptions(this.id()))
184}
185```
186β
187<a id="call-signature-3"></a>
188β
189## Call Signature
190β
191```ts
192function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
193```
194β
195Defined in: [packages/angular-query-experimental/src/query-options.ts:271](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/query-options.ts#L271)
196β
197You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options
198can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is
199required and is the query key to generate options for.
200β
201### Type Parameters
202β
203#### TQueryFnData
204β
205`TQueryFnData` = `unknown`
206β
207#### TError
208β
209`TError` = `Error`
210β
211#### TData
212β
213`TData` = `TQueryFnData`
214β
215#### TQueryKey
216β
217`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
218β
219### Parameters
220β
221#### options
222β
223[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
224β
225The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β everything you can pass to `injectQuery`.
226β
227### Returns
228β
229[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
230β
231The same options object, typed so that `queryKey` carries the inferred data type.
232β
233### Remarks
234β
235This is the only overload that accepts `queryFn: skipToken`, shown below.
236β
237### See
238β
239 - [injectQuery](injectQuery.md) to run a query with these options.
240 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
241β
242### Examples
243β
244A parameterized factory, so the same options object can be reused per `id`:
245```angular-ts
246import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'
247β
248export const postOptions = (id: string) =>
249 queryOptions({
250 queryKey: ['post', id],
251 queryFn: () => fetchPost(id),
252 })
253β
254@Component({
255 selector: 'post',
256 template: `
257 @if (postQuery.isPending()) {
258 Loading...
259 } @else if (postQuery.isError()) {
260 <span>Error: {{ postQuery.error()?.message }}</span>
261 } @else {
262 <h1>{{ postQuery.data().title }}</h1>
263 }
264 `,
265})
266export class Post {
267 readonly id = signal('1')
268 readonly postQuery = injectQuery(() => postOptions(this.id()))
269}
270```
271β
272A factory that disables the query, type safe, until `postId` is set:
273```angular-ts
274import { queryOptions, skipToken, injectQuery } from '@tanstack/angular-query-experimental'
275β
276export const postOptions = (postId: number | undefined) =>
277 queryOptions({
278 queryKey: ['post', postId],
279 queryFn: postId != null ? () => fetchPost(postId) : skipToken,
280 })
281β
282@Component({
283 selector: 'post',
284 template: `
285 @if (postId() == null) {
286 Select a post
287 } @else if (postQuery.isPending()) {
288 Loading...
289 } @else if (postQuery.isError()) {
290 <span>Error: {{ postQuery.error()?.message }}</span>
291 } @else {
292 <h1>{{ postQuery.data().title }}</h1>
293 }
294 `,
295})
296export class Post {
297 readonly postId = signal<number | undefined>(undefined)
298 readonly postQuery = injectQuery(() => postOptions(this.postId()))
299}
300```
301β
302<a id="parameters-summary"></a>
303β
304## Parameters
305β
306### options
307β
308[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
309β
310The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β everything you can pass to `injectQuery`.
311β
312<a id="options-properties"></a>
313β
314#### `options` properties
315β
316Built from [`CreateQueryOptions`](../interfaces/CreateQueryOptions.md#properties). See the type above for what it changes.
317β
318<a id="returns-summary"></a>
319β
320## Returns
321β
322[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
323β
324The same options object, typed so that `queryKey` carries the inferred data type.
325β
326<a id="result-properties"></a>
327β
328### Result properties
329β
330Built from [`CreateQueryOptions`](../interfaces/CreateQueryOptions.md#properties), [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md#properties). See the type above for what it changes.