Split Link
A link that routes each operation down one of two link chains based on a condition
On this page
splitLink is a link that allows you to branch your link chain's execution depending on a given condition. Both the true and false branches are required. You can provide just one link, or multiple links per branch via an array.
It's important to note that when you provide links for splitLink to execute, splitLink will create an entirely new link chain based on the links you passed. Therefore, you need to use a terminating link if you only provide one link or add the terminating link at the end of the array if you provide multiple links to be executed on a branch. Here's a visual representation of how splitLink works:
Usage Example
Disable batching for certain requests
Let's say you're using httpBatchLink as the terminating link in your tRPC client config. This means request batching is enabled in every request. However, if you need to disable batching only for certain requests, you would need to change the terminating link in your tRPC client config dynamically between httpLink and httpBatchLink. This is a perfect opportunity for splitLink to be used:
1. Configure client / utils/trpc.ts
context gives the fields condition reads, with their defaults:
import {
createTRPCClient,
httpBatchLink,
httpLink,
routerType,
splitLink,
} from '@trpc/client';
import type { AppRouter } from './server';
const url = `http://localhost:3000`;
const client = createTRPCClient({
router: routerType<AppRouter>(),
links: [
splitLink({
// the `context` this condition reads, with its defaults
context: { skipBatch: false },
condition: (op) => op.context.skipBatch,
// when condition is true, use normal request
true: httpLink({
url,
}),
// when condition is false, use batching
false: httpBatchLink({
url,
}),
}),
],
});2. Perform request without batching
const postResult = proxy.posts.query(undefined, {
context: {
skipBatch: true,
},
});or:
import { useQuery } from '@tanstack/react-query';
import { useTRPC } from './trpc';
export function MyComponent() {
const trpc = useTRPC();
const postsQuery = useQuery(
trpc.posts.queryOptions(undefined, {
trpc: {
context: {
skipBatch: true,
},
},
}),
);
return (
<pre>{JSON.stringify(postsQuery.data ?? null, null, 4)}</pre>
)
}Typed context
With context: { skipBatch: false } and router: routerType<AppRouter>(), calls take skipBatch?: boolean, so passing anything else is a type error. A call that leaves it out, or passes undefined, gets false. A field the call passes replaces its default whole: nested objects aren't merged. Without context, calls take only what the branches' links declare.
splitLink Options
The splitLink function takes an options object with condition, true and false, and optionally context.
declare function splitLink<TRouter extends AnyRouter = AnyRouter>(opts: {
/**
* Values for the `context` fields `condition` reads, used when a call
* leaves one out. Their types are the fields calls can pass. Without it,
* calls take only what the branches' links declare.
*/
context?: Record<string, unknown>;
/**
* Picks the chain for an operation.
*/
condition: (op: Operation) => boolean;
/**
* The link to execute next if the test function returns `true`.
*/
true: TRPCLink<TRouter> | TRPCLink<TRouter>[];
/**
* The link to execute next if the test function returns `false`.
*/
false: TRPCLink<TRouter> | TRPCLink<TRouter>[];
}): TRPCLink<TRouter>;Reference
You can check out the source code for this link on GitHub.