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
470
メルカリ社員100人に聞いたGoLandの使い方 / JetBrains Night Tokyo 2018
vvakame
27
13k
OSS入門 世界に参加する最初のひと押し / OSS first step
vvakame
7
790
GCP Compute 概要と選定 / DevFest Tokyo 2018
vvakame
12
1.7k
『Re:VIEW+CSS組版やっていき』を やった話とWebエンジニアが期待する未来 / CSS Publishinng for Web Developers
vvakame
3
9.3k
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
HQ駆動開発で最高のAI-Contextを作ろう
sansantech
PRO
1
110
IBC 2026 動画技術関連レポート / IBC 2026 Report
cyberagentdevelopers
PRO
2
440
増え続けるプロダクトの品質をどう担保するか。ProductSpecとTypeScriptによる取り組み
joere
3
490
リアクションエンジニア ― 小さなリアクションから、話しかけやすい組織をつくる ―
ryuguyana
1
130
AI時代に求められる優秀なTechLeadの振る舞い
pospome
11
15k
E2Eってつらくないですか?E2Eの管理コストを下げる手段とその実践
akaboshinit
6
3k
DBTS2026 盛り上がってますか!?Databricksが実現するAI時代のデータ基盤
akuwano
1
190
Oracle Database Security 詳細解説
oracle4engineer
PRO
2
270
生成AI時代のIAMのマニアックな話
takuros
6
2.6k
Kiro 入門 (1 つのエージェントを、どの画面からでも) | 目指せ Kiro マスター 01
inariku
PRO
4
840
Content Mapper APIの応用例~CSS Modules、言語ファイル~
uhyo
2
610
TSKaigi Sendai 2026 - tsgo時代のTypeScript Playgroundはどう動くか
teamlab
PRO
0
110
Featured
See All Featured
Skip the Path - Find Your Career Trail
mkilby
1
290
ラッコキーワード サービス紹介資料
rakko
5
5.7M
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
2
3k
Designing for Timeless Needs
cassininazir
1
550
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
56
3.5k
Mobile First: as difficult as doing things right
swwweet
225
10k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
630
Building Applications with DynamoDB
mza
96
7.3k
Site-Speed That Sticks
csswizardry
13
1.6k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
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Ͱຊग़͢…ʁ(མͪͳ͍Ͱ ͜Ε͖͢
͓͠·͍