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