Open History to see the last commit
1---
2id: queryOptions
3title: queryOptions
4redirect_from:
5 - framework/solid/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: QueryOptions<TQueryFnData, TError, TData, TQueryKey> & object): ReturnType<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
14function queryOptions<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(options: QueryOptions<TQueryFnData, TError, TData, TQueryKey> & object): ReturnType<UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
15```
16β
17- [`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.
18- [`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.
19β
20See also: [Parameters](#parameters-summary) Β· [Returns](#returns-summary)
21β
22<a id="call-signature-1"></a>
23β
24## Call Signature
25β
26```ts
27function queryOptions<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(options: QueryOptions<TQueryFnData, TError, TData, TQueryKey> & object): ReturnType<DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
28```
29β
30Defined in: [packages/solid-query/src/queryOptions.ts:94](https://github.com/TanStack/query/blob/main/packages/solid-query/src/queryOptions.ts#L94)
31β
32You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
33be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
34is the query key to generate options for.
35β
36This overload is selected when `initialData` is known to be defined, so the resulting `data` is never
37`undefined` (unless a `select` changes `TData` to include `undefined`).
38β
39### Type Parameters
40β
41#### TQueryFnData
42β
43`TQueryFnData` = `unknown`
44β
45#### TError
46β
47`TError` = `Error`
48β
49#### TData
50β
51`TData` = `TQueryFnData`
52β
53#### TQueryKey
54β
55`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
56β
57### Parameters
58β
59#### options
60β
61[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object`
62β
63The [DefinedInitialDataOptions](../type-aliases/DefinedInitialDataOptions.md) to use β everything you can pass to `useQuery`, with `initialData` known to be defined.
64β
65### Returns
66β
67`ReturnType`\<[`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
68β
69The same options object, typed so that `queryKey` carries the inferred data type.
70β
71### See
72β
73[useQuery](useQuery.md) to run a query with these options.
74β
75### Example
76β
77```tsx
78import { For } from 'solid-js'
79import { queryOptions, useQuery } from '@tanstack/solid-query'
80β
81const postsOptions = queryOptions({
82 queryKey: ['posts'],
83 queryFn: fetchPosts,
84 initialData: [],
85})
86β
87function Posts() {
88 // `postsQuery.data` is `Post[]`, never `undefined`, thanks to `initialData` β even if a refetch fails,
89 // so the list stays visible alongside the error.
90 const postsQuery = useQuery(() => postsOptions)
91β
92 return (
93 <div>
94 {postsQuery.isError ? <span>Error: {postsQuery.error.message}</span> : null}
95 <ul>
96 <For each={postsQuery.data}>{(post) => <li>{post.title}</li>}</For>
97 </ul>
98 </div>
99 )
100}
101```
102β
103<a id="call-signature-2"></a>
104β
105## Call Signature
106β
107```ts
108function queryOptions<TQueryFnData = unknown, TError = Error, TData = TQueryFnData, TQueryKey extends readonly unknown[] = readonly unknown[]>(options: QueryOptions<TQueryFnData, TError, TData, TQueryKey> & object): ReturnType<UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
109```
110β
111Defined in: [packages/solid-query/src/queryOptions.ts:142](https://github.com/TanStack/query/blob/main/packages/solid-query/src/queryOptions.ts#L142)
112β
113You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
114be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
115is the query key to generate options for.
116β
117### Type Parameters
118β
119#### TQueryFnData
120β
121`TQueryFnData` = `unknown`
122β
123#### TError
124β
125`TError` = `Error`
126β
127#### TData
128β
129`TData` = `TQueryFnData`
130β
131#### TQueryKey
132β
133`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
134β
135### Parameters
136β
137#### options
138β
139[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object`
140β
141The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β everything you can pass to `useQuery`.
142β
143### Returns
144β
145`ReturnType`\<[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
146β
147The same options object, typed so that `queryKey` carries the inferred data type.
148β
149### See
150β
151[useQuery](useQuery.md) to run a query with these options.
152β
153### Example
154β
155A parameterized factory, so the same options object can be reused per `id`:
156```tsx
157import { Match, Switch } from 'solid-js'
158import { queryOptions, useQuery } from '@tanstack/solid-query'
159β
160const postOptions = (id: string) =>
161 queryOptions({
162 queryKey: ['post', id],
163 queryFn: () => fetchPost(id),
164 })
165β
166function Post(props: { id: string }) {
167 const postQuery = useQuery(() => postOptions(props.id))
168β
169 return (
170 <Switch>
171 <Match when={postQuery.isPending}>Loading...</Match>
172 <Match when={postQuery.isError}>Error: {postQuery.error.message}</Match>
173 <Match when={postQuery.isSuccess}>
174 <h1>{postQuery.data.title}</h1>
175 </Match>
176 </Switch>
177 )
178}
179```
180β
181<a id="parameters-summary"></a>
182β
183## Parameters
184β
185### options
186β
187[`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object`
188β
189The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use β everything you can pass to `useQuery`.
190β
191<a id="options-properties"></a>
192β
193#### `options` properties
194β
195Built from [`QueryOptions`](../interfaces/QueryOptions.md#properties). See the type above for what it changes.
196β
197<a id="returns-summary"></a>
198β
199## Returns
200β
201`ReturnType`\<[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\>
202β
203The same options object, typed so that `queryKey` carries the inferred data type.
204β
205<a id="result-properties"></a>
206β
207### Result properties
208β
209Built from [`QueryOptions`](../interfaces/QueryOptions.md#properties), [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md#properties). See the type above for what it changes.