Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

queryOptions.ts

133 lines4.5 KB

1import type {
2 DefaultError,
3 InitialDataFunction,
4 NonUndefinedGuard,
5 QueryKey,
6 QueryKeyWithDataTag,
7} from '@tanstack/query-core'
8import type { CreateQueryOptions } from './types.js'
9​
10/**
11 * The options accepted by the `queryOptions` overload selected when `initialData` is omitted or may be
12 * `undefined` β€” `data` may be `undefined` while the query is `pending`.
13 */
14export type UndefinedInitialDataOptions<
15 TQueryFnData = unknown,
16 TError = DefaultError,
17 TData = TQueryFnData,
18 TQueryKey extends QueryKey = QueryKey,
19> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
20 initialData?:
21 | undefined
22 | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
23 | NonUndefinedGuard<TQueryFnData>
25​
26/**
27 * The options accepted by the `queryOptions` overload selected when `initialData` is set β€” `data` is never
28 * `undefined` (unless a `select` changes `TData` to include `undefined`).
29 */
30export type DefinedInitialDataOptions<
31 TQueryFnData = unknown,
32 TError = DefaultError,
33 TData = TQueryFnData,
34 TQueryKey extends QueryKey = QueryKey,
35> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
36 initialData:
37 | NonUndefinedGuard<TQueryFnData>
38 | (() => NonUndefinedGuard<TQueryFnData>)
40​
41/**
42 * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
43 * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
44 * is required and is the query key to generate options for.
45 *
46 * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
47 * a `select` changes `TData` to include `undefined`).
48 * @param options - The {@link DefinedInitialDataOptions} to use β€” everything you can pass to `createQuery`,
49 * with `initialData` set.
50 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
51 * @see {@link createQuery} to run a query with these options.
52 * @example
53 * ```svelte
54 * <script lang="ts">
55 * import { queryOptions, createQuery } from '@tanstack/svelte-query'
56 *
57 * const postsOptions = queryOptions({
58 * queryKey: ['posts'],
59 * queryFn: fetchPosts,
60 * initialData: [],
61 * })
62 *
63 * // `data` is `Post[]`, never `undefined`, thanks to `initialData` β€” even if a refetch fails,
64 * // so the list stays visible alongside the error.
65 * const query = createQuery(() => postsOptions)
66 * </script>
67 *
68 * {#if query.isError}
69 * <span>Error: {query.error.message}</span>
70 * {/if}
71 * <ul>
72 * {#each query.data as post (post.id)}
73 * <li>{post.title}</li>
74 * {/each}
75 * </ul>
76 * ```
77 */
78export function queryOptions<
79 TQueryFnData = unknown,
80 TError = DefaultError,
81 TData = TQueryFnData,
82 TQueryKey extends QueryKey = QueryKey,
83>(
84 options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
85): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
86 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
87​
88/**
89 * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
90 * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
91 * is required and is the query key to generate options for.
92 * @param options - The {@link UndefinedInitialDataOptions} to use β€” everything you can pass to `createQuery`.
93 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
94 * @see {@link createQuery} to run a query with these options.
95 * @example
96 * A parameterized factory, so the same options object can be reused per `id`:
97 * ```svelte
98 * <script lang="ts">
99 * import { queryOptions, createQuery } from '@tanstack/svelte-query'
100 *
101 * let { id }: { id: string } = $props()
102 *
103 * const postOptions = (id: string) =>
104 * queryOptions({
105 * queryKey: ['post', id],
106 * queryFn: () => fetchPost(id),
107 * })
108 *
109 * const query = createQuery(() => postOptions(id))
110 * </script>
111 *
112 * {#if query.isPending}
113 * Loading...
114 * {:else if query.isError}
115 * <span>Error: {query.error.message}</span>
116 * {:else}
117 * <h1>{query.data.title}</h1>
118 * {/if}
119 * ```
120 */
121export function queryOptions<
122 TQueryFnData = unknown,
123 TError = DefaultError,
124 TData = TQueryFnData,
125 TQueryKey extends QueryKey = QueryKey,
126>(
127 options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
128): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
129 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
130​
131export function queryOptions(options: unknown) {
132 return options