メインコンテンツへスキップ
バージョン: 9.x

ルーターの定義

非公式ベータ版翻訳

このページは PageTurner AI で翻訳されました(ベータ版)。プロジェクト公式の承認はありません。 エラーを見つけましたか? 問題を報告 →

情報
  • プロシージャは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;