--- id: queryOptions title: queryOptions redirect_from: - framework/solid/reference/queryOptions --- ## Overview ```ts function queryOptions(options: QueryOptions & object): ReturnType> & QueryKeyWithDataTag; function queryOptions(options: QueryOptions & object): ReturnType> & QueryKeyWithDataTag; ``` - [`ReturnType` → `ReturnType & 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. - [`ReturnType` → `ReturnType & 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. See also: [Parameters](#parameters-summary) · [Returns](#returns-summary) ## Call Signature ```ts function queryOptions(options: QueryOptions & object): ReturnType> & QueryKeyWithDataTag; ``` Defined in: [packages/solid-query/src/queryOptions.ts:86](https://github.com/TanStack/query/blob/main/packages/solid-query/src/queryOptions.ts#L86) 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. This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`). ### Type Parameters #### TQueryFnData `TQueryFnData` = `unknown` #### TError `TError` = `Error` #### TData `TData` = `TQueryFnData` #### TQueryKey `TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[] ### Parameters #### options [`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object` The [DefinedInitialDataOptions](../type-aliases/DefinedInitialDataOptions.md) to use — everything you can pass to `useQuery`, with `initialData` set. ### Returns `ReturnType`\<[`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\> The same options object, typed so that `queryKey` carries the inferred data type. ### See [useQuery](useQuery.md) to run a query with these options. ### Example ```tsx import { For } from 'solid-js' import { queryOptions, useQuery } from '@tanstack/solid-query' const postsOptions = queryOptions({ queryKey: ['posts'], queryFn: fetchPosts, initialData: [], }) function Posts() { // `postsQuery.data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails, // so the list stays visible alongside the error. const postsQuery = useQuery(() => postsOptions) return (
{postsQuery.isError ? Error: {postsQuery.error.message} : null}
    {(post) =>
  • {post.title}
  • }
) } ``` ## Call Signature ```ts function queryOptions(options: QueryOptions & object): ReturnType> & QueryKeyWithDataTag; ``` Defined in: [packages/solid-query/src/queryOptions.ts:134](https://github.com/TanStack/query/blob/main/packages/solid-query/src/queryOptions.ts#L134) 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. ### Type Parameters #### TQueryFnData `TQueryFnData` = `unknown` #### TError `TError` = `Error` #### TData `TData` = `TQueryFnData` #### TQueryKey `TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[] ### Parameters #### options [`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object` The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use — everything you can pass to `useQuery`. ### Returns `ReturnType`\<[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\> The same options object, typed so that `queryKey` carries the inferred data type. ### See [useQuery](useQuery.md) to run a query with these options. ### Example A parameterized factory, so the same options object can be reused per `id`: ```tsx import { Match, Switch } from 'solid-js' import { queryOptions, useQuery } from '@tanstack/solid-query' const postOptions = (id: string) => queryOptions({ queryKey: ['post', id], queryFn: () => fetchPost(id), }) function Post(props: { id: string }) { const postQuery = useQuery(() => postOptions(props.id)) return ( Loading... Error: {postQuery.error.message}

{postQuery.data.title}

) } ``` ## Parameters ### options [`QueryOptions`](../interfaces/QueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> & `object` The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use — everything you can pass to `useQuery`. #### `options` properties Built from [`QueryOptions`](../interfaces/QueryOptions.md#properties). See the type above for what it changes. ## Returns `ReturnType`\<[`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\>\> & [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md)\<`TQueryKey`, `TQueryFnData`, `TError`\> The same options object, typed so that `queryKey` carries the inferred data type. ### Result properties Built from [`QueryOptions`](../interfaces/QueryOptions.md#properties), [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md#properties). See the type above for what it changes.