GraphQL vs tRPC: スキーマファーストかTypeScriptか?
GraphQLは言語非依存でスキーマベースのクエリレイヤーであり、tRPCはスキーマやcodegenなしでクライアントとサーバー間にend-to-endのTypeScript型安全性をもたらします。
GraphQLは任意のクライアントや言語が利用できる型付きスキーマを定義し、柔軟なクエリと豊富なエコシステムを備え、パブリックAPIやポリグロットなクライアントに適しています。tRPCはTypeScript専用で、サーバーのprocedureが完全に型付けされたクライアント呼び出しとしてインポートされ、フルスタックTSアプリからスキーマとcodegenを排除しますが、TSの世界の中に限られます。GraphQLは柔軟性と相互運用性を、tRPCはTSのmonorepoでの摩擦のない型安全性を重視します。
| GraphQL | tRPC | |
|---|---|---|
| 言語 | 任意 | TypeScriptのみ |
| スキーマ | 明示的、型付き | コードから推論 |
| Codegen | しばしば必要 | なし |
| クライアント | ポリグロット、パブリック | TSフルスタック |
| 最適な用途 | パブリック/多様なクライアント | TSのmonorepo |
ユースケースと型安全性
GraphQLは形式的な契約を必要とするパブリックAPI、モバイル、ポリグロットなコンシューマーに適しています。tRPCはクライアントとサーバーが型を直接共有するフルスタックTypeScriptアプリ(多くはNext.js)に適しており、スキーマなしで即座にend-to-endの安全性をもたらしますが、TS以外や外部のコンシューマーを同じようにきれいに扱うことはできません。
CIにおいて
GraphQLのパイプラインはスキーマを検証しクライアントのcodegenを実行します。tRPCはTypeScriptコンパイラに依存するため、type-checkが主なgateになります。どちらもマネージドrunnerで動作し、より高速なrunnerはtype-checkとcodegenのステップを短縮します。
Decide with your own numbers, not a feature table
Feature comparisons age badly and rarely decide anything, because both tools in a mature category can do the job. What differs is how each behaves on your repository, and that takes one afternoon to measure.
# time a cold install with each candidate, cache cleared
hyperfine --prepare "rm -rf node_modules" --warmup 1 \
"<tool-a> install" "<tool-b> install"
# and the thing CI actually pays for: a cold run with no local cache
docker run --rm -v "$(pwd):/w" -w /w node:22 sh -c "<tool> install"What actually changes when you switch
- Lockfile format. A switch is a one-way door for anyone still on the old tool until everyone migrates, so plan it as a single coordinated change.
- Resolution strictness. Tools differ on whether an undeclared transitive import works, and the stricter one will surface latent bugs as new failures.
- CI cache configuration. The cache path and key differ per tool; carrying over the old ones silently disables caching.
- Everyone on the team and every runner must move together. Pin the version so they cannot drift.
結論
形式的で柔軟な契約を必要とするパブリックAPIやポリグロットなクライアントには、GraphQLです。摩擦のないend-to-endの型安全性を望む社内のフルスタックTypeScriptアプリには、tRPCです。tRPCはTSのmonorepo内では優れており、GraphQLは外部やTS以外のコンシューマーが重要な場合の選択肢です。