--- id: queryOptions title: queryOptions --- ## Overview ```ts function queryOptions(options: DefinedInitialDataOptions): DefinedInitialDataOptions & QueryKeyWithDataTag; function queryOptions(options: UnusedSkipTokenOptions): UnusedSkipTokenOptions & QueryKeyWithDataTag; function queryOptions(options: UndefinedInitialDataOptions): UndefinedInitialDataOptions & QueryKeyWithDataTag; ``` - [`DefinedInitialDataOptions` → `DefinedInitialDataOptions & QueryKeyWithDataTag`](#call-signature-1): You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is the query key to generate options for. - [`UnusedSkipTokenOptions` → `UnusedSkipTokenOptions & QueryKeyWithDataTag`](#call-signature-2): You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is the query key to generate options for. - [`UndefinedInitialDataOptions` → `UndefinedInitialDataOptions & QueryKeyWithDataTag`](#call-signature-3): You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `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: DefinedInitialDataOptions): DefinedInitialDataOptions & QueryKeyWithDataTag; ``` Defined in: [packages/angular-query-experimental/src/query-options.ts:149](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/query-options.ts#L149) You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is required and is the query key to generate options for. This overload is selected when `initialData` is known to be defined, 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 [`DefinedInitialDataOptions`](../type-aliases/DefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> The [DefinedInitialDataOptions](../type-aliases/DefinedInitialDataOptions.md) to use — everything you can pass to `injectQuery`, with `initialData` known to be defined. ### Returns [`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 - [injectQuery](injectQuery.md) to run a query with these options. - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern. ### Example ```angular-ts import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental' export const postsOptions = queryOptions({ queryKey: ['posts'], queryFn: fetchPosts, initialData: [], }) @Component({ selector: 'posts', template: ` @if (postsQuery.isError()) { Error: {{ postsQuery.error()?.message }} }
    @for (post of postsQuery.data(); track post.id) {
  • {{ post.title }}
  • }
`, }) export class Posts { readonly postsQuery = injectQuery(() => postsOptions) } ``` ## Call Signature ```ts function queryOptions(options: UnusedSkipTokenOptions): UnusedSkipTokenOptions & QueryKeyWithDataTag; ``` Defined in: [packages/angular-query-experimental/src/query-options.ts:196](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/query-options.ts#L196) You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `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 [`UnusedSkipTokenOptions`](../type-aliases/UnusedSkipTokenOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> The [UnusedSkipTokenOptions](../type-aliases/UnusedSkipTokenOptions.md) to use — everything you can pass to `injectQuery`. ### Returns [`UnusedSkipTokenOptions`](../type-aliases/UnusedSkipTokenOptions.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 - [injectQuery](injectQuery.md) to run a query with these options. - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern. ### Example A parameterized factory, so the same options object can be reused per `id`: ```angular-ts import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental' export const postOptions = (id: string) => queryOptions({ queryKey: ['post', id], queryFn: () => fetchPost(id), }) @Component({ selector: 'post', template: ` @if (postQuery.isPending()) { Loading... } @else if (postQuery.isError()) { Error: {{ postQuery.error()?.message }} } @else {

{{ postQuery.data().title }}

} `, }) export class Post { readonly id = signal('1') readonly postQuery = injectQuery(() => postOptions(this.id())) } ``` ## Call Signature ```ts function queryOptions(options: UndefinedInitialDataOptions): UndefinedInitialDataOptions & QueryKeyWithDataTag; ``` Defined in: [packages/angular-query-experimental/src/query-options.ts:274](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/query-options.ts#L274) You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `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 [`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use — everything you can pass to `injectQuery`. ### Returns [`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. ### Remarks This is the only overload that accepts `queryFn: skipToken`, shown below. ### See - [injectQuery](injectQuery.md) to run a query with these options. - [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern. ### Examples A parameterized factory, so the same options object can be reused per `id`: ```angular-ts import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental' export const postOptions = (id: string) => queryOptions({ queryKey: ['post', id], queryFn: () => fetchPost(id), }) @Component({ selector: 'post', template: ` @if (postQuery.isPending()) { Loading... } @else if (postQuery.isError()) { Error: {{ postQuery.error()?.message }} } @else {

{{ postQuery.data().title }}

} `, }) export class Post { readonly id = signal('1') readonly postQuery = injectQuery(() => postOptions(this.id())) } ``` A factory that disables the query, type safe, until `postId` is set: ```angular-ts import { queryOptions, skipToken, injectQuery } from '@tanstack/angular-query-experimental' export const postOptions = (postId: number | undefined) => queryOptions({ queryKey: ['post', postId], queryFn: postId != null ? () => fetchPost(postId) : skipToken, }) @Component({ selector: 'post', template: ` @if (postId() == null) { Select a post } @else if (postQuery.isPending()) { Loading... } @else if (postQuery.isError()) { Error: {{ postQuery.error()?.message }} } @else {

{{ postQuery.data().title }}

} `, }) export class Post { readonly postId = signal(undefined) readonly postQuery = injectQuery(() => postOptions(this.postId())) } ``` ## Parameters ### options [`UndefinedInitialDataOptions`](../type-aliases/UndefinedInitialDataOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`\> The [UndefinedInitialDataOptions](../type-aliases/UndefinedInitialDataOptions.md) to use — everything you can pass to `injectQuery`. #### `options` properties Built from [`CreateQueryOptions`](../interfaces/CreateQueryOptions.md#properties). See the type above for what it changes. ## Returns [`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 [`CreateQueryOptions`](../interfaces/CreateQueryOptions.md#properties), [`QueryKeyWithDataTag`](../type-aliases/QueryKeyWithDataTag.md#properties). See the type above for what it changes.