Open History to see the last commit
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 })
228}
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 }))
320}
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`\>