Open History to see the last commit
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 )
124}
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 )
159}
160```