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

queryOptions.md

207 lines7.3 KB

1---
2id: queryOptions
3title: queryOptions
4redirect_from:
5 - framework/solid/reference/queryOptions
6---
7​
8## Overview
9​
10```ts
11function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: QueryOptions<TQueryFnData, TError, TData, TQueryKey> & object): ReturnType<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
12function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: QueryOptions<TQueryFnData, TError, TData, TQueryKey> & object): ReturnType<UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
13```
14​
15- [`ReturnType<DefinedInitialDataOptions>` β†’ `ReturnType<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.
16- [`ReturnType<UndefinedInitialDataOptions>` β†’ `ReturnType<UndefinedInitialDataOptions> & 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.
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: QueryOptions<TQueryFnData, TError, TData, TQueryKey> & object): ReturnType<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
26```
27​
28Defined in: [packages/solid-query/src/queryOptions.ts:86](https://github.com/TanStack/query/blob/main/packages/solid-query/src/queryOptions.ts#L86)
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[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object`
60​
61The [DefinedInitialDataOptions](../type-aliases/DefinedInitialDataOptions.md) to use β€” everything you can pass to `useQuery`, with `initialData` set.
62​
63### Returns
64​
65`ReturnType`\<[`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​
73### Example
74​
75```tsx
76import { For } from 'solid-js'
77import { queryOptions, useQuery } from '@tanstack/solid-query'
78​
79const postsOptions = queryOptions({
80 queryKey: ['posts'],
81 queryFn: fetchPosts,
82 initialData: [],
83})
84​
85function Posts() {
86 // `postsQuery.data` is `Post[]`, never `undefined`, thanks to `initialData` β€” even if a refetch fails,
87 // so the list stays visible alongside the error.
88 const postsQuery = useQuery(() => postsOptions)
89​
90 return (
91 <div>
92 {postsQuery.isError ? <span>Error: {postsQuery.error.message}</span> : null}
93 <ul>
94 <For each={postsQuery.data}>{(post) => <li>{post.title}</li>}</For>
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: QueryOptions<TQueryFnData, TError, TData, TQueryKey> & object): ReturnType<UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
107```
108​
109Defined in: [packages/solid-query/src/queryOptions.ts:134](https://github.com/TanStack/query/blob/main/packages/solid-query/src/queryOptions.ts#L134)
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[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object`
138​
139The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β€” everything you can pass to `useQuery`.
140​
141### Returns
142​
143`ReturnType`\<[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.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​
151### Example
152​
153A parameterized factory, so the same options object can be reused per `id`:
154```tsx
155import { Match, Switch } from 'solid-js'
156import { queryOptions, useQuery } from '@tanstack/solid-query'
157​
158const postOptions = (id: string) =>
159 queryOptions({
160 queryKey: ['post', id],
161 queryFn: () => fetchPost(id),
162 })
163​
164function Post(props: { id: string }) {
165 const postQuery = useQuery(() => postOptions(props.id))
166​
167 return (
168 <Switch>
169 <Match when={postQuery.isPending}>Loading...</Match>
170 <Match when={postQuery.isError}>Error: {postQuery.error.message}</Match>
171 <Match when={postQuery.isSuccess}>
172 <h1>{postQuery.data.title}</h1>
173 </Match>
174 </Switch>
175 )
177```
178​
179<a id="parameters-summary"></a>
180​
181## Parameters
182​
183### options
184​
185[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object`
186​
187The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β€” everything you can pass to `useQuery`.
188​
189<a id="options-properties"></a>
190​
191#### `options` properties
192​
193Built from [`QueryOptions`](../interfaces/QueryOptions.md#properties). See the type above for what it changes.
194​
195<a id="returns-summary"></a>
196​
197## Returns
198​
199`ReturnType`\<[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
200​
201The same options object, typed so that `queryKey` carries the inferred data type.
202​
203<a id="result-properties"></a>
204​
205### Result properties
206​
207Built from [`QueryOptions`](../interfaces/QueryOptions.md#properties), [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md#properties). See the type above for what it changes.