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
GoによるGraphQLの実装
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
keinuma
October 06, 2020
Programming
3.4k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GoによるGraphQLの実装
keinuma
October 06, 2020
Transcript
39 slides
Go によるGraphQL の 2020/10/6 沼⽥ 1 / 39
⾃⼰ key value 前 ぬまた 業 エンジニア カミナシ JavaScript, Golang
2 / 39
アジェンダ . GraphQL . Go を使うメリット . GraphQL + Go
の . gqlgen . gqlgen の 例 . まとめ 3 / 39
GraphQL 4 / 39
Facebook が開発したWeb API のための Alt REST API スキーマとクエリからなる 5 /
39
スキーマ データ、API のインターフェースを 義 Query が 照、Mutation が更 API 6
/ 39
7 / 39
クエリ スキーマ 義をもとに 義 レスポンスに必 なデータを 択 8 / 39
=> 9 / 39
GraphQL の 徴 エンドポイントが⼀つ (/graphql) API クライアント(GraphiQL )が提供されている スキーマ 義、
が クエリの柔 性が い 10 / 39
Go を使うメリット 11 / 39
Go の構 体とGraphQL のスキーマをマッピング可 リゾルバーをダックタイピングで GraphQL の 性を構 する 12
/ 39
GraphQL + Go の 13 / 39
BaaS or 独⼒で BaaS はGraphQL のコア の が提供されている GraphQL の
がシンプルな場 、BaaS がよさそう 14 / 39
BaaS の例 AWS 提供のマネージドサービス DynamoDB, Lambda などAWS のサービスと できる Heroku
+PostgresQL によるマネージドサービス データベースのテーブルを 義するとAPI を⽣成できる AppSync Hasura 15 / 39
独⼒で する場 スキーマファーストかコードファーストを 択 スキーマファースト クライアントとバックエンドの を並列に め やすい コードファースト
開発フローが⼀ 向になる 16 / 39
コードファーストのライブラリ スキーマとリゾルバーをコードで 現し、スキー マを ⼒ Go のstruct からスキーマを⽣成する graphql-go thunder
17 / 39
スキーマファースト のライブラリ ランタイム時にスキーマとコードをチェック CLI からスキーマをコードに graph-gophers/graphql-go gqlgen 18 / 39
gqlgen 19 / 39
gqlgen の使い スキーマを 義 gqlgen コマンドを ⾏ 20 / 39
⽣成されるファイル model.go : スキーマに 義されているデ ータファイル resolver.go: GraphQL の り
にデータ をマッピング generated.go: リクエストパラメータか らモデルを⽣成( しない) 21 / 39
gqlgen の利点 GraphQL のサポート範囲が い 型 ⾃動⽣成を利⽤しやすい 22 / 39
GraphQL のサポート範囲が い 23 / 39
型 CLI ⾏時に型の 性を れる interface{} は発⽣しない 24 / 39
⾃動⽣成を利⽤しやすい スキーマとGo の がある場 は を埋めるた めのコードが ⼒される 25 /
39
Go のプロパティが不⾜している状 GraphQL type Todo { id: ID! text: String!
done: Boolean! } Go type Todo struct { ID string Text string } 26 / 39
コードに不⾜している Done を埋めるためのリゾルバーが ⼒される func (t *TodoResolver) Done(ctx context.Context //
する panic("panic") } 27 / 39
gqlgen の 例 28 / 39
使⽤技 API フレームワーク: echo ORM: GORM DB: MySQL サンプルリポジトリ 29
/ 39
トピック ドメイン3 + GraphQL GraphQL のResolver をController にする スキーマとのマッピング 30
/ 39
ドメイン3 + GraphQL model + service + repository model にドメインを
service にロジックを repository はinterface のみ はinfra に 31 / 39
API リクエスト時にController を ⾏ Presenter はResponse を⽣成 Repository はインターフェースのみ Infra
でDB の処理を ⾏ 32 / 39
Controller →Resolver Model →GraphQL Model 33 / 39
のユースケース エラーハンドリング 34 / 39
firebase auth を利⽤ echo のmiddleware でトークンの を ⾏ トークン 得
はユーザー情 をResolver に して各API に している 35 / 39
Schema にディレクティブを のチェック処理をディレクティブのResolver に する directive @hasRole(role: Role!) on FIELD_DEFINITION
enum Role { ADMIN USER } 36 / 39
エラーハンドリング Resolver のcontext に してエラーを めていく GraphQL はエラーコードを持たないので して する
37 / 39
エラーを するヘルパー func AddError(ctx context.Context) (bool, error) { graphql.AddError(ctx, &gqlerror.Error{
Path: graphql.GetPath(ctx), Message: "A descriptive error message", Extensions: map[string]interface{}{ "code": "4", }, }) return false, gqlerror.Errorf("BOOM! Headshot") } 38 / 39
まとめ GraphQL を するときはコードファーストかスキー マファーストを 択する gqlgen を利⽤して責 ごとに 離してGraphQL
を で きる スキーマが になるとリゾルバーが肥⼤ しそう 39 / 39
More Decks by keinuma
See All by keinuma
品質と新規開発のバランス / Quality and new development
keinuma
0
2.5k
Firestoreを実装してみた話
keinuma
0
68
Flutter or React Native
keinuma
1
150
Atomic Design手法
keinuma
0
110
なぜAtomic Designが生まれたか
keinuma
0
98
Other Decks in Programming
See All in Programming
マルチプロダクトSaaSの アプリ一覧を速くする——複数プロダクトにまたがる「権限行脚」を減らすまで #techramen26conf
mktakuya
1
260
Kiro Crewによる開発体験ーKiro CrewでStrands Decider 2B に関する技術検証をやってみた
satoshi256kbyte
1
230
ruby/uri meets WHATWG
jinroq
0
180
SODA - FACT BOOK(EN)
sodainc
1
300
Crash Auto Pipelines
veronikapj
1
460
Getting Started with Vibe Coding
miccheng
0
250
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
220
そのデザイントークン、Flutterでも同じ見た目になりますか?
nakamizu
0
500
止まらない比較を止める — 余帰納法で無限ストリームを比べる
tarao
0
220
Compiling Your Language to WasmGC & Friends
tanishiking
0
770
AIエージェントの「テストは通るのに本番で壊れる」を減らす
shibuiwilliam
3
130
AIコーディングをセッションで終わらせない: PHPライブラリ開発におけるコンテキスト承継設計
ickx
0
110
Featured
See All Featured
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
470
GitHub's CSS Performance
jonrohan
1033
470k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.3k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
700
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Fantastic passwords and where to find them - at NoRuKo
philnash
53
4k
Git: the NoSQL Database
bkeepers
PRO
433
67k
Making Projects Easy
brettharned
120
6.9k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
6
970
Rails Girls Zürich Keynote
gr2m
96
14k
Balancing Empowerment & Direction
lara
6
1.4k