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

queryOptions.md

300 lines10.7 KB

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 `useQuery`. These options can be shared across hooks and imperative APIs such as `queryClient.query`. `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 `useQuery`. These options can be shared across hooks and imperative APIs such as `queryClient.query`. `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 `useQuery`. These options can be shared across hooks and imperative APIs such as `queryClient.query`. `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/preact-query/src/queryOptions.ts:139](https://github.com/TanStack/query/blob/main/packages/preact-query/src/queryOptions.ts#L139)
29​
30You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
31be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
32is 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 `useQuery`, with `initialData` set.
62​
63### Returns
64​
65[`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
66​
67The same options object, typed so that `queryKey` carries the inferred data type.
68​
69### See
70​
71 - [useQuery](useQuery.md) to run a query with these options.
72 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
73​
74### Example
75​
76```tsx
77import { queryOptions, useQuery } from '@tanstack/preact-query'
78​
79export const postsOptions = queryOptions({
80 queryKey: ['posts'],
81 queryFn: fetchPosts,
82 initialData: [],
83})
84​
85function Posts() {
86 // `data` is `Post[]`, never `undefined`, thanks to `initialData` β€” even if a refetch fails,
87 // so the list stays visible alongside the error.
88 const { data, isError, error } = useQuery(postsOptions)
89​
90 return (
91 <div>
92 {isError ? <span>Error: {error.message}</span> : null}
93 <ul>
94 {data.map((post) => <li key={post.id}>{post.title}</li>)}
95 </ul>
96 </div>
97 )
99```
100​
101<a id="call-signature-2"></a>
102​
103## Call Signature
104​
105```ts
106function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
107```
108​
109Defined in: [packages/preact-query/src/queryOptions.ts:178](https://github.com/TanStack/query/blob/main/packages/preact-query/src/queryOptions.ts#L178)
110​
111You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
112be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
113is the query key to generate options for.
114​
115### Type Parameters
116​
117#### TQueryFnData
118​
119`TQueryFnData` = `unknown`
120​
121#### TError
122​
123`TError` = `Error`
124​
125#### TData
126​
127`TData` = `TQueryFnData`
128​
129#### TQueryKey
130​
131`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
132​
133### Parameters
134​
135#### options
136​
137[`UnusedSkipTokenOptions`](../type-aliases/UnusedSkipTokenOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
138​
139The [UnusedSkipTokenOptions](../type-aliases/UnusedSkipTokenOptions.md) to use β€” everything you can pass to `useQuery`.
140​
141### Returns
142​
143[`UnusedSkipTokenOptions`](../type-aliases/UnusedSkipTokenOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
144​
145The same options object, typed so that `queryKey` carries the inferred data type.
146​
147### See
148​
149 - [useQuery](useQuery.md) to run a query with these options.
150 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
151​
152### Example
153​
154A parameterized factory, so the same options object can be reused per `id`:
155```tsx
156import { queryOptions, useQuery } from '@tanstack/preact-query'
157​
158export const postOptions = (id: string) =>
159 queryOptions({
160 queryKey: ['post', id],
161 queryFn: () => fetchPost(id),
162 })
163​
164function Post({ id }: { id: string }) {
165 const { data, isPending, isError, error } = useQuery(postOptions(id))
166​
167 if (isPending) return 'Loading...'
168 if (isError) return <span>Error: {error.message}</span>
169​
170 return <h1>{data.title}</h1>
172```
173​
174<a id="call-signature-3"></a>
175​
176## Call Signature
177​
178```ts
179function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
180```
181​
182Defined in: [packages/preact-query/src/queryOptions.ts:239](https://github.com/TanStack/query/blob/main/packages/preact-query/src/queryOptions.ts#L239)
183​
184You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
185be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
186is the query key to generate options for.
187​
188### Type Parameters
189​
190#### TQueryFnData
191​
192`TQueryFnData` = `unknown`
193​
194#### TError
195​
196`TError` = `Error`
197​
198#### TData
199​
200`TData` = `TQueryFnData`
201​
202#### TQueryKey
203​
204`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
205​
206### Parameters
207​
208#### options
209​
210[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
211​
212The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β€” everything you can pass to `useQuery`.
213​
214### Returns
215​
216[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
217​
218The same options object, typed so that `queryKey` carries the inferred data type.
219​
220### Remarks
221​
222This is the only overload that accepts `queryFn: skipToken`, shown below.
223​
224### See
225​
226 - [useQuery](useQuery.md) to run a query with these options.
227 - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
228​
229### Examples
230​
231A parameterized factory, so the same options object can be reused per `id`:
232```tsx
233import { queryOptions, useQuery } from '@tanstack/preact-query'
234​
235export const postOptions = (id: string) =>
236 queryOptions({
237 queryKey: ['post', id],
238 queryFn: () => fetchPost(id),
239 })
240​
241function Post({ id }: { id: string }) {
242 const { data, isPending, isError, error } = useQuery(postOptions(id))
243​
244 if (isPending) return 'Loading...'
245 if (isError) return <span>Error: {error.message}</span>
246​
247 return <h1>{data.title}</h1>
249```
250​
251A factory that disables the query, type safe, until `postId` is set:
252```tsx
253import { queryOptions, skipToken, useQuery } from '@tanstack/preact-query'
254​
255export const postOptions = (postId: number | undefined) =>
256 queryOptions({
257 queryKey: ['post', postId],
258 queryFn: postId != null ? () => fetchPost(postId) : skipToken,
259 })
260​
261function Post({ postId }: { postId: number | undefined }) {
262 const { data, isLoading, isError, error } = useQuery(postOptions(postId))
263​
264 if (postId == null) return 'Select a post'
265 if (isLoading) return 'Loading...'
266 if (isError) return <span>Error: {error.message}</span>
267​
268 return <h1>{data?.title}</h1>
270```
271​
272<a id="parameters-summary"></a>
273​
274## Parameters
275​
276### options
277​
278[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
279​
280The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β€” everything you can pass to `useQuery`.
281​
282<a id="options-properties"></a>
283​
284#### `options` properties
285​
286Built from [`UseQueryOptions`](../interfaces/UseQueryOptions.md#properties). See the type above for what it changes.
287​
288<a id="returns-summary"></a>
289​
290## Returns
291​
292[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
293​
294The same options object, typed so that `queryKey` carries the inferred data type.
295​
296<a id="result-properties"></a>
297​
298### Result properties
299​
300Built from [`UseQueryOptions`](../interfaces/UseQueryOptions.md#properties), [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md#properties). See the type above for what it changes.