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
64
メールアドレスを深堀りする
bary822
2
1.2k
kiba ETLで小さく始めるデータ分析基盤構築
bary822
3
260
AR_migrationの例外.pdf
bary822
0
59
Featured
See All Featured
Designing for Performance
lara
610
69k
Building an army of robots
kneath
306
46k
Thoughts on Productivity
jonyablonski
70
4.8k
A better future with KSS
kneath
239
17k
Rebuilding a faster, lazier Slack
samanthasiow
83
9.2k
Gamification - CAS2011
davidbonilla
81
5.4k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
46
7.6k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
Embracing the Ebb and Flow
colly
87
4.8k
The Invisible Side of Design
smashingmag
301
51k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
18
1.1k
The Cost Of JavaScript in 2023
addyosmani
53
8.9k
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にサヨナラするのか • 学習コスト • 周辺ライブラリ