Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
2 replies
Paul

trpc - Calling a route inside itself

Based on the documentation here, you can call another route on the server side using:
const caller = route.createCaller({})

However, if the route is within itself, is it possible to do so using the this keyword? If not, how can one call a route sibling?

import { z } from "zod";

import { router, publicProcedure } from "../trpc";

export const appRouter = router({
  example: exampleRouter,
});

export const exampleRouter = router({
  hello: publicProcedure.query(() => "Hello"),
  world: publicProcedure.query(() => "World"),
  greet: publicProcedure.query(function () {
    const caller = this.createCaller({});
    // const caller = appRouter.createCaller({}) ????
    return caller.hello() + caller.world();
  }),
});
Was this page helpful?