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

queryOptions.md

345 lines12.3 KB

1---
2id: queryOptions
3title: queryOptions
4redirect_from:
5 - framework/vue/reference/queryOptions
6---
7​
8## Overview
9​
10```ts
11function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialQueryOptionsWithDataTag<TQueryFnData, TError, TData, TQueryKey>;
12function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: () => DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>): () => DefinedInitialQueryOptionsWithDataTag<TQueryFnData, TError, TData, TQueryKey>;
13function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialQueryOptionsWithDataTag<TQueryFnData, TError, TData, TQueryKey>;
14function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: () => UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>): () => UndefinedInitialQueryOptionsWithDataTag<TQueryFnData, TError, TData, TQueryKey>;
15```
16​
17- [`DefinedInitialQueryOptions` β†’ `DefinedInitialQueryOptionsWithDataTag`](#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.
18- [`() => DefinedInitialQueryOptions` β†’ `() => DefinedInitialQueryOptionsWithDataTag`](#call-signature-2): Same as the plain-object overload, but for options that close over reactive state (`ref`s read inside the function body). Wrap them in a getter so `queryClient` methods like `invalidateQueries`/`fetchQuery` always read the current values instead of the ones captured when the options were created.
19- [`UndefinedInitialQueryOptions` β†’ `UndefinedInitialQueryOptionsWithDataTag`](#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.
20- [`() => UndefinedInitialQueryOptions` β†’ `() => UndefinedInitialQueryOptionsWithDataTag`](#call-signature-4): Same as the plain-object overload, but for options that close over reactive state (`ref`s read inside the function body). Wrap them in a getter so the `queryKey` β€” and anything else derived from a `ref` β€” reacts to changes, and so `queryClient` methods like `invalidateQueries`/`fetchQuery` always read the current values instead of the ones captured when the options were created.
21​
22See also: [Parameters](#parameters-summary) Β· [Returns](#returns-summary)
23​
24<a id="call-signature-1"></a>
25​
26## Call Signature
27​
28```ts
29function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialQueryOptionsWithDataTag<TQueryFnData, TError, TData, TQueryKey>;
30```
31​
32Defined in: [packages/vue-query/src/queryOptions.ts:251](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryOptions.ts#L251)
33​
34You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
35be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
36is the query key to generate options for.
37​
38This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
39a `select` changes `TData` to include `undefined`).
40​
41### Type Parameters
42​
43#### TQueryFnData
44​
45`TQueryFnData` = `unknown`
46​
47#### TError
48​
49`TError` = `Error`
50​
51#### TData
52​
53`TData` = `TQueryFnData`
54​
55#### TQueryKey
56​
57`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
58​
59### Parameters
60​
61#### options
62​
63[`DefinedInitialQueryOptions`](../type-aliases/DefinedInitialQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
64​
65The [DefinedInitialQueryOptions](../type-aliases/DefinedInitialQueryOptions.md) to use β€” everything you can pass to `useQuery`, with
66`initialData` set.
67​
68### Returns
69​
70[`DefinedInitialQueryOptionsWithDataTag`](../type-aliases/DefinedInitialQueryOptionsWithDataTag.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
71​
72The same options object, typed so that `queryKey` carries the inferred data type.
73​
74### See
75​
76[useQuery](useQuery.md) to run a query with these options.
77​
78### Example
79​
80```vue
81<script setup lang="ts">
82import { queryOptions, useQuery } from '@tanstack/vue-query'
83​
84const postsOptions = queryOptions({
85 queryKey: ['posts'],
86 queryFn: fetchPosts,
87 initialData: [],
88})
89​
90// `data` is `Post[]`, never `undefined`, thanks to `initialData` β€” even if a refetch fails,
91// so the list stays visible alongside the error.
92const { data, isError, error } = useQuery(postsOptions)
93</script>
94```
95​
96<a id="call-signature-2"></a>
97​
98## Call Signature
99​
100```ts
101function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: () => DefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>): () => DefinedInitialQueryOptionsWithDataTag<TQueryFnData, TError, TData, TQueryKey>;
102```
103​
104Defined in: [packages/vue-query/src/queryOptions.ts:286](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryOptions.ts#L286)
105​
106Same as the plain-object overload, but for options that close over reactive state (`ref`s read inside the
107function body). Wrap them in a getter so `queryClient` methods like `invalidateQueries`/`fetchQuery` always
108read the current values instead of the ones captured when the options were created.
109​
110### Type Parameters
111​
112#### TQueryFnData
113​
114`TQueryFnData` = `unknown`
115​
116#### TError
117​
118`TError` = `Error`
119​
120#### TData
121​
122`TData` = `TQueryFnData`
123​
124#### TQueryKey
125​
126`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
127​
128### Parameters
129​
130#### options
131​
132() => [`DefinedInitialQueryOptions`](../type-aliases/DefinedInitialQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
133​
134A function returning the [DefinedInitialQueryOptions](../type-aliases/DefinedInitialQueryOptions.md) to use, re-evaluated on demand.
135​
136### Returns
137​
138A function that returns the same options object, typed so that `queryKey` carries the inferred data
139type.
140​
141() => [`DefinedInitialQueryOptionsWithDataTag`](../type-aliases/DefinedInitialQueryOptionsWithDataTag.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
142​
143### See
144​
145[useQuery](useQuery.md) to run a query with these options.
146​
147### Example
148​
149```vue
150<script setup lang="ts">
151import { ref } from 'vue'
152import { queryOptions, useQuery } from '@tanstack/vue-query'
153​
154const postId = ref(1)
155const postOptions = queryOptions(() => ({
156 queryKey: ['post', postId.value],
157 queryFn: () => fetchPost(postId.value),
158 initialData: { id: postId.value, title: '' },
159}))
160​
161// Pass the getter itself, not `postOptions()`, so the query keeps reacting to `postId` changes.
162const { data } = useQuery(postOptions)
163</script>
164```
165​
166<a id="call-signature-3"></a>
167​
168## Call Signature
169​
170```ts
171function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialQueryOptionsWithDataTag<TQueryFnData, TError, TData, TQueryKey>;
172```
173​
174Defined in: [packages/vue-query/src/queryOptions.ts:329](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryOptions.ts#L329)
175​
176You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
177be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
178is the query key to generate options for.
179​
180### Type Parameters
181​
182#### TQueryFnData
183​
184`TQueryFnData` = `unknown`
185​
186#### TError
187​
188`TError` = `Error`
189​
190#### TData
191​
192`TData` = `TQueryFnData`
193​
194#### TQueryKey
195​
196`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
197​
198### Parameters
199​
200#### options
201​
202[`UndefinedInitialQueryOptions`](../type-aliases/UndefinedInitialQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
203​
204The [UndefinedInitialQueryOptions](../type-aliases/UndefinedInitialQueryOptions.md) to use β€” everything you can pass to `useQuery`.
205​
206### Returns
207​
208[`UndefinedInitialQueryOptionsWithDataTag`](../type-aliases/UndefinedInitialQueryOptionsWithDataTag.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
209​
210The same options object, typed so that `queryKey` carries the inferred data type.
211​
212### See
213​
214[useQuery](useQuery.md) to run a query with these options.
215​
216### Example
217​
218A parameterized factory, so the same options object can be reused per `id`:
219```vue
220<script setup lang="ts">
221import { queryOptions, useQuery } from '@tanstack/vue-query'
222​
223function postOptions(id: string) {
224 return queryOptions({
225 queryKey: ['post', id],
226 queryFn: () => fetchPost(id),
227 })
229​
230const { data, isPending, isError, error } = useQuery(postOptions('1'))
231</script>
232```
233​
234<a id="call-signature-4"></a>
235​
236## Call Signature
237​
238```ts
239function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: () => UndefinedInitialQueryOptions<TQueryFnData, TError, TData, TQueryKey>): () => UndefinedInitialQueryOptionsWithDataTag<TQueryFnData, TError, TData, TQueryKey>;
240```
241​
242Defined in: [packages/vue-query/src/queryOptions.ts:392](https://github.com/TanStack/query/blob/main/packages/vue-query/src/queryOptions.ts#L392)
243​
244Same as the plain-object overload, but for options that close over reactive state (`ref`s read inside the
245function body). Wrap them in a getter so the `queryKey` β€” and anything else derived from a `ref` β€” reacts
246to changes, and so `queryClient` methods like `invalidateQueries`/`fetchQuery` always read the current
247values instead of the ones captured when the options were created.
248​
249### Type Parameters
250​
251#### TQueryFnData
252​
253`TQueryFnData` = `unknown`
254​
255#### TError
256​
257`TError` = `Error`
258​
259#### TData
260​
261`TData` = `TQueryFnData`
262​
263#### TQueryKey
264​
265`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
266​
267### Parameters
268​
269#### options
270​
271() => [`UndefinedInitialQueryOptions`](../type-aliases/UndefinedInitialQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
272​
273A function returning the [UndefinedInitialQueryOptions](../type-aliases/UndefinedInitialQueryOptions.md) to use, re-evaluated on
274demand.
275​
276### Returns
277​
278A function that returns the same options object, typed so that `queryKey` carries the inferred
279data type.
280​
281() => [`UndefinedInitialQueryOptionsWithDataTag`](../type-aliases/UndefinedInitialQueryOptionsWithDataTag.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
282​
283### See
284​
285[useQuery](useQuery.md) to run a query with these options.
286​
287### Examples
288​
289```vue
290<script setup lang="ts">
291import { ref } from 'vue'
292import { queryOptions, useQuery, useQueryClient } from '@tanstack/vue-query'
293​
294const postId = ref(1)
295const postOptions = queryOptions(() => ({
296 queryKey: ['post', postId.value],
297 queryFn: () => fetchPost(postId.value),
298}))
299​
300// Pass the getter itself, not `postOptions()`, so the query keeps reacting to `postId` changes.
301const { data } = useQuery(postOptions)
302const queryClient = useQueryClient()
303// Here, call `postOptions()` so `invalidateQueries` reads the current `queryKey` right away.
304queryClient.invalidateQueries(postOptions())
305</script>
306```
307​
308A parameterized factory that disables the query, type safe, until `postId` is set. The whole-options getter
309re-evaluates `queryFn` on every change to `postId`. `queryFn` can also be a `computed`, but never a bare
310getter, since a function there is the query function itself:
311```vue
312<script setup lang="ts">
313import { queryOptions, skipToken, useQuery } from '@tanstack/vue-query'
314​
315function postOptions(postId: number | undefined) {
316 return queryOptions(() => ({
317 queryKey: ['post', postId],
318 queryFn: postId != null ? () => fetchPost(postId) : skipToken,
319 }))
321​
322const props = defineProps<{ postId: number | undefined }>()
323const { data, isLoading, isError, error } = useQuery(postOptions(props.postId))
324</script>
325```
326​
327<a id="parameters-summary"></a>
328​
329## Parameters
330​
331### options
332​
333() => [`UndefinedInitialQueryOptions`](../type-aliases/UndefinedInitialQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>
334​
335A function returning the [UndefinedInitialQueryOptions](../type-aliases/UndefinedInitialQueryOptions.md) to use, re-evaluated on
336demand.
337​
338<a id="returns-summary"></a>
339​
340## Returns
341​
342A function that returns the same options object, typed so that `queryKey` carries the inferred
343data type.
344​
345() => [`UndefinedInitialQueryOptionsWithDataTag`](../type-aliases/UndefinedInitialQueryOptionsWithDataTag.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>