Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
tRPCの紹介
Search
Yohei Iino
June 06, 2023
Technology
300
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
tRPCの紹介
tRPCの紹介
Yohei Iino
June 06, 2023
More Decks by Yohei Iino
See All by Yohei Iino
1年半放置したExpo製アプリを最新化してみた
wheatandcat
0
120
作成中のFlutterアプリの中間発表
wheatandcat
0
98
最近読んだ技術書を簡単紹介
wheatandcat
0
120
ユニバーサルリンク/アプリリンクを使ってQRコードでゲストログインできるようにする
wheatandcat
0
400
Firebase App Checkを実装したので紹介
wheatandcat
0
370
PlanetScaleの無料プランがなくなるので、NeonとTiDBを試してみた
wheatandcat
0
440
Flutter HooksとRiverpodの解説
wheatandcat
0
610
T3 Stack(応用編: Next Auth & SSRの実装紹介)
wheatandcat
1
420
App Routerの紹介
wheatandcat
0
170
Other Decks in Technology
See All in Technology
いちAWSエンジニアのAI活用を振り返る #devio2026 / devio osaka 2026 kawahara
masahirokawahara
2
390
サーバーフルコンピューティング?AWS Lambda
iwatatomoya
1
260
BedrockとLambdaで作る リアルタイム進行型推理ゲーム
kawametho
0
190
AIに攻撃される前に、AIに攻撃させる
tsuchikazu
0
150
メルペイ 会計システム概要と歴史
mewuto
0
180
事業活動を AI Ready にする攻めと守りのデータエンジニアリング / data-engineering-for-ai-ready-business
pei0804
3
540
データ品質を壊しながらSnowflakeのAIに分析させてみた
kawanago
0
520
Deep Data Security 機能解説
oracle4engineer
PRO
2
710
Codex概要
ymiya55
0
430
オブザーバビリティを高める AI エージェント体験を考える / Designing AI Agent Experiences That Enhance Observability
aoto
PRO
2
310
実体験から学ぶ分析エージェントの開発と運⽤
recruitengineers
PRO
1
280
MCPゲートウェイを作って運用してわかったこと — Agent時代の権限管理の現在地
mtpooh
10
3.1k
Featured
See All Featured
BBQ
matthewcrist
89
10k
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.4k
What's in a price? How to price your products and services
michaelherold
247
13k
GitHub's CSS Performance
jonrohan
1033
470k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Writing Fast Ruby
sferik
630
63k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Building Adaptive Systems
keathley
44
3.2k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.6k
Transcript
tRPC の紹介 Press Space for next page
自己紹介 📝 飯野陽平(wheatandcat ) 🏢 法人設立(合同会社UNICORN 代表社員) 💻 Work: シェアフル株式会社CTO
📚 Blog: https://www.wheatandcat.me/ 🛠 今までに作ったもの memoir ペペロミア MarkyLinky Atomic Design Check List
tRPC とは? スキーマやコード生成なしで型安全なAPI の連携が行えるPRC フレームワーク 比較対象としては、GraphQL やOpenAPI などの技術に該当 TypeScript に特化しており、非常にシンプルに実装できる
BFF として利用されることも多い
そもそもRPC とは? RPC ( Remote Procedure Call ) とは、ネットワークで繋がっている別のコンピュータのプログラムを実行 できるようにする技術
クライアント側からサーバー側への疎通を行う際に、具体的な通信手段やプロトコルを意識することな く、関数の呼び出しを行うことがでやり取りが行える仕組み Google が開発したRPC のプロトコルなので、gRPC という命名になっている tRPC のt はTypeScript を表している
学ぶモチベーション OpenAPI やGraphQL だとスキーマ定義が必須でコード以外の実装が入りファットになりがち またフロントエンド側を型安全にするためには、さらに別途でコード生成の対応が必要 気軽にかつ型安全を保ちながらAPI を実装したい なので、tRPC を使ってみた
サーバー側の実装 参考:quickstart import { createHTTPServer } from "@trpc/server/adapters/standalone"; import {
initTRPC } from "@trpc/server" const t = initTRPC.create(); const appRouter = t.router({ helloWorld: t.procedure.query(() => { return "Hello World"; }), }); export type AppRouter = typeof appRouter; const server = createHTTPServer({ router: appRouter, }); server.listen(8000);
クライアント側の実装(スクリプト) import { createTRPCProxyClient, httpBatchLink } from "@trpc/client"; import type
{ AppRouter } from "./server"; const trpc = createTRPCProxyClient<AppRouter>({ links: [ httpBatchLink({ url: "http://localhost:3000", }), ], }); trpc.helloWorld.query().then((res) => { // output "Hello World" console.log(res); });
クライアント側の実装(スクリプト) コードでは以下のように補完が表示される
クライアント側の実装(React) 公式でHooks が提供されているので使用して実装 https://trpc.io/docs/reactjs/setup
クライアント側の実装(React) ▪ containers/Trpc.tsx import { useState } from "react"; import
{ QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { httpBatchLink } from "@trpc/client"; import { trpc } from "../utils/trpc"; type Props = { children: React.ReactNode; }; function Trpc(props: Props) { const [queryClient] = useState(() => new QueryClient()); const [trpcClient] = useState(() => trpc.createClient({ links: [ httpBatchLink({ url: "http://localhost:8080", }), ], }) ); return ( <trpc.Provider client={trpcClient} queryClient={queryClient}> <QueryClientProvider client={queryClient}> {props.children} </QueryClientProvider> </trpc.Provider> );
クライアント側の実装(React) ▪ Page.tsx import { trpc } from "./utils/trpc"; export
default function IndexPage() { const userQuery = trpc.helloWorld.useQuery(); return <div>{userQuery.data}</div>; }
デモ: React + tRPC + Prisma で実装 ▪ GitHub https://github.com/wheatandcat/tRPC-demo/tree/main/trpc-react-prisma
デモを見せながらコードを解説 Prisma でDB の定義を行う server/prisma/schema.prisma tRPC とPrisma の連携 server/src/router.ts React との連携 client/src/Page.tsx
補足: Deno + tRPC を試す ▪ GitHub https://github.com/wheatandcat/deno-trpc-demo モチベーション 何だかんだでTypeScript
をデプロイするのが面倒だったので、Deno で行けたら1 つで全て済むと思 って試してみた 結果 tRPC の実装はできるけど、Deno で実装したファイルをimport できるのはDeno のみなので、結果フ ロントエンドもDeno で実装する必要がある こうなってくると旨味が薄れるので、まだ実用段階ではなかったなという感想
補足: 手軽にtRPC を試すならCreate T3 App がおすすめ T3 Stack のdemo アプリを簡単に作成できる
フロントエンド界隈で新しく提唱されているT3 Stack について調べてみた 実行方法は以下を参照 Introduction • Create T3 App
まとめ コードジェネートして型安全をする方法に慣れきっていたが、tRPC はコードジェネートしなくても型安 全が実現できるので手軽感がかなりある TypeScript 周りのツールも大分充実してきて書きやすいので、tRPC を使っていくのも全然有り BFF 構成なら、GraphQL より筋が良さそう
ご清聴ありがとうございました 🎉