Open History to see the last commit
1import type {
2 DefaultError,
3 QueryKey,
4 QueryKeyWithDataTag,
5} from '@tanstack/query-core'
6import type { QueryOptions } from './types'
7import type { Accessor } from 'solid-js'
8β
9/**
10 * The options accepted by the `queryOptions` overload selected when no `initialData` is set β `data` may be
11 * `undefined` while the query is `pending`. `queryOptions` itself accepts and returns a plain object (its
12 * parameter type is `ReturnType<UndefinedInitialDataOptions<...>>`, i.e. this `Accessor` called); Solid's
13 * reactivity applies where the result is consumed instead, e.g. `useQuery(() => options)`.
14 * @template TQueryFnData - The type your `queryFn` resolves to.
15 * @template TError - The type of errors your `queryFn` may throw.
16 * @template TData - The type `data` ends up as after `select` runs.
17 * @template TQueryKey - The type of your `queryKey`.
18 */
19export type UndefinedInitialDataOptions<
20 TQueryFnData = unknown,
21 TError = DefaultError,
22 TData = TQueryFnData,
23 TQueryKey extends QueryKey = QueryKey,
24> = Accessor<
25 QueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
26 initialData?: undefined
27 }
28>
29β
30/**
31 * The options accepted by the `queryOptions` overload selected when `initialData` is set β `data` is never
32 * `undefined` (unless a `select` changes `TData` to include `undefined`).
33 * @template TQueryFnData - The type your `queryFn` resolves to.
34 * @template TError - The type of errors your `queryFn` may throw.
35 * @template TData - The type `data` ends up as after `select` runs.
36 * @template TQueryKey - The type of your `queryKey`.
37 */
38export type DefinedInitialDataOptions<
39 TQueryFnData = unknown,
40 TError = DefaultError,
41 TData = TQueryFnData,
42 TQueryKey extends QueryKey = QueryKey,
43> = Accessor<
44 QueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
45 initialData: TQueryFnData | (() => TQueryFnData)
46 }
47>
48β
49/**
50 * You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
51 * be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
52 * is the query key to generate options for.
53 *
54 * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless
55 * a `select` changes `TData` to include `undefined`).
56 * @param options - The {@link DefinedInitialDataOptions} to use β everything you can pass to `useQuery`, with `initialData` set.
57 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
58 * @see {@link useQuery} to run a query with these options.
59 * @example
60 * ```tsx
61 * import { For } from 'solid-js'
62 * import { queryOptions, useQuery } from '@tanstack/solid-query'
63 *
64 * const postsOptions = queryOptions({
65 * queryKey: ['posts'],
66 * queryFn: fetchPosts,
67 * initialData: [],
68 * })
69 *
70 * function Posts() {
71 * // `postsQuery.data` is `Post[]`, never `undefined`, thanks to `initialData` β even if a refetch fails,
72 * // so the list stays visible alongside the error.
73 * const postsQuery = useQuery(() => postsOptions)
74 *
75 * return (
76 * <div>
77 * {postsQuery.isError ? <span>Error: {postsQuery.error.message}</span> : null}
78 * <ul>
79 * <For each={postsQuery.data}>{(post) => <li>{post.title}</li>}</For>
80 * </ul>
81 * </div>
82 * )
83 * }
84 * ```
85 */
86export function queryOptions<
87 TQueryFnData = unknown,
88 TError = DefaultError,
89 TData = TQueryFnData,
90 TQueryKey extends QueryKey = QueryKey,
91>(
92 options: ReturnType<
93 DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
94 >,
95): ReturnType<
96 DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
97> &
98 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
99β
100/**
101 * You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
102 * be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
103 * is the query key to generate options for.
104 * @param options - The {@link UndefinedInitialDataOptions} to use β everything you can pass to `useQuery`.
105 * @returns The same options object, typed so that `queryKey` carries the inferred data type.
106 * @see {@link useQuery} to run a query with these options.
107 * @example
108 * A parameterized factory, so the same options object can be reused per `id`:
109 * ```tsx
110 * import { Match, Switch } from 'solid-js'
111 * import { queryOptions, useQuery } from '@tanstack/solid-query'
112 *
113 * const postOptions = (id: string) =>
114 * queryOptions({
115 * queryKey: ['post', id],
116 * queryFn: () => fetchPost(id),
117 * })
118 *
119 * function Post(props: { id: string }) {
120 * const postQuery = useQuery(() => postOptions(props.id))
121 *
122 * return (
123 * <Switch>
124 * <Match when={postQuery.isPending}>Loading...</Match>
125 * <Match when={postQuery.isError}>Error: {postQuery.error.message}</Match>
126 * <Match when={postQuery.isSuccess}>
127 * <h1>{postQuery.data.title}</h1>
128 * </Match>
129 * </Switch>
130 * )
131 * }
132 * ```
133 */
134export function queryOptions<
135 TQueryFnData = unknown,
136 TError = DefaultError,
137 TData = TQueryFnData,
138 TQueryKey extends QueryKey = QueryKey,
139>(
140 options: ReturnType<
141 UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
142 >,
143): ReturnType<
144 UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>
145> &
146 QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
147β
148export function queryOptions(options: unknown) {
149 return options
150}