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
300
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
63
メールアドレスを深堀りする
bary822
2
1.1k
kiba ETLで小さく始めるデータ分析基盤構築
bary822
3
260
AR_migrationの例外.pdf
bary822
0
57
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.5k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Reflections from 52 weeks, 52 projects
jeffersonlam
351
21k
For a Future-Friendly Web
brad_frost
179
9.9k
Faster Mobile Websites
deanohume
308
31k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
332
22k
The Cult of Friendly URLs
andyhume
79
6.5k
A designer walks into a library…
pauljervisheath
207
24k
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にサヨナラするのか • 学習コスト • 周辺ライブラリ