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
意外と難しいGraphQLのスカラー型
Search
uhyo
September 22, 2025
Technology
5
1k
意外と難しいGraphQLのスカラー型
2025-09-22 『TypeScriptのスキーマ駆動開発』実践事例 LT
uhyo
September 22, 2025
Tweet
Share
More Decks by uhyo
See All by uhyo
タグ付きユニオン型を便利に使うテクニックとその注意点
uhyo
2
930
ECMAScript仕様の最新動向: プロセスの変化と仕様のトレンド
uhyo
3
760
TypeScript 6.0で非推奨化されるオプションたち
uhyo
17
6.9k
Claude Code 10連ガチャ
uhyo
5
1k
AI時代、“平均値”ではいられない
uhyo
8
3.8k
RSCの時代にReactとフレームワークの境界を探る
uhyo
13
4.8k
知られざるprops命名の慣習 アクション編
uhyo
12
3.4k
libsyncrpcってなに?
uhyo
0
790
Next.jsと状態管理のプラクティス
uhyo
7
27k
Other Decks in Technology
See All in Technology
We Built for Predictability; The Workloads Didn’t Care
stahnma
0
150
Bedrock PolicyでAmazon Bedrock Guardrails利用を強制してみた
yuu551
0
260
量子クラウドサービスの裏側 〜Deep Dive into OQTOPUS〜
oqtopus
0
140
プロダクト成長を支える開発基盤とスケールに伴う課題
yuu26
4
1.4k
pool.ntp.orgに ⾃宅サーバーで 参加してみたら...
tanyorg
0
270
Oracle Cloud Observability and Management Platform - OCI 運用監視サービス概要 -
oracle4engineer
PRO
2
14k
登壇駆動学習のすすめ — CfPのネタの見つけ方と書くときに意識していること
bicstone
3
130
GitHub Issue Templates + Coding Agentで簡単みんなでIaC/Easy IaC for Everyone with GitHub Issue Templates + Coding Agent
aeonpeople
1
260
小さく始めるBCP ― 多プロダクト環境で始める最初の一歩
kekke_n
1
520
22nd ACRi Webinar - NTT Kawahara-san's slide
nao_sumikawa
0
100
[CV勉強会@関東 World Model 読み会] Orbis: Overcoming Challenges of Long-Horizon Prediction in Driving World Models (Mousakhan+, NeurIPS 2025)
abemii
0
150
OCI Database Management サービス詳細
oracle4engineer
PRO
1
7.4k
Featured
See All Featured
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
130
A Tale of Four Properties
chriscoyier
162
24k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.9k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
1.9k
Stop Working from a Prison Cell
hatefulcrawdad
273
21k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
24k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.1k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Code Review Best Practice
trishagee
74
20k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.6k
Transcript
意外と難しいGraphQLのスカラー型 2025-09-22『TypeScriptのスキーマ駆動開発』実践事例 LT
発表者紹介 uhyo 株式会社カオナビ フロントエンドエキスパート 前職ではGraphQLを使っていた。 2
注意 この話は、GraphQL + TypeScriptの定番ツール GraphQL Codegen よりも安全性にこだわった自作ツール nitrogql が流行らないことの恨み節です。 3
GraphQLとTypeScriptの型の関係 GraphQLには、型の概念が存在する。 String, Int, Boolean 型など。 TypeScriptのコード生成をする場合、GraphQLの型を TypeScriptの型に対応付ける必要がある。 例: String→string
Int→number 4 GraphQLの型 TypeScriptの型
注意が必要なID型 ID型もGraphQLに組み込みの型だが、 特殊な性質を持つ。 •文字列としてシリアライズされる。 •しかし、ID型の入力としては整数も受け付ける。 5
注意が必要なID型 ID → string ? ID → string | number
? 6
注意が必要なID型 const data = useQuery({ query, variables: { inputId: 12345
}, }); data.outputId 7 このIDは string | number になるのが望ましい (シリアライズ前のため) このIDは string になるのが望 ましい (シリアライズ済のため)
GraphQL Codegenでは…… 状況に応じてID型に対応するTypeScriptの型を 出し分ける必要がある。 ところが、GraphQL Codegenは一律で string型を出力。(デフォルトの設定の場合) 本来numberが使えるはずの場面で使わせて くれない! 8
設定を変えればいける こんな設定を書けばGraphQL Codegenでも 出し分けが可能。 scalars: { ID: { input: ‘string
| number’, output: ‘string’ } } 9
GraphQL Codegenでの出し分け const data = useQuery({ query, variables: { inputId:
12345 }, }); data.outputId 10 ここではinput用の型を使用 (string | number) ここではoutput用の型を使用 (string)
めでたし……と思いきや サーバー側(GraphQLのリゾルバー定義) 向けの型もGraphQL Codegenで出力すると…… deleteUser: (_, { id }) =>
{ return userStore.delete(id); } 11
めでたし……と思いきや サーバー側(GraphQLのリゾルバー定義) 向けの型もGraphQL Codegenで出力すると…… deleteUser: (_, { id }) =>
{ return userStore.delete(id); } 12 入力なのでinput用の型が使用 されるが、正しい型はstring (string | numberではない!) ※逆に、resolverの実装から出力されるIDは string | number型が正しい
GraphQL Codegen向けの正しい設定 こうする必要がある。 どうしてこうなった…… scalars: { //クライアント用 ID: { input:
‘string | number’, output: ‘string’ } } 13 scalars: { //サーバー用 ID: { input: ‘string’, output: ‘string | number’ } }
一方nitrogqlでは input/outputではなくsend/receiveという分けを 導入することで、両者で共通の定義にできる。 scalarTypes: { //クライアント・サーバー共通 ID: { send: ‘string
| number’, receive: ‘string’ } } 14
まとめ GraphQL CodegenはGraphQL + TypeScript向け のデファクトスタンダードなツールだが、 型安全に使うために適切に設定する難易度が とても高い。気を付けよう。 15