0
Fork 0
mirror of https://github.com/TanStack/query.git synced 2026-11-16 22:50:20 +00:00
query/docs/framework/angular/reference/functions/queryOptions.md
Wonsuk Choi d169ee8778
docs(*): add property tables and overload overviews to reference pages and keep declared alias names (#11758)
* docs(*): add parameter and result property tables to reference function pages and an overview to overloaded ones

* docs(*): link to the base type's properties when a parameter or result type has no property table

* docs(framework/*/reference): regenerate reference docs

* docs(framework/*/reference): regenerate reference docs

* chore(scripts/generate-docs): restore the 'dirname' import that the merge dropped

* docs(framework/*/reference): regenerate reference docs

* docs(scripts/generate-docs): label overloads in the overview with their declared parameter and return types

* docs(framework/*/reference): regenerate reference docs

* docs(scripts/generate-docs): convert declared intersection types from the source so signatures keep their alias names

* docs(framework/*/reference): regenerate reference docs

* docs(scripts/generate-docs): reword a comment to avoid the unknown word 'inlines'

* docs(scripts/generate-docs): skip a parameter table on overloaded pages when its heading is missing
2026-10-06 02:38:27 +09:00

12 KiB

id title
queryOptions queryOptions

Overview

function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;
function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;

See also: Parameters · Returns

Call Signature

function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;

Defined in: packages/angular-query-experimental/src/query-options.ts:145

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 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

DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>

The DefinedInitialDataOptions to use — everything you can pass to injectQuery, with initialData set.

Returns

DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>

The same options object, typed so that queryKey carries the inferred data type.

See

Example

import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'

export const postsOptions = queryOptions({
  queryKey: ['posts'],
  queryFn: fetchPosts,
  initialData: [],
})

@Component({
  selector: 'posts',
  template: `
    <!-- `postsQuery.data()` is never `undefined`, thanks to `initialData` — even if a refetch
    fails, so the list stays visible alongside the error. -->
    @if (postsQuery.isError()) {
      <span>Error: {{ postsQuery.error()?.message }}</span>
    }
    <ul>
      @for (post of postsQuery.data(); track post.id) {
        <li>{{ post.title }}</li>
      }
    </ul>
  `,
})
export class Posts {
  readonly postsQuery = injectQuery(() => postsOptions)
}

Call Signature

function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;

Defined in: packages/angular-query-experimental/src/query-options.ts:192

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<TQueryFnData, TError, TData, TQueryKey>

The UnusedSkipTokenOptions to use — everything you can pass to injectQuery.

Returns

UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>

The same options object, typed so that queryKey carries the inferred data type.

See

Example

A parameterized factory, so the same options object can be reused per id:

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()) {
      <span>Error: {{ postQuery.error()?.message }}</span>
    } @else {
      <h1>{{ postQuery.data().title }}</h1>
    }
  `,
})
export class Post {
  readonly id = signal('1')
  readonly postQuery = injectQuery(() => postOptions(this.id()))
}

Call Signature

function queryOptions<TQueryFnData, TError, TData, TQueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>;

Defined in: packages/angular-query-experimental/src/query-options.ts:270

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<TQueryFnData, TError, TData, TQueryKey>

The UndefinedInitialDataOptions to use — everything you can pass to injectQuery.

Returns

UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<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

Examples

A parameterized factory, so the same options object can be reused per id:

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()) {
      <span>Error: {{ postQuery.error()?.message }}</span>
    } @else {
      <h1>{{ postQuery.data().title }}</h1>
    }
  `,
})
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:

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()) {
      <span>Error: {{ postQuery.error()?.message }}</span>
    } @else {
      <h1>{{ postQuery.data().title }}</h1>
    }
  `,
})
export class Post {
  readonly postId = signal<number | undefined>(undefined)
  readonly postQuery = injectQuery(() => postOptions(this.postId()))
}

Parameters

options

UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>

The UndefinedInitialDataOptions to use — everything you can pass to injectQuery.

options properties

Built from CreateQueryOptions. See the type above for what it changes.

Returns

UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> & QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>

The same options object, typed so that queryKey carries the inferred data type.

Result properties

Built from CreateQueryOptions, QueryKeyWithDataTag. See the type above for what it changes.