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 `initialData` is omitted, may be
12 * `undefined`, or is a function that may return `undefined` β `data` may be `undefined` while the query is
13 * `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 `initialData` is known to be defined β
29 * `data` is never `undefined` (unless a `select` changes `TData` to include `undefined`).
30 */
31export type DefinedInitialDataOptions<
32 TQueryFnData = unknown,
33 TError = DefaultError,
34 TData = TQueryFnData,
35 TQueryKey extends QueryKey = QueryKey,
36> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
37 initialData:
38 | NonUndefinedGuard<TQueryFnData>
39 | (() => NonUndefinedGuard<TQueryFnData>)
40}
41β
42/**
43 * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
44 * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
45 * is required and is the query key to generate options for.
46 *
47 * This overload is selected when `initialData` is known to be defined, so the resulting `data` is never
48 * `undefined` (unless a `select` changes `TData` to include `undefined`).
49 * @param options - The {@link DefinedInitialDataOptions} to use β everything you can pass to `createQuery`,
50 * with `initialData` known to be defined.
51 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
52 * @see {@link createQuery} to run a query with these options.
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 */
79export function queryOptions<
80 TQueryFnData = unknown,
81 TError = DefaultError,
82 TData = TQueryFnData,
83 TQueryKey extends QueryKey = QueryKey,
84>(
85 options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
86): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
87 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
88β
89/**
90 * You can generally pass everything to `queryOptions` that you can also pass to `createQuery`. These options
91 * can be shared across `createQuery` calls and imperative APIs such as `queryClient.query`. `options.queryKey`
92 * is required and is the query key to generate options for.
93 * @param options - The {@link UndefinedInitialDataOptions} to use β everything you can pass to `createQuery`.
94 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
95 * @see {@link createQuery} to run a query with these options.
96 * @example
97 * A parameterized factory, so the same options object can be reused per `id`:
98 * ```svelte
99 * <script lang="ts">
100 * import { queryOptions, createQuery } from '@tanstack/svelte-query'
101 *
102 * let { id }: { id: string } = $props()
103 *
104 * const postOptions = (id: string) =>
105 * queryOptions({
106 * queryKey: ['post', id],
107 * queryFn: () => fetchPost(id),
108 * })
109 *
110 * const query = createQuery(() => postOptions(id))
111 * </script>
112 *
113 * {#if query.isPending}
114 * Loading...
115 * {:else if query.isError}
116 * <span>Error: {query.error.message}</span>
117 * {:else}
118 * <h1>{query.data.title}</h1>
119 * {/if}
120 * ```
121 */
122export function queryOptions<
123 TQueryFnData = unknown,
124 TError = DefaultError,
125 TData = TQueryFnData,
126 TQueryKey extends QueryKey = QueryKey,
127>(
128 options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
129): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
130 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
131β
132export function queryOptions(options: unknown) {
133 return options
134}