Slide 1

Slide 1 text

入門 Hasura 〜 HasuraでサクッとGraphQL 体験 〜 @RyoKawamata

Slide 2

Slide 2 text

自己紹介

Slide 3

Slide 3 text

@RyoKawamata 消防士歴 6年半, エンジニア歴 1年半 株式会社Misoca リモートワーク@茨城

Slide 4

Slide 4 text

今日話すこと

Slide 5

Slide 5 text

GraphQLの簡単な紹介 Hasuraで出来ること・出来ないこと

Slide 6

Slide 6 text

GraphQL とは何か?

Slide 7

Slide 7 text

API向けの問い合わせ言語・仕様

Slide 8

Slide 8 text

問い合わせ言語?

Slide 9

Slide 9 text

APIを対象にしたSQL的なもの

Slide 10

Slide 10 text

あるユーザーの 名前、会社名、URLを取得したい example

Slide 11

Slide 11 text

SELECT name, company, url FROM users WHERE id = '1234' SQL DB

Slide 12

Slide 12 text

SQL DB name compony url ryo misoca misoca.jp

Slide 13

Slide 13 text

query getUser { user(id: ‘1234’) { name company url } } GraphQL API Server

Slide 14

Slide 14 text

“data” { ”user” { “name”: “ryo” “company”: “misoca” “url”: “misoca.jp” } } GraphQL API Server

Slide 15

Slide 15 text

● 必要なものを必要な分だけ取得できる ● 単一のエンドポイント ● 強力な型システムを持つ feature

Slide 16

Slide 16 text

REST API の課題とGraphQL

Slide 17

Slide 17 text

過剰な取得 必要のないデータまで まとめて取得してしまう

Slide 18

Slide 18 text

example ユーザーの名前とリポジトリ数を取得したい

Slide 19

Slide 19 text

REST Request GET https://api.github.com/users/${ username }

Slide 20

Slide 20 text

REST Response { "login": "kawamataryo", "id": 11070996, "node_id": "MDQ6VXNlcjExMDcwOTk2", "avatar_url": "https://avatars1.githubusercontent.com/u/11070996?v=4", "gravatar_id": "", "public_repos": 95, "public_gists": 0, "followers": 8, "following": 7, "url": "https://api.github.com/users/kawamataryo", "html_url": "https://github.com/kawamataryo", "followers_url": "https://api.github.com/users/kawamataryo/followers", "following_url": "https://api.github.com/users/kawamataryo/following{/other_user}", "gists_url": "https://api.github.com/users/kawamataryo/gists{/gist_id}", . . 以下略

Slide 21

Slide 21 text

REST Response { "login": "kawamataryo", "id": 11070996, "node_id": "MDQ6VXNlcjExMDcwOTk2", "avatar_url": "https://avatars1.githubusercontent.com/u/11070996?v=4", "gravatar_id": "", "public_repos": 95, "public_gists": 0, "followers": 8, "following": 7, "url": "https://api.github.com/users/kawamataryo", "html_url": "https://github.com/kawamataryo", "followers_url": "https://api.github.com/users/kawamataryo/followers", "following_url": "https://api.github.com/users/kawamataryo/following{/other_user}", "gists_url": "https://api.github.com/users/kawamataryo/gists{/gist_id}", . . 以下略 大量の不要なカラムも 取得してしまう

Slide 22

Slide 22 text

{ user(login: “username”) { name repositories { totalCount } } } GraphQL Request POST https://api.github.com/graphql

Slide 23

Slide 23 text

{ "data": { "user": { "name": "ryo", "repositories": { "totalCount": 106 } } } } GraphQL Respose

Slide 24

Slide 24 text

{ "data": { "user": { "name": "ryo", "repositories": { "totalCount": 106 } } } } GraphQL Respose 必要なリソースのみ 宣言的に取得出来る

Slide 25

Slide 25 text

過小な取得 必要な情報によっては 複数回のリクエストを組み合わ せる必要がある。 (関連まで取りたい場合など)

Slide 26

Slide 26 text

example あるユーザーのフォロワー数と、 そのフォロワーのフォロワー数を取得したい

Slide 27

Slide 27 text

REST GET https://api.github.com/users/${ username }/followers でフォロワーを取得 さらにフォロワーの数だけ、同じリクエストを投げ て、全ての結果をカウントする。

Slide 28

Slide 28 text

REST GET https://api.github.com/users/${ username }/followers でフォロワーを取得 さらにフォロワーの数だけ、同じリクエストを投げ て、全ての結果をカウントする。 複数回のリクエストの 統合が必要

Slide 29

Slide 29 text

{ user(login: “username”) { followers { totalCount nodes { name followers { totalCount } } } } } GraphQL Request

Slide 30

Slide 30 text

{ user(login: “username”) { followers { totalCount nodes { name followers { totalCount } } } } } GraphQL Request 一つのリクエストで 関連のデータまでまとめて 取得出来る

Slide 31

Slide 31 text

つまり

Slide 32

Slide 32 text

GraphQL 最高便利..!!!

Slide 33

Slide 33 text

でも..

Slide 34

Slide 34 text

サーバーサイドの実装大変そう、、 自由に取れるってセキュリティ大丈夫?? そもそもAPI変えるほどの工数ない

Slide 35

Slide 35 text

大丈夫!!!

Slide 36

Slide 36 text

Hasura がある!!

Slide 37

Slide 37 text

Hasuraとは?

Slide 38

Slide 38 text

PostgreSQLのTableスキーマから GraphQLのAPIを自動生成するもの

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

DEMO

Slide 41

Slide 41 text

Hasuraの良いところ

Slide 42

Slide 42 text

CRUD・Subscription のクエリを自動で提供してくれる Pross

Slide 43

Slide 43 text

● ネストした関連データの取得 ● 集計処理(count, sum, avg, max, min) ● フィルター処理(eql, pattern match) ● 並び替え ● ページネーション ● トランザクションでの更新 ● カスタムSQL関数を含むクエリ ...etc

Slide 44

Slide 44 text

テーブルのカラム単位で 操作の認可を設定できる Pross

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Firebase, Auth0

Slide 47

Slide 47 text

Event Trigger・Remote Schema で柔軟なAPIも構築可能 Pross

Slide 48

Slide 48 text

Event Trigger 指定されたテーブルのイベントをトリガーに Webhookを呼び出してカスタムロジックを実行する機能

Slide 49

Slide 49 text

Remote Schema 独自に実装した GraphQLサーバーを Hasuraのエンドポイ ントにマージして利用 可能にする機能

Slide 50

Slide 50 text

Hasuraを使う上での注意

Slide 51

Slide 51 text

クエリに対するValidationが まぁまぁきつい Cons

Slide 52

Slide 52 text

PostgreSQLのTriggerで実行 or 別にGraphQL サーバーを立てて Remote Schema実行

Slide 53

Slide 53 text

まとめ

Slide 54

Slide 54 text

Hasuraは便利なものの 全てを解決すことはできない。 ただ、他サービスと上手く連携し作り上げられ れば、GraphQL APIサーバー構築の工数を大 幅に下げられる可能性はある

Slide 55

Slide 55 text

参考

Slide 56

Slide 56 text

O`REILLY 初めてのGraphQL ~ WEBサービスを作って学ぶ次世代API~ 定価 2,860 円