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
意外と難しいGraphQLのスカラー型
Search
uhyo
September 22, 2025
Technology
1.4k
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
意外と難しいGraphQLのスカラー型
2025-09-22 『TypeScriptのスキーマ駆動開発』実践事例 LT
uhyo
September 22, 2025
More Decks by uhyo
See All by uhyo
Content Mapper APIの応用例~CSS Modules、言語ファイル~
uhyo
2
370
Reactの設計論
uhyo
26
17k
型があると何が嬉しいか
uhyo
3
170
Baseline対応のDOMの型定義を作った
uhyo
3
1k
AIのReact習熟度を測る
uhyo
2
1.1k
React、まだ楽しくて草
uhyo
7
5.3k
TypeScript 7.0の現在地と備え方
uhyo
6
3.9k
React 19時代のコンポーネント設計ベストプラクティス
uhyo
20
11k
型定義でAIと会話する:型を通じてAIに意図を伝えるテクニック
uhyo
1
130
Other Decks in Technology
See All in Technology
増え続けるプロダクトの品質をどう担保するか。ProductSpecとTypeScriptによる取り組み
joere
2
250
新米を「売り越さない」、Aurora DSQLで作る在庫ゼロ死守の限定ドロップEC
yama3133
0
110
外国人エンジニアがVueプロトタイプで仕様を合わせている話 / Vue Fes Japan 2026 LT
eluwing
0
330
Vibe-to-Spec開発_実践ガイド
happysamurai294
0
130
AIエージェントに 権限と鍵をどこまで渡すか
nrinetcom
PRO
2
160
cdk deployとcdkd deploy、実際にデプロイして5倍速近くの差を実測した
yama3133
1
130
今から始めるAWS環境の暗号移行 - ポスト量子時代に備える
sato4mi
0
130
1週間、Opus封印でお願いします
kintotechdev
0
400
200人の配信コメント、 全部Jevが仕分けます 〜 マサカリガードを作ってみた 〜
katayamatg
1
470
kintoneで在庫管理システムを作るときの要件定義・設計ポイント
kai0707
0
190
【Oracle AI SPOTLIGHT ウェビナー】事例から見る!なぜOCIが選ばれたのか ― AI時代のクラウド戦略を紐解く ―
oracle4engineer
PRO
1
140
これだけは抑えておきたい!後悔しない re:Invent の過ごし方 / AWS re:Invent 2026 prestudy
kinunori
2
590
Featured
See All Featured
Evolving SEO for Evolving Search Engines
ryanjones
0
340
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.5k
Amusing Abliteration
ianozsvald
1
370
How Software Deployment tools have changed in the past 20 years
geshan
2
35k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
A Modern Web Designer's Workflow
chriscoyier
699
190k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
610
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
2
2.2k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
141
35k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.6k
AI: The stuff that nobody shows you
jnunemaker
PRO
10
1.2k
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