#5493·trpc

feat: getQueryKey server side

Author: FranciscoCaetano88Created Feb 18, 2024Updated Jul 24, 2026
Labels✅ accepted-PRs-welcome

Describe the feature you'd like to request

I'd like to have a way of getting/building a query key from a procedure or router from the server. The implementation of getQueryKey shouldn't be bound to the client only.

Describe the solution you'd like to see

The following usecase describes a function where I get all the data from a dataset and populate the queryClient with each individual data as if they were fetched individually. Since I already have the dataset required I shouldn't be forced to refetch each individual data to populate the queryClient's cache:

`async function fetchSomething({ queryClient }: { queryClient: QueryClient }) { const helpers = await createSSRHelper(); const somethingData = await helpers.something.getAllOfSomething.fetch({ withMoreStuff: true });

  somethingData.forEach((something) => {
      const queryKey = getQueryKey(trpc.something.getSomethingById, {
       somethingId: something.id,
       withMoreStuff: true
    });

    queryClient.setQueryData(queryKey, something);
  });

  return somethingData;

}`

Describe alternate solutions

An alternate solution would be to fetch each data using the SSR Helper (causing unnecessary queries to a db or other services) or to input the querykey by hand in the setQueryData.

Additional information

No response

‍‍ Contributing

  • ‍♂️ Yes, I'd be down to file a PR implementing this feature!