Links & Request Batching
Similar to urql's exchanges or Apollo's links. Links enables you to customize the flow of data between tRPC Client and the tRPC-server.
#
Request BatchingRequest batching is automatically enabled which batches your requests to the server, this can make the below code produce exactly one HTTP request and on the server exactly one database query:
// below will be done in the same request when batching is enabledconst somePosts = await Promise.all([ client.query('post.byId', 1), client.query('post.byId', 2), client.query('post.byId', 3),])
#
Customizing data flowThe below examples assuming you use Next.js, but the same as below can be added if you use the vanilla tRPC client
#
Disabling request batchingbatching
on your server:#
1. Disable In your [trpc].ts
:
export default trpcNext.createNextApiHandler({ // [...] // ๐ disable batching batching: { enabled: false, },});
#
2. Use batch-free link in your tRPC Clientimport type { AppRouter } from 'pages/api/trpc/[trpc]';import { withTRPC } from '@trpc/next';import { AppType } from 'next/dist/shared/lib/utils';// ๐ import the httpBatchLinkimport { httpLink } from '@trpc/client/links/httpLink';
const MyApp: AppType = ({ Component, pageProps }) => { return <Component {...pageProps} />;};
export default withTRPC<AppRouter>({ config() { return { links: [ httpLink({ url: '/api/trpc', }), ], }; }, // ssr: false,})(MyApp);
splitLink
to control request flow#
Using a #
Disable batching for certain requests_app.tsx
#
1. Configure client / import { withTRPC } from '@trpc/next';import { httpBatchLink } from '@trpc/client/links/httpBatchLink';import { httpLink } from '@trpc/client/links/httpLink';import { splitLink } from '@trpc/client/links/splitLink';
// [..]export default withTRPC<AppRouter>({ config() { const url = `http://localhost:3000`;
return { links: [ splitLink({ condition(op) { // check for context property `skipBatch` return op.context.skipBatch === true; }, // when condition is true, use normal request true: httpLink({ url, }), // when condition is false, use batching false: httpBatchLink({ url, }), }), ], }; },})(MyApp);
#
2. Perform request without batchingconst postsQuery = trpc.useQuery(['posts'], { context: { skipBatch: true, },});
// or
const postResult = client.query('posts', null, { context: { skipBatch: true, },})
#
Creating a custom linkimport type { AppRouter } from 'pages/api/trpc/[trpc]';import { TRPCLink } from '@trpc/client';
const customLink: TRPCLink<AppRouter> = (runtime) => { // here we just got initialized in the app - this happens once per app // useful for storing cache for instance return ({ prev, next, op }) => { // this is when passing the result to the next link next(op, (result) => { // this is when we've gotten result from the server if (result instanceof Error) { // maybe send to bugsnag? } prev(result); }); };};
export default withTRPC<AppRouter>({ config() { return { links: [ customLink, // [..] // โ Make sure to end with a `httpBatchLink` or `httpLink` ], }; }, // ssr: false})(MyApp);