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サーバを立てるぞ! / Building GraphQL server b...
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Masahiro Wakame
August 10, 2018
Technology
4.9k
15
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GoでGraphQLサーバを立てるぞ! / Building GraphQL server by go
mercari.go #2 登壇資料です
https://mercari.connpass.com/event/95665/
Masahiro Wakame
August 10, 2018
More Decks by Masahiro Wakame
See All by Masahiro Wakame
社内フレームワークとその依存性解決 / in-house framework and its dependency management
vvakame
1
790
DatastoreからSpannerに 移行したいぞ途中編 / migrate Datastore to Spanner in progress
vvakame
0
1.3k
Google Cloud Next 2019 わくわく報告会 / Google Cloud Next 2019 WakuWaku Report
vvakame
1
460
メルカリ社員100人に聞いたGoLandの使い方 / JetBrains Night Tokyo 2018
vvakame
27
13k
OSS入門 世界に参加する最初のひと押し / OSS first step
vvakame
7
780
GCP Compute 概要と選定 / DevFest Tokyo 2018
vvakame
13
1.7k
『Re:VIEW+CSS組版やっていき』を やった話とWebエンジニアが期待する未来 / CSS Publishinng for Web Developers
vvakame
3
9.2k
go.mercari.io/datastore はいいぞ! / go.mercari.io/datastore is pretty good!
vvakame
4
1.2k
最強のDatastoreライブラリを作った / golang.tokyo #11
vvakame
3
13k
Other Decks in Technology
See All in Technology
"優秀な誰か"の下位互換にならないために
yut0naga1_fa
1
1.9k
QA4AI 2026-Oct 生成AI時代の品質保証
tokuty
1
900
Claude の新機能を、 出た日に触って社内に届けるまで
rfdnxbro
1
190
WasmをRustにする
sansantech
PRO
0
300
Getting Started with Google Antigravity
glaforge
0
330
スキマバイトという雇用形態は存在しないので、雇用期間1日のアルバイト契約を事業者とワーカーが結ぶ手続きを支えるのがタイミーなんです
akitoshiga
0
210
AI コーディングと人間の認知スループット / CTO Night & Day 2026 Morning Session / AI Coding and Human Cognitive Throughput
prog893
5
1.3k
Metadata - Section 1: HTML Basics
mohab1
0
130
React改造してみた
tsukina_7mochi
0
130
触りたくないデータに、どう触るか AI時代のRBACアクセスガバナンス
ivry_presentationmaterials
0
280
DGX Spark でどこまでハッキングができるのか?
kash1064
3
1.4k
Persisting Massive Data with Rails ~Designing for 60 billion rows, and fighting the bottlenecks~ Railsで挑む大量データ永続化 〜600億件を永続化するための設計とボトルネックとの戦い〜
occhi
0
740
Featured
See All Featured
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
58k
The Pragmatic Product Professional
lauravandoore
37
7.5k
So, you think you're a good person
axbom
PRO
2
2.2k
Fireside Chat
paigeccino
43
4.1k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
RailsConf 2023
tenderlove
30
1.6k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4.4k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
3k
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
740
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
Transcript
GoͰGraphQLαʔόΛཱͯΔͧʂ mercari.go #2 Θ͔Ί ·͞ͻΖ #mercarigo bit.ly/2vyiluP
Θ͔Ί ·͞ͻΖ @v vakame Masahiro Wakame GAE/Go TypeScript ٕज़ॻయ
TL;DR •GraphQLͬͯԿʁ •GitHub v4 APIΛ৮ͬͯΈΔ •github.com/99designs/gqlgen •ઃܭϕετϓϥΫςΟε ΫϥΠΞϯτͷ͠ͳ͍
GraphQLͬͯԿʁ ࡉ͔͍͜ͱ ࠷ޙͷࢀߟࢿྉूͰʂ
GraphQLͬͯԿʁ •Facebook͕2015ʹެ։༷ͨ͠ •άϥϑߏʹର͢ΔQuery Language •༑ୡͷ༑ୡΛ5ਓͣͭऔಘ͢Δͱ͔ •Client→Serverͷσʔλཁٻ࣌ʹ͏
GraphQLͬͯԿʁ RESTͳΒ3RPCඞཁ GraphQLͳΒ1ճ { circle(id: "Q2lyY2xlRXhoaWJpdEluZm86MTIzNAo") { name spaces penName
webSiteURL genre genreFreeFormat products(first: 9) { pageInfo { hasNextPage endCursor } nodes { name description type page price relatedURLs firstAppearanceEventName firstAtTechBookFest images { url height width } } } loginUserChecked { checked } } }
େࣄͳ͜ͱ •ΘΓͱ੍͋Δ (N+1ରࡦͱ͔) •ແݶʹૣ͘ͳ͍ •REST APIͷ্ҐޓͰͳ͍ •ࠩҟΛΑ͘ཧղ͠Α͏
ϝϧΧϦͰͬͯΔʁ •C Sπʔϧ PHP+webonyx/graphql-php •US ϝϧΧϦWeb ??? •MicroserviceԽ CSπʔϧ Go+gqlgen
•ϝϧΧϦWeb Node.js+Apollo •ϝϧϖΠ CSπʔϧ(ݕ౼த) Go+gqlgen ٕज़ॻయWeb࣮ݧதͰ͢
ClientͷྲྀΕ •·ͣSchema(+αʔό)͕͋Δ •ͦ͜ʹOperationΛ͛Δ •Query, Mutation, Subscribe ͷ3छ •ཁٻͨ͠௨ΓͷܗࣜͰσʔλདྷΔ
Queryͷྫ { viewer { id login name location bio company
repositoriesContributedTo(first: 2) { nodes { owner { login } name } } } } { "data": { "viewer": { "id": "MDQ6VXNlcjEyNTMzMg==", "login": "vvakame", "name": "Masahiro Wakame", "location": "Tokyo, Japan", "bio": "TypeScript and GAE/Go", "company": "Merpay, Inc.", "repositoriesContributedTo": { "nodes": [ { "owner": { "login": "mruby" }, "name": "mruby" }, { "owner": { "login": "DefinitelyTyped" }, "name": "DefinitelyTyped" } ] } } } } ରԠͨ͠ߏ͕ฦΔ
ClientͷྲྀΕ •ͭ·Γ… •ΫΤϦʹΑͬͯಘΒΕΔσʔλ͕มΘΔ •ΫΤϦݸผʹฦΓͷσʔλͷܕཉ͍͠ •→ݴޠʹΑ͍ͬͯͮΒ͍Մೳੑʁ ·͊JSͱ૬ੑྑͦ͞͏ͳͷΘ͔Δ
ͬ͘͟ΓServerݪཧ •Query͕དྷΔ •ฦ͢σʔλTreeߏʹͳΔ •Treeͷઅ(ͱ͔)ຖʹResolverΛఆٛ͢Δ •Resolver͕ཁٻ͞ΕͨNodeΛੜ͠ฦ͢ •Resolverͷू߹ == αʔόͷ࣮
ͬ͘͟ΓServerݪཧ type ResolverRoot interface { Query() QueryResolver User() UserResolver CircleExhibitInfo()
CircleExhibitInfoResolver ProductInfo() ProductInfoResolver } type QueryResolver interface { Circle(ctx context.Context, id string) (*CircleExhibitInfo, } type CircleExhibitInfoResolver interface { ID(ctx context.Context, obj *CircleExhibitInfo) (string, err Products(ctx context.Context, obj *CircleExhibitInfo, first } type ProductInfoResolver interface { ID(ctx context.Context, obj *ProductInfo) (string, error) Images(ctx context.Context, obj *ProductInfo) ([]Image, erro } query { circle(id: "Q2lyY2xlRXhoaWJpdEluZm86MTIzNAo") { name spaces penName webSiteURL genre genreFreeFormat products(first: 9) { pageInfo { hasNextPage endCursor } nodes { name description type page price relatedURLs firstAppearanceEventName firstAtTechBookFest images { url height width } } } loginUserChecked { checked } }
ͬ͘͟ΓServerݪཧ Resolverͷू߹ ʹ αʔόͷ࣮
͖͜͜ •୯ҰΤϯυϙΠϯτ •Introspection •SchemaΛΔʹGraphQL͕͑Δ •ڧ͍ܕ͚ʂ
GitHub v4 APIΛ৮Δ
৮Γ·͠ΐ͏ •developer.github.com/v4/explorer/ •GraphiQLʹ৮ΕΔ •GitHubͷߟ͑ͨ࠷ڧͷઃܭʹ৮ΕΔ •͋ΘΑͬͯ͘ΈΔ
σϞ •ϩάΠϯϢʔβͷ໊લग़͢ •ҙͷϦϙδτϦ͕࡞ΒΕͨग़͢ •GraphQLʹؔ࿈ͷ͋ΔUserΛநग़͢Δ
ίʔυ͔Β •దʹPOST͢Δ͚ͩ •Authorization: bearer ${PersonalAccessToken} •https://api.github.com/graphql •https://github.com/settings/tokens bit.ly/2Oo8DlD
github.com/99designs/gqlgen I’m committer!
ͳͥGoͳͷ͔ʁ •Go͍͠ฒߦॲཧ؆୯ͩ͠ •Microservice܈͕GoͳͷͰ…ʂ •GAE/GoͰಈ͘ͷͰ…ʂ •ࣾʹPHPNode.js͍·͢ •Frontend Engineer
GoओཁϥΠϒϥϦൺֱ •github.com/graph-gophers/graphql-go •github.com/graphql-go/graphql •github.com/samsarahq/thunder •github.com/99designs/gqlgen
GoओཁϥΠϒϥϦൺֱ •github.com/graph-gophers/graphql-go •github.com/graphql-go/graphql •github.com/samsarahq/thunder •github.com/99designs/gqlgen ػೳ͕ͪΐͬͱශऑ GoͷίʔυͰSchemaॻ͖ͨ͘ͳ͍ ಉ্ ΘΓͱྑͦ͞͏ʁ
ͳͥgqlgen͔ʁ •ϘΠϥʔϓϨʔτݏʂ •શίʔυੜݏʂ •ίʔυੜ+खॻ͖ΛఔΑ͘… •ΑΖ͍͠ͳΒgqlgenͩʂ
gqlgenྑ͍ͷ͔ʁ ྑ͘͠·ͨ͠❤ Ͱcommitterʹͳͬͨ
gqlgenͷαϙʔτൣғ •Query •Mutation •Subscription •Directive •import syntax •type extend ͋Δ
΄͍͠ ͦͷ͏ͪΓ·͢ committer͔ͩΒͶ
σϞ •gqlgen init ͯ͠ΈΔ •มߋΛՃ͑ͯΈΔ •gqlgen gen ͯ͠ΈΔ •࣮ࡍʹୟ͍ͯΈΔ ࣌ؒ…͋Δʁ
gqlgen.com/ getting-started/
ύʔπͷղઆ •Resolver •Directive •Middleware •ErrorPresenter
ύʔπͷղઆ •Resolver •Directive •Middleware •ErrorPresenter ੜ͞Εͨinterface࣮ͯ͠ʂ ੜ͞ΕͨDirectiveʹ࣮Λ༩͑ͯʂ దʹϩΪϯάͱ͔ʹ͑·͢ ΤϥʔͷΧελϚΠζͰ͖·͢ gitter.im/gqlgen/Lobby
gqlgen.com
Middleware •ResolverMiddleware •Resolverͷಈ࡞લޙʹॲཧΛڬΉ •RequestMiddleware •Req,RespʹॲཧΛڬΉ(ओʹϩΪϯά) bit.ly/2MmjbEE
ઃܭϕετϓϥΫςΟε ຊʹϕετ͔͠ΒΜʂ
RelayΛษڧ͢͠ •RelayΛ࠾༻͠ͳͯ͘ษڧͯ͠ •Server͚ͩߟ͑ΔͱΠϛϑͳ༷͕͋Δ •ओʹFragmentपΓ •UIଆΛඒ͘͠ઃܭ͢ΔͨΊ TSαϙʔτ͖ͨ github.com/facebook/relay/pull/2293
RelayΛษڧ͢͠ •Relay spec 3ܑఋ •cursor, global object, input object •specຊମ͡Όͳ͍ͷͰཤमಀ͕ͪ͠
•GitHub౿ऻ͍ͯ͠Δ •bit.ly/2npeSdv
N+1 •Πϕϯτ3ͭ×αʔΫϧ100݅×൦9ݸ •1+3×1+3×100×1 = 304ճͷDBΞΫηε •ؤுͬͯ࠷దԽ͢Δͱ 1+1+1 ??
N+1 •gqlgenResolverΛฒྻॲཧ͢Δ •→ू͕͍͠ʂ •github.com/vektah/dataloaden •ࢦఆظؒͷϦΫΤετΛҰׅԽ •·ͩະධՁ… ྑ͍ํ๏ࢥ͍ͭ͘ʁ
REST APIͱͷରൺ •REST API = RPC •GraphQL = QueryLanguage •Τϥʔʹର͢Δߟ͑ํ͕ࠜຊతʹҧ͏
•“ਖ਼͍͠Requestͷ͛ํ” ͱ͍͏֓೦ •ڻ͖࠷খͷݪଇ & σόοΨϏϦςΟ
REST APIͱͷରൺ •Τϥʔʹର͢Δߟ͑ํϚδॏཁ •େࣄͳ͜ͱͳͷͰ2ճݴ͍·ͨ͠ •GraphiQLͰ݁͢ΔσόοΨϏϦςΟ •੍ޚͳΔ͘DirectiveϨϕϧͰ ϑϧεΫϥονͰ͖ΔͳΒͦΕ͕Α͍
Schemaઃܭ •Schema͔ΒಡΈऔΕΔใΛ૿͢ •DirectiveΛͬͨScope੍ޚͱ͔ •DBͷ1table=1typeʹ͢Δͱ͔ •มʹ߹ମͤͨ͞Γׂͨ͠Γ͠ͳ͍
AuthपΓ •ಛʹܾ·༷͕ͬͨͳ͍ •ͷͰɺී௨ʹHTTP͢Δ •ηογϣϯ, OAuth2 etc…
·ͩΘ͔ΒΜ
ςετͲ͏͢Δʁ •ࠓͷͱ͜Ζgolden testingͯ͠Δ •Resolver, DirectiveΛݸผʹςετʁ
Mock? •αʔόଆ࣮͕͋ͱʹͳΔ߹ •ΫϥΠΞϯτଆʹͲ͏ ָͤ͞Δ͖ʁ
BFF vs ࣮ •جຊ࣮ͷ΄͏ָ͕ͦ͏ •Backend Engineerతʹ •Microserviceͷͱ͖ʁ •݁ہBFFతଘࡏʹͳΔͷͰ…ʁ
ӡ༻ͷ •ϝτϦΫεऔΓ͍ͨʁ •ϩάऔΓ͍ͨʁ •statsੳ͍ͨ͠ʁ
ΫϥΠΞϯτଆϫʔΫϑϩʔ •ͳʹ͔Θ͔ΒΜ •ಛʹAndroid, iOSͷݟ͕ͳ͍ •Webదʹͬͯ΄͍͠
ࢀߟࢿྉ
ࢀߟࢿྉू •b.hatena.ne.jp/vvakame/GraphQL •graphql.org •www.howtographql.com •ٕज़ॻయ5Ͱຊग़͢…ʁ(མͪͳ͍Ͱ ͜Ε͖͢
͓͠·͍