Open History to see the last commit
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 )
98}
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 )
176}
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.