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
Transcript
15 slides
意外と難しい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
More Decks by uhyo
See All by uhyo
AIフレンドリーなGenerative UIをJSXで
uhyo
2
150
Content Mapper APIの応用例~CSS Modules、言語ファイル~
uhyo
2
700
Reactの設計論
uhyo
26
18k
型があると何が嬉しいか
uhyo
3
180
Baseline対応のDOMの型定義を作った
uhyo
3
1.1k
AIのReact習熟度を測る
uhyo
2
1.1k
React、まだ楽しくて草
uhyo
7
5.3k
TypeScript 7.0の現在地と備え方
uhyo
6
4k
React 19時代のコンポーネント設計ベストプラクティス
uhyo
20
11k
Other Decks in Technology
See All in Technology
OCI Artifact による自動運転タクシーに向けた OTA 基盤の構築
biwashi
1
300
AI-Ready なデータ基盤ってなに? & 新卒未経験が、いま考えていること
imuta
0
1.1k
新規事業開発でのAWS活用成功談&失敗談! 〜新規事業開発を専門に行うRelicならではのAWS活用〜
amixedcolor
1
140
認知の質も文化醸成も。数年取り組んで見えてきた技術広報のKPI設計 / Tech PR KPIs: Lessons on Awareness Quality and Culture After Several Years
bitkey
PRO
0
220
AWS DevOps Agentの構造から読み解く運用 AI エージェントの設計原則/aws-devops-agent-design-principles
tomoki10
0
280
パスワードのない鍵で、 データを手元に置く
ginjihayashi
0
400
PHPで家庭用ゲーム機の専用コントローラーをMIDIデバイスとして扱ってみる
wind111
0
200
Del vibe coding a un Desarrollo (agéntico) fiable
jcabot
0
220
HTTP通信に使おうとすると実はめんどくさいfile_get_contents
maguroalternative
0
260
プラガブルで宣言的なソフトウェアファクトリーを支えるTypeScript
timakin
4
940
データから見る AI時代のエンジニア転職と採用
junya_okura
4
1.3k
Libérez Firefox et Thunderbird avec nos astuces
hellosct1
0
120
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.7k
Discover your Explorer Soul
emna__ayadi
2
1.3k
SEO for Brand Visibility & Recognition
aleyda
0
4.9k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Speed Design
sergeychernyshev
33
2.3k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
The Pragmatic Product Professional
lauravandoore
37
7.5k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.4k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
33
5.9k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
380