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