Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
5分でtRPCに入門する
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Haruki Murasaki
October 27, 2022
Programming
670
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
5分でtRPCに入門する
2022/10/27
https://rakus.connpass.com/event/258984/
Haruki Murasaki
October 27, 2022
More Decks by Haruki Murasaki
See All by Haruki Murasaki
開発速度を5倍早くするVSCodeの拡張機能を作った
purp1eeeee
2
490
Other Decks in Programming
See All in Programming
書籍「プロフェッショナルAI駆動開発」紹介スライド
juntaromatsumoto
0
980
AIの中の人になってみる
htkym
0
160
Can LLMs Replicate 4 Years of Compose Migration? Exploring the boundaries of automation with 279 XML files from a real product
makun
0
130
PyO3 で既存 Python 評価器を Rust core 化する ー wasm-bindgen でブラウザにも配るための設計
kdash
1
370
이 함수, 실패하면 어떻게 되나요? null부터 Rich Errors까지, Kotlin 에러 처리 15년
haeti2
0
130
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
350
Swift愛好会100回記念 第1回を振り返る
jollyjoester
0
120
片田舎のおっさん、 Swift Buildのダイアモンド問題解決の不具合修正PRを出すが、解決方法がキャッシュをしないようにすることであり、ビルド時間が伸びると言われてマージされないので高速化もする/swiftbuild
yimajo
0
360
初めての模倣学習とVLA
natsutan
0
470
in-process GraphQL のすすめ #ginzajs
izumin5210
4
1.5k
AI駆動開発にグラフDBを重ねてみた
satoshi256kbyte
2
730
AIに既存システムを理解させる技術 ~レガシーを見捨てないハーネスエンジニアリング入門~
ochtum
0
200
Featured
See All Featured
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
190
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
1
390
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
580
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
Docker and Python
trallard
47
4.2k
Into the Great Unknown - MozCon
thekraken
41
2.7k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
660
Music & Morning Musume
bryan
47
7.4k
Transcript
5分で tRPCに入門する 2022/10/27 @purp1eeeee
自己紹介 むらさき - 株式会社HRBrainのソフトウェアエンジニア - TypeScript/React/Go - TwitterとGitHub: purp1eeeee
tRPCとは
tRPCとは - TypeScriptに特化したHTTP通信ライブラリ - スキーマを書いたりコード生成をする必要がない - エンドポイントの実装からapi clientの型を生成する - サーバーとクライアント間で型を共有する
- monorepoと相性が良い - Next.jsのAPI Routesも
tRPC入門 登場人物 - next 13.0.0 - react 18.2.0 - react-dom
18.2.0 - @trpc/server - @trpc/client - @trpc/next - @trpc/react-query - @tanstack/react-query - zod
tRPC入門 エンドポイントの定義 /server/trpc.ts 最初にtRPCの初期化とユーティリティをexportするエントリーファイルを準備
tRPC入門 エンドポイントの定義 /pages/api/trpc/[trpc].ts request bodyなどはzodやyupなどで定義する それぞれのqueryやmutationに渡すcallback内で DBへのアクセスや加工を行う 25行目のAppRouterがポイントで、この型をclient にジェネリクスとして渡すことで型安全を保証する
tRPC入門 クライアントの定義 /utils/trpc.ts エンドポイントの指定やloggerの様な middleware、カスタムヘッダーや CORSの設定をする /pages/_app.tsx trpcが内部でreact-queryのhook を生成するためprovider対応の ためHOCを行う
tRPC入門 APIを叩いてみる /pages/index.tsx trpcが生成しているhookを呼ぶ 引数や返り値は全て型推論が効いて いる
tRPC入門 APIを叩いてみる
まとめ - 少ない記述で型安全なAPI間やりとりを実現できる - Next.jsと相性が良さそう - 認証認可やエラー処理も試したい 最終形はこちら:https://github.com/purp1eeeee/trpc-practice