본문 바로가기
버전: 9.x

라우터 정의

비공식 베타 번역

이 페이지는 PageTurner AI로 번역되었습니다(베타). 프로젝트 공식 승인을 받지 않았습니다. 오류를 발견하셨나요? 문제 신고 →

정보
  • 프로시저(procedure)는 REST 엔드포인트와 동등하게 볼 수 있습니다.
  • 쿼리와 뮤테이션은 의미상의 차이 외에는 내부적으로 차이가 없습니다.
  • 라우터 정의는 쿼리, 뮤테이션, 구독 모두 동일하게 적용됩니다. 단, 구독은 Subscription 인스턴스를 반환해야 합니다.

입력 검증

tRPC는 yup/superstruct/zod/myzod/커스텀 유효성 검사기 등을 즉시 사용할 수 있습니다. - 테스트 스위트 참조

입력이 없는 예제

tsx
import * as trpc from '@trpc/server';
// [...]
export const appRouter = trpc
.router<Context>()
// Create procedure at path 'hello'
.query('hello', {
resolve({ ctx }) {
return {
greeting: `hello world`,
};
},
});
tsx
import * as trpc from '@trpc/server';
// [...]
export const appRouter = trpc
.router<Context>()
// Create procedure at path 'hello'
.query('hello', {
resolve({ ctx }) {
return {
greeting: `hello world`,
};
},
});

Zod와 함께 사용하기

tsx
import * as trpc from '@trpc/server';
import { z } from 'zod';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: z
.object({
text: z.string().nullish(),
})
.nullish(),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
};
},
});
export type AppRouter = typeof appRouter;
tsx
import * as trpc from '@trpc/server';
import { z } from 'zod';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: z
.object({
text: z.string().nullish(),
})
.nullish(),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
};
},
});
export type AppRouter = typeof appRouter;

Yup와 함께 사용하기

tsx
import * as trpc from '@trpc/server';
import * as yup from 'yup';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: yup.object({
text: yup.string().required(),
}),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
};
},
});
export type AppRouter = typeof appRouter;
tsx
import * as trpc from '@trpc/server';
import * as yup from 'yup';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: yup.object({
text: yup.string().required(),
}),
resolve({ input }) {
return {
greeting: `hello ${input?.text ?? 'world'}`,
};
},
});
export type AppRouter = typeof appRouter;

Superstruct와 함께 사용하기

tsx
import * as trpc from '@trpc/server';
import * as t from 'superstruct';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: t.object({
/**
* Also supports inline doc strings when referencing the type.
*/
text: t.defaulted(t.string(), 'world'),
}),
resolve({ input }) {
return {
greeting: `hello ${input.text}`,
};
},
});
export type AppRouter = typeof appRouter;
tsx
import * as trpc from '@trpc/server';
import * as t from 'superstruct';
// [...]
export const appRouter = trpc.router<Context>().query('hello', {
input: t.object({
/**
* Also supports inline doc strings when referencing the type.
*/
text: t.defaulted(t.string(), 'world'),
}),
resolve({ input }) {
return {
greeting: `hello ${input.text}`,
};
},
});
export type AppRouter = typeof appRouter;

메서드 체이닝

여러 엔드포인트를 추가하려면 호출을 체이닝해야 합니다

tsx
import * as trpc from '@trpc/server';
// [...]
export const appRouter = trpc
.router<Context>()
.query('hello', {
resolve() {
return {
text: `hello world`,
};
},
})
.query('bye', {
resolve() {
return {
text: `goodbye`,
};
},
});
export type AppRouter = typeof appRouter;
tsx
import * as trpc from '@trpc/server';
// [...]
export const appRouter = trpc
.router<Context>()
.query('hello', {
resolve() {
return {
text: `hello world`,
};
},
})
.query('bye', {
resolve() {
return {
text: `goodbye`,
};
},
});
export type AppRouter = typeof appRouter;