Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
GraphQLを_Rubyで気軽に試す.pdf
Search
Hiroto Fukui
August 31, 2018
0
220
GraphQLを_Rubyで気軽に試す.pdf
Hiroto Fukui
August 31, 2018
Tweet
Share
More Decks by Hiroto Fukui
See All by Hiroto Fukui
Deploy from slack
bary822
0
48
メールアドレスを深堀りする
bary822
2
1.1k
kiba ETLで小さく始めるデータ分析基盤構築
bary822
3
220
AR_migrationの例外.pdf
bary822
0
46
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
268
26k
Optimising Largest Contentful Paint
csswizardry
31
2.8k
Producing Creativity
orderedlist
PRO
340
39k
Music & Morning Musume
bryan
46
6k
Designing with Data
zakiwarfel
98
5k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
38
9.2k
The Invisible Customer
myddelton
119
13k
Building a Modern Day E-commerce SEO Strategy
aleyda
36
6.8k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
43
2k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Scaling GitHub
holman
458
140k
Thoughts on Productivity
jonyablonski
66
4.2k
Transcript
GraphQLを Rubyで気軽に試す Hiroto Fukui @bary822 Ruby関西 #83
Who 2014 - 2017 2017 - Hiroto Fukui @bary822
Who
Who Osaka Tokyo
Who
Who
Who
Who
Who
• What • Why • History • Req & Res
Example • What's special? ◦ Type ◦ Introspection • Demo • Challenges
What
Query Language
Query Language SQL GraphQL
Query Language SQL GraphQL
Why REST
/mobile • デバイス毎に最適化されたAPI • PC > mobile
/v2 • バージョン管理 • 互換性
GET /product GET /inventory GET /shipping GET /payment
docs
{ “status”: 400, “message”: “Bad Request” }
None
History • 2012 - Facebookが内部向けに開発 • 2015 - 仕様がRFCとして公開される •
2015 - React Europeでお披露目される • 2018 - Working Draft
増え続けるエンドポイント 滞るドキュメント更新 止まない問い合わせ
None
Automatically Solved!! • 単一エンドポイント • 適切で具体的なエラーメッセージ • ドキュメントの更新
Example
Request POST http://api.example.com/graphql?{dog(dogId:1):{name,wei ght,color}}
Request POST http://api.example.com/graphql? { dog(dogId: 1):{ name, weight color }
}
Response { "data": { "dog": { "name": "Goemon", "weight": 20.0,
"color": "Red" } } }
☝ • Requestに記述した欲しいデータだけが同じフォーマットで 帰ってきている • 1つのエンドポイント(/graphql)だけですべてのリソースを提供 できる • データを取得しているけど、HTTP methodはPOST
Type
DogType { name: String weight: Float color: String favoriteToys: [ToyType]
friends: [DogType] }
Client Server DogType ToyType name, color name DogType ToyType name,
color name
None
Client Server /dog name, color, weight, ... DogType ToyType OwnerType
FoodType name, category, ... name, gender, ... name, ingredient, ...
None
Introspection
POST http://.../graphql { IntrospectionQuery: { ... } } { "Dog":
{ "name": "String", "weight": "Float", "color": "String", ... } "Owner": { name: "String", dogs: ["Dog"] } }
None
Client Server DogType ToyType Self-Validation Static-Validation Auto-Generated Error Message
# Request POST http://api.example.com/graphql?query= { dog(dogId: "Not an integer"){ name
} }
# Response { "errors": [ { "message": "Argument 'dogId' on
Field 'dog' has an invalid value. Expected type 'Int!'.", "locations": [{ "line": 2, "column": 2} ], "fields": [ "query", "dog", "dogId" ] } ] }
mid-summary • GraphQLは欲しいデータを記述するためのAPI問い合わせ言 語である • GraphQLを使うと複数のクライアントの要望に対応しやすく、 その後のメンテナンスもしやすい • GraphQLは強く型付けされた言語であり、それによってAPI運 用にで発生する典型的な辛みを解決できる
None
graphql-ruby
https://github.com /bary822/graphql-api-rails
None
(demo) - auto-genrated doc - req&res - validation
Challenges • 既に多くのREST APIクライアントを抱えている場合 • いつRESTにサヨナラするのか • 学習コスト • 周辺ライブラリ