question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useQueries hook for fetching unknown number of entities

See original GitHub issue

When fetching an unknown amount of entities, the useQueries hook comes handy, e.g.:

let filteredUsers: number[] = [/* Unknown amount of IDs */];
const results = useQueries(
  filteredUsers.map((id) => ["user.find", id] as const);
)

The typings of useQueries went through significant overhauls lately, which might make it less cumbersome to infer tRPC’s end-to-end types.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
KATTcommented, Jan 30, 2022

Yeah, it’s very hard to deal with the types on this one which is why I haven’t added support for it.

@TkDodo, the react-query maintainer, even brought up on Twitter today that he can barely follow how the types work himself 😅 https://twitter.com/TkDodo/status/1487804403356680201

To me, this feels like an edge-case and I’m unsure if I want to support this as part of the wrapper, to be honest.

However, you can still use useQueries and achieve this if you want:

import { useRouter } from 'next/router';
import { trpc } from 'utils/trpc';
import { useQueries } from 'react-query';

const PostViewPage = () => {
  const { client } = trpc.useContext();
  const postIds = ['1', '2'];
  const posts = useQueries(
    postIds.map((id) => ({
      queryKey: ['post.byId', { id }],
      queryFn: (args: any) => {
        const input = args.queryKey[1];
        return client.query('post.byId', input);
      },
    })),
  );
  // [..]
  return <></>
}
2reactions
tianhuilcommented, Sep 16, 2022

Thank you for providing this workaround @KATT. Let me try to convince you that it’s not an edge case.

To me, this feels like an edge-case and I’m unsure if I want to support this as part of the wrapper, to be honest.

The big reason for having a native trpc.useQueries would be smart cache validation (https://tanstack.com/query/v4/docs/guides/query-invalidation). I would like to query for 10 todos (by id) and then update a single one of them without having to refetch all 10. Ideally, the code would be something like …

const {data, isLoading} = trpc.useQueries(todos.map((id) => ['todo', { id }]))

const editedId = ...
const mutation = trpc.useMutation(['todo', { id: editedId }])
Read more comments on GitHub >

github_iconTop Results From Across the Web

useQueries | TanStack Query Docs
The useQueries hook can be used to fetch a variable number of queries: `tsx.
Read more >
Missing an equivalent of React Query's useQueries hook #1041
There does not seem to be an equivalent of the React Query's useQueries hook in SWR. Being able to run a variable number...
Read more >
Fetching data with queries | Full-Stack Quickstart
We'll use Apollo Client's useQuery React Hook to execute our new query within the Launches component. The hook's result object provides properties that...
Read more >
How to fetch n dependent data with react-query - Stack Overflow
How should I do this with react-query useQuery hook? Example 1: const { data} = useQuery("USERS", fetchUsers); data.map ...
Read more >
Common data fetching patterns for real apps with react-query
Fetching data: Fetch data and handle results: useQueries . Fetch a subset of data using query strings or query params: useQuery with parameters ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found