Open History to see the last commit
1import type {
2 DefaultError,
3 InitialDataFunction,
4 NonUndefinedGuard,
5 OmitKeyof,
6 QueryKey,
7 QueryKeyWithDataTag,
8 SkipToken,
9} from '@tanstack/query-core'
10import type { CreateQueryOptions } from './types'
11β
12/**
13 * The options accepted by the `queryOptions` overload selected when no `initialData` is set β `data` may be
14 * `undefined` while the query is `pending`.
15 * @template TQueryFnData - The type your `queryFn` resolves to.
16 * @template TError - The type of errors your `queryFn` may throw.
17 * @template TData - The type `data` ends up as after `select` runs.
18 * @template TQueryKey - The type of your `queryKey`.
19 */
20export type UndefinedInitialDataOptions<
21 TQueryFnData = unknown,
22 TError = DefaultError,
23 TData = TQueryFnData,
24 TQueryKey extends QueryKey = QueryKey,
25> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
26 /**
27 * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been
28 * created or cached yet). If set to a function, the function will be called **once** during the shared/root
29 * query initialization, and be expected to synchronously return the initial data. Initial data is
30 * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the
31 * cache.
32 */
33 initialData?:
34 | undefined
35 | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
36 | NonUndefinedGuard<TQueryFnData>
37}
38β
39/**
40 * The options accepted by the `queryOptions` overload selected when no `initialData` is set and `queryFn` is
41 * not `skipToken` β same as {@link UndefinedInitialDataOptions}, but `queryFn` may not be `skipToken`.
42 * @template TQueryFnData - The type your `queryFn` resolves to.
43 * @template TError - The type of errors your `queryFn` may throw.
44 * @template TData - The type `data` ends up as after `select` runs.
45 * @template TQueryKey - The type of your `queryKey`.
46 */
47export type UnusedSkipTokenOptions<
48 TQueryFnData = unknown,
49 TError = DefaultError,
50 TData = TQueryFnData,
51 TQueryKey extends QueryKey = QueryKey,
52> = OmitKeyof<
53 CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
54 'queryFn'
55> & {
56 /**
57 * `skipToken` is not allowed as a value here β this overload is selected when no `initialData` is set. If
58 * you don't intend to run the query yet, set `enabled: false` β omitting `queryFn` alone still triggers a
59 * fetch that fails with "Missing queryFn" unless `enabled` is `false` or a default query function has been
60 * defined. A default query function only supplies `queryFn`; it doesn't defer the fetch on its own.
61 */
62 queryFn?: Exclude<
63 CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>['queryFn'],
64 SkipToken | undefined
65 >
66}
67β
68/**
69 * The options accepted by the `queryOptions` overload selected when `initialData` is set β `data` is never
70 * `undefined` (unless a `select` changes `TData` to include `undefined`).
71 * @template TQueryFnData - The type your `queryFn` resolves to.
72 * @template TError - The type of errors your `queryFn` may throw.
73 * @template TData - The type `data` ends up as after `select` runs.
74 * @template TQueryKey - The type of your `queryKey`.
75 */
76export type DefinedInitialDataOptions<
77 TQueryFnData = unknown,
78 TError = DefaultError,
79 TData = TQueryFnData,
80 TQueryKey extends QueryKey = QueryKey,
81> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
82 initialData:
83 | NonUndefinedGuard<TQueryFnData>
84 | (() => NonUndefinedGuard<TQueryFnData>)
85}
86β
87/**
88 * You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options
89 * can be shared across functions and imperative APIs such as `queryClient.query`. `options.queryKey` is
90 * required and is the query key to generate options for.
91 *
92 * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
93 * a `select` changes `TData` to include `undefined`).
94 * @param options - The {@link DefinedInitialDataOptions} to use β everything you can pass to `injectQuery`,
95 * with `initialData` set.
96 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
97 * @see {@link injectQuery} to run a query with these options.
98 * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
99 * @example
100 * ```angular-ts
101 * import { queryOptions, injectQuery } from '@tanstack/angular-query'
102 *
103 * export const postsOptions = queryOptions({
104 * queryKey: ['posts'],
105 * queryFn: fetchPosts,
106 * initialData: [],
107 * })
108 *
109 * @Component({
110 * selector: 'posts',
111 * template: `
112 * <!-- `postsQuery.data()` is never `undefined`, thanks to `initialData` β even if a refetch
113 * fails, so the list stays visible alongside the error. -->
114 * @if (postsQuery.isError()) {
115 * <span>Error: {{ postsQuery.error()?.message }}</span>
116 * }
117 * <ul>
118 * @for (post of postsQuery.data(); track post.id) {
119 * <li>{{ post.title }}</li>
120 * }
121 * </ul>
122 * `,
123 * })
124 * export class Posts {
125 * readonly postsQuery = injectQuery(() => postsOptions)
126 * }
127 * ```
128 */
129export function queryOptions<
130 TQueryFnData = unknown,
131 TError = DefaultError,
132 TData = TQueryFnData,
133 TQueryKey extends QueryKey = QueryKey,
134>(
135 options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
136): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
137 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
138β
139/**
140 * You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options
141 * can be shared across functions and imperative APIs such as `queryClient.query`. `options.queryKey` is
142 * required and is the query key to generate options for.
143 * @param options - The {@link UnusedSkipTokenOptions} to use β everything you can pass to `injectQuery`.
144 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
145 * @see {@link injectQuery} to run a query with these options.
146 * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
147 * @example
148 * A parameterized factory, so the same options object can be reused per `id`:
149 * ```angular-ts
150 * import { queryOptions, injectQuery } from '@tanstack/angular-query'
151 *
152 * export const postOptions = (id: string) =>
153 * queryOptions({
154 * queryKey: ['post', id],
155 * queryFn: () => fetchPost(id),
156 * })
157 *
158 * @Component({
159 * selector: 'post',
160 * template: `
161 * @if (postQuery.isPending()) {
162 * Loading...
163 * } @else if (postQuery.isError()) {
164 * <span>Error: {{ postQuery.error()?.message }}</span>
165 * } @else {
166 * <h1>{{ postQuery.data().title }}</h1>
167 * }
168 * `,
169 * })
170 * export class Post {
171 * readonly id = signal('1')
172 * readonly postQuery = injectQuery(() => postOptions(this.id()))
173 * }
174 * ```
175 */
176export function queryOptions<
177 TQueryFnData = unknown,
178 TError = DefaultError,
179 TData = TQueryFnData,
180 TQueryKey extends QueryKey = QueryKey,
181>(
182 options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>,
183): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> &
184 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
185β
186/**
187 * You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options
188 * can be shared across functions and imperative APIs such as `queryClient.query`. `options.queryKey` is
189 * required and is the query key to generate options for.
190 * @param options - The {@link UndefinedInitialDataOptions} to use β everything you can pass to `injectQuery`.
191 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
192 * @remarks This is the only overload that accepts `queryFn: skipToken`, shown below.
193 * @see {@link injectQuery} to run a query with these options.
194 * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
195 * @example
196 * A parameterized factory, so the same options object can be reused per `id`:
197 * ```angular-ts
198 * import { queryOptions, injectQuery } from '@tanstack/angular-query'
199 *
200 * export const postOptions = (id: string) =>
201 * queryOptions({
202 * queryKey: ['post', id],
203 * queryFn: () => fetchPost(id),
204 * })
205 *
206 * @Component({
207 * selector: 'post',
208 * template: `
209 * @if (postQuery.isPending()) {
210 * Loading...
211 * } @else if (postQuery.isError()) {
212 * <span>Error: {{ postQuery.error()?.message }}</span>
213 * } @else {
214 * <h1>{{ postQuery.data().title }}</h1>
215 * }
216 * `,
217 * })
218 * export class Post {
219 * readonly id = signal('1')
220 * readonly postQuery = injectQuery(() => postOptions(this.id()))
221 * }
222 * ```
223 * @example
224 * A factory that disables the query, type safe, until `postId` is set:
225 * ```angular-ts
226 * import { queryOptions, skipToken, injectQuery } from '@tanstack/angular-query'
227 *
228 * export const postOptions = (postId: number | undefined) =>
229 * queryOptions({
230 * queryKey: ['post', postId],
231 * queryFn: postId != null ? () => fetchPost(postId) : skipToken,
232 * })
233 *
234 * @Component({
235 * selector: 'post',
236 * template: `
237 * @if (postId() == null) {
238 * Select a post
239 * } @else if (postQuery.isPending()) {
240 * Loading...
241 * } @else if (postQuery.isError()) {
242 * <span>Error: {{ postQuery.error()?.message }}</span>
243 * } @else {
244 * <h1>{{ postQuery.data().title }}</h1>
245 * }
246 * `,
247 * })
248 * export class Post {
249 * readonly postId = signal<number | undefined>(undefined)
250 * readonly postQuery = injectQuery(() => postOptions(this.postId()))
251 * }
252 * ```
253 */
254export function queryOptions<
255 TQueryFnData = unknown,
256 TError = DefaultError,
257 TData = TQueryFnData,
258 TQueryKey extends QueryKey = QueryKey,
259>(
260 options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,
261): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
262 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
263β
264export function queryOptions(options: unknown) {
265 return options
266}