Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

createQueries.md

160 lines8.2 KB

1---
2id: createQueries
3title: createQueries
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8```ts
9const createQueries: <T, TCombinedResult>(queriesOptions: Accessor<{
10 combine?: (result: T extends [] ? [] : T extends [Head] ? [GetResults<Head>] : T extends [Head, ...Tail[]] ? [...Tail[]] extends [] ? [] : [...(...)[]] extends [...] ? [..., ...] : ... extends ... ? ... : ... : { [K in string | number | symbol]: GetResults<(...)[(...)]> }) => TCombinedResult;
11 queries: | readonly [T extends [] ? [] : T extends [Head] ? [GetOptions<Head>] : T extends [Head, ...Tail[]] ? [...Tail[]] extends [] ? [] : [...(...)[]] extends [...] ? [..., ...] : ... extends ... ? ... : ... : readonly unknown[] extends T ? T : T extends ...[] ? ...[] : ...[]]
12 | readonly [{ [K in string | number | symbol]: GetOptions<T[K]> }];
13}>, queryClient?: Accessor<QueryClient>) => TCombinedResult = useQueries;
14```
15​
16Defined in: [packages/solid-query/src/index.ts:86](https://github.com/TanStack/query/blob/main/packages/solid-query/src/index.ts#L86)
17​
18The `useQueries` primitive can be used to fetch a variable number of queries.
19​
20The `queries` key accepts an array with query option objects mostly identical to `useQuery` β€” see
21`placeholderData` below for the one difference. A custom `QueryClient` is supplied once, as `useQueries`'
22own top-level second argument, rather than per query.
23​
24Having the same query key more than once in the array of query objects may cause some data to be shared
25between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
26structure.
27​
28The `combine` option can be used to combine the results of the queries into a single value. The result will
29be structurally shared to be as referentially stable as possible.
30​
31`placeholderData` is supported here too, but unlike `useQuery`, it doesn't receive information from
32previously rendered queries, because the number of queries can differ between renders.
33​
34## Type Parameters
35​
36### T
37​
38`T` *extends* `any`[]
39​
40### TCombinedResult
41​
42`TCombinedResult` *extends*
43 \| \[\]
44 \| \[
45 \| [`QueryObserverRefetchErrorResult`](../interfaces/QueryObserverRefetchErrorResult.md)\<`unknown`, `Error`\>
46 \| [`QueryObserverSuccessResult`](../interfaces/QueryObserverSuccessResult.md)\<`unknown`, `Error`\>
47 \| [`QueryObserverLoadingErrorResult`](../interfaces/QueryObserverLoadingErrorResult.md)\<`unknown`, `Error`\>
48 \| [`QueryObserverLoadingResult`](../interfaces/QueryObserverLoadingResult.md)\<`unknown`, `Error`\>
49 \| [`QueryObserverPendingResult`](../interfaces/QueryObserverPendingResult.md)\<`unknown`, `Error`\>
50 \| [`QueryObserverPlaceholderResult`](../interfaces/QueryObserverPlaceholderResult.md)\<`unknown`, `Error`\>
51 \| [`QueryObserverRefetchErrorResult`](../interfaces/QueryObserverRefetchErrorResult.md)\<`unknown`, `unknown`\>
52 \| [`QueryObserverSuccessResult`](../interfaces/QueryObserverSuccessResult.md)\<`unknown`, `unknown`\>
53 \| [`QueryObserverLoadingErrorResult`](../interfaces/QueryObserverLoadingErrorResult.md)\<`unknown`, `unknown`\>
54 \| [`QueryObserverLoadingResult`](../interfaces/QueryObserverLoadingResult.md)\<`unknown`, `unknown`\>
55 \| [`QueryObserverPendingResult`](../interfaces/QueryObserverPendingResult.md)\<`unknown`, `unknown`\>
56 \| [`QueryObserverPlaceholderResult`](../interfaces/QueryObserverPlaceholderResult.md)\<`unknown`, `unknown`\>\]
57 \| (
58 \| [`QueryObserverRefetchErrorResult`](../interfaces/QueryObserverRefetchErrorResult.md)\<`unknown`, `Error`\>
59 \| [`QueryObserverSuccessResult`](../interfaces/QueryObserverSuccessResult.md)\<`unknown`, `Error`\>
60 \| [`QueryObserverLoadingErrorResult`](../interfaces/QueryObserverLoadingErrorResult.md)\<`unknown`, `Error`\>
61 \| [`QueryObserverLoadingResult`](../interfaces/QueryObserverLoadingResult.md)\<`unknown`, `Error`\>
62 \| [`QueryObserverPendingResult`](../interfaces/QueryObserverPendingResult.md)\<`unknown`, `Error`\>
63 \| [`QueryObserverPlaceholderResult`](../interfaces/QueryObserverPlaceholderResult.md)\<`unknown`, `Error`\>
64 \| [`QueryObserverRefetchErrorResult`](../interfaces/QueryObserverRefetchErrorResult.md)\<`unknown`, `unknown`\>
65 \| [`QueryObserverSuccessResult`](../interfaces/QueryObserverSuccessResult.md)\<`unknown`, `unknown`\>
66 \| [`QueryObserverLoadingErrorResult`](../interfaces/QueryObserverLoadingErrorResult.md)\<`unknown`, `unknown`\>
67 \| [`QueryObserverLoadingResult`](../interfaces/QueryObserverLoadingResult.md)\<`unknown`, `unknown`\>
68 \| [`QueryObserverPendingResult`](../interfaces/QueryObserverPendingResult.md)\<`unknown`, `unknown`\>
69 \| [`QueryObserverPlaceholderResult`](../interfaces/QueryObserverPlaceholderResult.md)\<`unknown`, `unknown`\>)[] = `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetResults`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tail[]`\] ? \[`...Tail[]`\] *extends* \[\] ? \[\] : \[`...Tail[]`\] *extends* \[`Head`\] ? \[`GetResults`\<`Head`\>, `GetResults`\<`Head`\>\] : \[`...Tail[]`\] *extends* \[`Head`, `...Tail[]`\] ? \[`...Tail[]`\] *extends* \[\] ? \[\] : \[`...Tail[]`\] *extends* \[`Head`\] ? \[`GetResults`\<...\>, `GetResults`\<...\>, `GetResults`\<...\>\] : \[`...(...)[]`\] *extends* \[..., `...(...)[]`\] ? ... *extends* ... ? ... : ... : \[`...(...)[]`\] : \[...\{ \[K in string \| number \| symbol\]: GetResults\<(...)\[(...)\]\> \}\[\]\] : \{ \[K in string \| number \| symbol\]: GetResults\<T\[K\]\> \}
70​
71## Parameters
72​
73### queriesOptions
74​
75`Accessor`\<\{
76 `combine?`: (`result`: `T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetResults`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tail[]`\] ? \[`...Tail[]`\] *extends* \[\] ? \[\] : \[`...(...)[]`\] *extends* \[...\] ? \[..., ...\] : ... *extends* ... ? ... : ... : \{ \[K in string \| number \| symbol\]: GetResults\<(...)\[(...)\]\> \}) => `TCombinedResult`;
77 `queries`: \| readonly \[`T` *extends* \[\] ? \[\] : `T` *extends* \[`Head`\] ? \[`GetOptions`\<`Head`\>\] : `T` *extends* \[`Head`, `...Tail[]`\] ? \[`...Tail[]`\] *extends* \[\] ? \[\] : \[`...(...)[]`\] *extends* \[...\] ? \[..., ...\] : ... *extends* ... ? ... : ... : readonly `unknown`[] *extends* `T` ? `T` : `T` *extends* ...[] ? ...[] : ...[]\]
78 \| readonly \[\{ \[K in string \| number \| symbol\]: GetOptions\<T\[K\]\> \}\];
79\}\>
80​
81An accessor returning the `queries` array to run, and an optional `combine`
82function.
83​
84### queryClient?
85​
86`Accessor`\<[`QueryClient`](../classes/QueryClient.md)\>
87​
88An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
89will be used.
90​
91## Returns
92​
93`TCombinedResult`
94​
95The combined result. Without `combine`, this is an array with all the query results, in the same
96order as the input. When `combine` is provided, this is the value returned by `combine` instead.
97​
98## Examples
99​
100```tsx
101import { For } from 'solid-js'
102import { useQueries } from '@tanstack/solid-query'
103​
104function Posts(props: { ids: Array<number> }) {
105 const postQueries = useQueries(() => ({
106 queries: props.ids.map((id) => ({
107 queryKey: ['post', id],
108 queryFn: () => fetchPost(id),
109 staleTime: Infinity,
110 })),
111 }))
112​
113 return (
114 <ul>
115 <For each={postQueries}>
116 {(postQuery) => {
117 if (postQuery.isPending) return <li>Loading...</li>
118 if (postQuery.isError) return <li>Error: {postQuery.error.message}</li>
119 return <li>{postQuery.data.title}</li>
120 }}
121 </For>
122 </ul>
123 )
125```
126​
127Combining results into a single value:
128```tsx
129import { For, Match, Switch } from 'solid-js'
130import { useQueries } from '@tanstack/solid-query'
131​
132function Posts(props: { ids: Array<number> }) {
133 const combinedPostsQuery = useQueries(() => ({
134 queries: props.ids.map((id) => ({
135 queryKey: ['post', id],
136 queryFn: () => fetchPost(id),
137 })),
138 combine: (postQueries) => {
139 return {
140 data: postQueries.map((postQuery) => postQuery.data),
141 isPending: postQueries.some((postQuery) => postQuery.isPending),
142 isError: postQueries.some((postQuery) => postQuery.isError),
143 }
144 },
145 }))
146​
147 return (
148 <Switch
149 fallback={
150 <ul>
151 <For each={combinedPostsQuery.data}>{(post) => <li>{post?.title}</li>}</For>
152 </ul>
153 }
154 >
155 <Match when={combinedPostsQuery.isPending}>Loading...</Match>
156 <Match when={combinedPostsQuery.isError}>Error loading posts</Match>
157 </Switch>
158 )
160```