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

queryOptions.md

347 lines13.0 KB

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 })
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 }))
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`\>