Define Router

info
  • A procedure can be viewed as the equivalent of a REST-endpoint.
  • There’s no internal difference between queries and mutations apart from semantics.
  • Defining router is the same for queries, mutations, and subscription with the exception that subscriptions need to return a Subscription-instance.

Input validation

tRPC works out-of-the-box with yup/superstruct/zod/myzod/custom validators/[..] - see test suite

Example without input

  1. import * as trpc from '@trpc/server';
  2. // [...]
  3. export const appRouter = trpc
  4. .router<Context>()
  5. // Create procedure at path 'hello'
  6. .query('hello', {
  7. resolve({ ctx }) {
  8. return {
  9. greeting: `hello world`,
  10. };
  11. },
  12. });

With Zod

  1. import * as trpc from '@trpc/server';
  2. import { z } from 'zod';
  3. // [...]
  4. export const appRouter = trpc.router<Context>().query('hello', {
  5. input: z
  6. .object({
  7. text: z.string().nullish(),
  8. })
  9. .nullish(),
  10. resolve({ input }) {
  11. return {
  12. greeting: `hello ${input?.text ?? 'world'}`,
  13. };
  14. },
  15. });
  16. export type AppRouter = typeof appRouter;

With Yup

  1. import * as trpc from '@trpc/server';
  2. import * as yup from 'yup';
  3. // [...]
  4. export const appRouter = trpc.router<Context>().query('hello', {
  5. input: yup.object({
  6. text: yup.string().required(),
  7. }),
  8. resolve({ input }) {
  9. return {
  10. greeting: `hello ${input?.text ?? 'world'}`,
  11. };
  12. },
  13. });
  14. export type AppRouter = typeof appRouter;

With Superstruct

  1. import * as trpc from '@trpc/server';
  2. import * as t from 'superstruct';
  3. // [...]
  4. export const appRouter = trpc.router<Context>().query('hello', {
  5. input: t.object({
  6. /**
  7. * Also supports inline doc strings when referencing the type.
  8. */
  9. text: t.defaulted(t.string(), 'world'),
  10. }),
  11. resolve({ input }) {
  12. return {
  13. greeting: `hello ${input.text}`,
  14. };
  15. },
  16. });
  17. export type AppRouter = typeof appRouter;

Method chaining

To add multiple endpoints, you must chain the calls

  1. import * as trpc from '@trpc/server';
  2. // [...]
  3. export const appRouter = trpc
  4. .router<Context>()
  5. .query('hello', {
  6. resolve() {
  7. return {
  8. text: `hello world`,
  9. };
  10. },
  11. })
  12. .query('bye', {
  13. resolve() {
  14. return {
  15. text: `goodbye`,
  16. };
  17. },
  18. });
  19. export type AppRouter = typeof appRouter;