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