© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL 入門 (AWS AppSync)
AWS CREATIVE STUDIO | 2018
Amazon Web Services Japan K.K.
Solution Architect, Strategic
Atsuya Nunomura
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Who am I ?
布村 純也(ぬのむら あつや)
Amazon Web Services Japan K.K.
Solution Architect, Strategic
Background
Web/Mobile アプリケーション・API開発,
Web Service開発、Apollo好き
好きなサービス
AppSync, ECS, Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Overview
様々なコミュニティで「GraphQL vs REST」という形で紹介され
REST APIの次のパラダイムとして注目を集めているGraphQL
今日は・・・
GraphQLの概要、アーキテクチャ、RESTとの比較、利用シーンに
ついてのお話と、AWS AppSync を利用する事で簡単に
GraphQLを使い始める事が出来るというお話
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL 概要
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL !?
ー TL ; DR ー
■ API用のQuery言語(A query language for your API)
■ TypeSystemを使用してQueryを実行する為のサーバー側ランタイム
■ クライアントがサーバーからデータを取得、変更、購読
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
登場の背景
Web (HTML)
RDB
RESTful API
RDB
No
SQL
GraphQL
RDB
No
SQL
API
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
なぜ登場した!?
■ REST API開発者、利用者の課題
・API仕様のドキュメント管理が大変
・APIの叩き方を理解するのが大変
・APIのドキュメントと実装がズレてケンカ
■ クライアント開発者からの不満
・1ページ表示するのに幾つもAPIを叩かないといけない
・折角イベントドリブンに作ってもサーバーとの接続は結局
Request / Responseの形が残る
(T_T)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQLによって得られるメリット
クライアント/サーバー間のインタフェースが
クリーンになる
通信オーバーヘッドが削減される
APIドキュメント作成に費やす時間が不要になる
APIを理解するのに費やす時間が削減される
(^ ^)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
なぜGraphQLが注目されているか? - GraphQLの特徴 -
1.型指定されたスキーマ
2.クライアントからのレスポンス形式の指定
3.サブスクリプションを利用したリアルタイム処理
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
なぜGraphQLが注目されているか? - GraphQLの特徴 -
1.型指定されたスキーマ
APIドキュメントを主導で記述する必要が無くなり、
APIを定義したスキーマをベースに自動生成
2.クライアントからのレスポンス形式の指定
3.サブスクリプションを利用したリアルタイム処理
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
スキーマ定義
type Query {
getTodos: [Todo]
}
type Todo {
id: ID!
name: String
description: String
priority: Int
duedate: String
}
スカラー型、オブジェクト型、
列挙型などを利用可能
Not Nullは感嘆符で表現
ID!
リストは角カッコで表現
[String!]
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
ドキュメント自動生成
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Query実行環境
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
なぜGraphQLが注目されているか? - GraphQLの特徴 -
1.型指定されたスキーマ
2.クライアントからのレスポンス形式の指定
・オーバーフェッチ、アンダーフェッチが無くなる
・クリーンなインタフェース
3.サブスクリプションを利用したリアルタイム処理
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
クライアントがレスポンス形式を指定
{
"id": "1",
"name": "Get Milk",
“priority": "1"
},
{
"id": “2",
"name": “Go to gym",
“priority": “5"
},…
type Query {
getTodos: [Todo]
}
type Todo {
id: ID!
name: String
description: String
priority: Int
duedate: String
}
query {
getTodos {
id
name
priority
}
}
アプリのスキーマと
モデルデータ
クライアントが必要な
ものだけをリクエスト
リクエストしたデータ
だけが返される
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
クリーンなインタフェース
オープンで宣言的なデータ取得の仕様
従来のデータフェッチ GraphQL
/posts
/postInfo
/postJustTitle
/postsByAuthor
/postNameStartsWithX
/commentsOnPost
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
なぜGraphQLが注目されているか? - GraphQLの特徴 -
1.型指定されたスキーマ
2.クライアントからのレスポンス形式の指定
3.サブスクリプションを利用したリアルタイム処理
クライアントはデータをサブスクライブする事で
イベント・ドリブンに処理を実装可能
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQLの処理形態
Query(取得) Subscription(購読)
Mutation(変更)
Mutation
Subscription
“Event !!”
ここのお話
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Subscription
ほぼリアルタイムでのデータ購読
Mutationをトリガーとしたイベントベースモード
mutation addPost( id:123
title: "New post!"
author: "Nadia"){
id
title
author
}
data: [{
id:123,
title:"New Post!"
author:"Nadia"
}]
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
スキーマ定義構成
type Subscription {
addedPost: Post
@aws_subscribe(mutations: ["addPost"])
deletedPost: Post
@aws_subscribe(mutations: ["deletePost"])
}
type Mutation {
addPost(id: ID! author: String! title:
String content: String): Post!
deletePost(id: ID!): Post!
}
subscription NewPostSub {
addedPost {
__typename
version
title
content
author
url
}
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Subscription ハンドシェイク
Subscription NewPostSub {
addedPost{…}
}
WebSocket URL and Connection Payload
Secure Websocket Connection (wss://)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS AppSync
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS AppSyncとは!?
フルマネージド GraphQL サービス
すぐに GraphQL の利用を始められます
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Developerの課題解決
リアルタイムコ
ラボレーション
同期を考慮し
たオフラインプ
ログラミング
必要なデータ
のみの取得
複数のデータ
ソースへの
アクセス
アクセス制御
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Usecase
■ リアルタイム
・最新の情報をウォッチするダッシュボード
・ほぼリアルタイムでデータを更新
■ コラボレーション
・複数ユーザーが共同編集を行うアプリケーション
・ドキュメント、画像、テキストメッセージ等、様々な
コンテンツタイプを自動更新
■ ソーシャルメディア
・ソーシャルメディアやチャット
・複数ユーザー間でのメッセージング管理をサポート
・オフライン時でもアプリケーションを操作でき、
再接続時に自動 Sync
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AppSyncのコンセプト
• AWS App Sync Client : 認証、オフラインロジックなどを含んだClient
• Data Source : DynamoDB / Elasticsearch / Lambda / HTTP Endpoint
• Identity : GraphQL Proxy へのリクエストの認証
• GraphQL Proxy : リクエストのマッピング、コンフリクトのハンドリング、
アクセスコントロール
• Operation : Query / Mutation / Subscription など GraphQL のオペレーション
• Action : GraphQL から Subscriber への通知
• Resolver : リクエスト / レスポンスの処理を記述する関数
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AppSync Overview
AWS AppSync
Amazon
DynamoDB
AWS
Lambda
ElasticSearch
subscriptions
/graphql
Resolvers
DataSources
HTTP Endpoinmt
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
DynamoDBとAmazonES
Amazon
DynamoDB
ElasticSearch
/addPost
/searchPosts
データソースを組み合わせることで高度な検索にも対応可能。
キーワード検索、ファジー検索、地理空間検索…
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda, HTTP Endpointと3rd Party API
/searchPosts
LambdaをDataSourceとして扱えるため、なんでもできる
外部のWebAPIを叩くことも可能
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
スケーラビリティ
AppSyncバックグラウンドの
Resolver,DataSourceの組合せは自由
開発における拡張性が非常に高く
迅速なプロトタイピングが可能
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
迅速なプロトタイピング
スキーマ
定義
DataSource
との接続
クライアント
の設定
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
迅速なプロトタイピング
スキーマ
定義
DataSource
との接続
クライアント
の設定
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AppSync Overview
AWS AppSync
Amazon
DynamoDB
AWS
Lambda
ElasticSearch
subscriptions
/graphql
Resolvers
DataSources
HTTP Endpoinmt
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
スキーマ定義
■ スキーマはサーバの機能を記述し、クエリが有効かどうかを判
断する為に使用されます。
■ GraphQL API は1つの GraphQL スキーマで定義され、
SDL(Schema Definition Language)によって記述さる。
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
スキーマの書き方
schema {
query:Query
mutation: Mutation
subscription: Subscription
}
ルートスキーマを定義
Subscription(購読)
Mutation(更新)
Query(取得)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
タイプの書き方
type Query {
getTodos: [Todo]
}
type Todo {
id: ID!
name: String
description: String
status: TodoStatus
}
enum TodoStatus {
done
pending
}
スカラー型、オブジェクト型、
列挙型などを利用可能
Not Nullは感嘆符で表現
ID!
リストは角カッコで表現
[String!]
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
迅速なプロトタイピング
スキーマ
定義
DataSource
との接続
クライアント
の設定
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AppSync Overview
AWS AppSync
Amazon
DynamoDB
AWS
Lambda
ElasticSearch
subscriptions
/graphql
Resolvers
DataSources
HTTP Endpoinmt
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
リゾルバーマッピングテンプレートとは!?
• マッピングテンプレートは、GraphQLリクエストをデータソースの
命令に変換する方法と、データソースからの応答をGraphQLレスポ
ンスに変換する方法を定義する
• Apache Velocity Template Language(VTL)
• プログラミングガイド
https://docs.aws.amazon.com/appsync/latest/devguide/resolver-
mapping-template-reference-programming-guide.html
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
リゾルバーマッピングテンプレートで実施する例
• アクセスコントロール
• 新規アイテムのデフォルト値
• 入力のバリデーション、フォーマット
• データの変換と整形
• リスト、マップの加工
• ユーザーIDに基づいたレスポンスのフィルタリング/変更
• 複雑な権限チェック
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
2種類のマッピングテンプレート
• リクエストテンプレート (Request  DataSource命令)
• レスポンステンプレート (DataSource応答  GraphQL Response)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
リクエストマッピング テンプレート(例)
{
"version" : "2017-02-28",
"operation" : "GetItem",
"key" : {
"id" : { "S" : "${context.arguments.id}" }
}
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
レスポンスマッピング テンプレート(例)
■ デフォルトで返す場合:
■ データを結合する場合:
$utils.toJson($context.result)
{
"id" : ${context.data.id},
"title" : "${context.data.theTitle}",
"content" : "${context.data.body1} ${context.data.body2}"
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
VTLを直接記述(自動補完)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
No-code GraphQL API Builderを利用
・スキーマ内の定義済みのTypeから
DynamoDB テーブルをプロビジョニング
・リゾルバを利用してフィルタ、検索、
比較などを簡単に組み込み
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
スキーマからジェネレート
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
DynamoDBからジェネレート
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
迅速なプロトタイピング
スキーマ
定義
DataSource
との接続
クライアント
の設定
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AppSync Overview
AWS AppSync
Amazon
DynamoDB
AWS
Lambda
ElasticSearch
subscriptions
/graphql
Resolvers
DataSources
HTTP Endpoinmt
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Endpointへの接続情報
export default {
"graphqlEndpoint": "https://**.appsync-api.**.amazonaws.com/graphql",
"region": "us-east-1",
"authenticationType": ”API_KEY",
"apiKey": ”***"
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Clientの設定
const client = new AWSAppSyncClient({
url: awsconfig.ENDPOINT,
region: AWS.config.region,
auth: { type: AUTH_TYPE.AWS_IAM, credentials: Auth.currentCredentials() }
});
const WithProvider = () => (
<ApolloProvider client={client}>
<Rehydrated>
<AppWithData />
</Rehydrated>
</ApolloProvider>
);
自動でオフライン利用が可能に
https://aws.github.io/aws-amplify/
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Clientの認証
//API Key
const client = new AWSAppSyncClient({
url: awsconfig.ENDPOINT,
region: awsconfig.REGION,
auth: { type: AUTH_TYPE.API_KEY, apiKey: awsconfig.apiKey}
});
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Clientの認証
//IAM認証
auth: { type: AUTH_TYPE.AWS_IAM,
credentials: Auth.currentCredentials()
}
//Cognito User Pool 認証
auth: { type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS,
jwtToken: Auth.currentSession().accessToke.jwtToken
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
纏め
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
1.型指定されたスキーマ
APIドキュメントを主導で記述する必要が無くなり、
APIを定義したスキーマをベースに自動生成
2.クライアントからのレスポンス形式の指定
・オーバーフェッチ、アンダーフェッチが無くなる
・クリーンなインタフェース
3.サブスクリプションを利用したリアルタイム処理
クライアントはデータをサブスクライブする事でイベント・
ドリブンに処理を実装可能
纏め
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Happy coding with AppSync
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

GraphQL入門 (AWS AppSync)

  • 1.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. GraphQL 入門 (AWS AppSync) AWS CREATIVE STUDIO | 2018 Amazon Web Services Japan K.K. Solution Architect, Strategic Atsuya Nunomura
  • 2.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Who am I ? 布村 純也(ぬのむら あつや) Amazon Web Services Japan K.K. Solution Architect, Strategic Background Web/Mobile アプリケーション・API開発, Web Service開発、Apollo好き 好きなサービス AppSync, ECS, Lambda
  • 3.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Overview 様々なコミュニティで「GraphQL vs REST」という形で紹介され REST APIの次のパラダイムとして注目を集めているGraphQL 今日は・・・ GraphQLの概要、アーキテクチャ、RESTとの比較、利用シーンに ついてのお話と、AWS AppSync を利用する事で簡単に GraphQLを使い始める事が出来るというお話
  • 4.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. GraphQL 概要
  • 5.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. GraphQL !? ー TL ; DR ー ■ API用のQuery言語(A query language for your API) ■ TypeSystemを使用してQueryを実行する為のサーバー側ランタイム ■ クライアントがサーバーからデータを取得、変更、購読
  • 6.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 登場の背景 Web (HTML) RDB RESTful API RDB No SQL GraphQL RDB No SQL API
  • 7.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. なぜ登場した!? ■ REST API開発者、利用者の課題 ・API仕様のドキュメント管理が大変 ・APIの叩き方を理解するのが大変 ・APIのドキュメントと実装がズレてケンカ ■ クライアント開発者からの不満 ・1ページ表示するのに幾つもAPIを叩かないといけない ・折角イベントドリブンに作ってもサーバーとの接続は結局 Request / Responseの形が残る (T_T)
  • 8.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. GraphQLによって得られるメリット クライアント/サーバー間のインタフェースが クリーンになる 通信オーバーヘッドが削減される APIドキュメント作成に費やす時間が不要になる APIを理解するのに費やす時間が削減される (^ ^)
  • 9.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. なぜGraphQLが注目されているか? - GraphQLの特徴 - 1.型指定されたスキーマ 2.クライアントからのレスポンス形式の指定 3.サブスクリプションを利用したリアルタイム処理
  • 10.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. なぜGraphQLが注目されているか? - GraphQLの特徴 - 1.型指定されたスキーマ APIドキュメントを主導で記述する必要が無くなり、 APIを定義したスキーマをベースに自動生成 2.クライアントからのレスポンス形式の指定 3.サブスクリプションを利用したリアルタイム処理
  • 11.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. スキーマ定義 type Query { getTodos: [Todo] } type Todo { id: ID! name: String description: String priority: Int duedate: String } スカラー型、オブジェクト型、 列挙型などを利用可能 Not Nullは感嘆符で表現 ID! リストは角カッコで表現 [String!]
  • 12.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. ドキュメント自動生成
  • 13.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Query実行環境
  • 14.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. なぜGraphQLが注目されているか? - GraphQLの特徴 - 1.型指定されたスキーマ 2.クライアントからのレスポンス形式の指定 ・オーバーフェッチ、アンダーフェッチが無くなる ・クリーンなインタフェース 3.サブスクリプションを利用したリアルタイム処理
  • 15.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. クライアントがレスポンス形式を指定 { "id": "1", "name": "Get Milk", “priority": "1" }, { "id": “2", "name": “Go to gym", “priority": “5" },… type Query { getTodos: [Todo] } type Todo { id: ID! name: String description: String priority: Int duedate: String } query { getTodos { id name priority } } アプリのスキーマと モデルデータ クライアントが必要な ものだけをリクエスト リクエストしたデータ だけが返される
  • 16.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. クリーンなインタフェース オープンで宣言的なデータ取得の仕様 従来のデータフェッチ GraphQL /posts /postInfo /postJustTitle /postsByAuthor /postNameStartsWithX /commentsOnPost
  • 17.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. なぜGraphQLが注目されているか? - GraphQLの特徴 - 1.型指定されたスキーマ 2.クライアントからのレスポンス形式の指定 3.サブスクリプションを利用したリアルタイム処理 クライアントはデータをサブスクライブする事で イベント・ドリブンに処理を実装可能
  • 18.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. GraphQLの処理形態 Query(取得) Subscription(購読) Mutation(変更) Mutation Subscription “Event !!” ここのお話
  • 19.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. GraphQL Subscription ほぼリアルタイムでのデータ購読 Mutationをトリガーとしたイベントベースモード mutation addPost( id:123 title: "New post!" author: "Nadia"){ id title author } data: [{ id:123, title:"New Post!" author:"Nadia" }]
  • 20.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. スキーマ定義構成 type Subscription { addedPost: Post @aws_subscribe(mutations: ["addPost"]) deletedPost: Post @aws_subscribe(mutations: ["deletePost"]) } type Mutation { addPost(id: ID! author: String! title: String content: String): Post! deletePost(id: ID!): Post! } subscription NewPostSub { addedPost { __typename version title content author url } }
  • 21.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. GraphQL Subscription ハンドシェイク Subscription NewPostSub { addedPost{…} } WebSocket URL and Connection Payload Secure Websocket Connection (wss://)
  • 22.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AWS AppSync
  • 23.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AWS AppSyncとは!? フルマネージド GraphQL サービス すぐに GraphQL の利用を始められます
  • 24.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Developerの課題解決 リアルタイムコ ラボレーション 同期を考慮し たオフラインプ ログラミング 必要なデータ のみの取得 複数のデータ ソースへの アクセス アクセス制御
  • 25.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Usecase ■ リアルタイム ・最新の情報をウォッチするダッシュボード ・ほぼリアルタイムでデータを更新 ■ コラボレーション ・複数ユーザーが共同編集を行うアプリケーション ・ドキュメント、画像、テキストメッセージ等、様々な コンテンツタイプを自動更新 ■ ソーシャルメディア ・ソーシャルメディアやチャット ・複数ユーザー間でのメッセージング管理をサポート ・オフライン時でもアプリケーションを操作でき、 再接続時に自動 Sync
  • 26.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AppSyncのコンセプト • AWS App Sync Client : 認証、オフラインロジックなどを含んだClient • Data Source : DynamoDB / Elasticsearch / Lambda / HTTP Endpoint • Identity : GraphQL Proxy へのリクエストの認証 • GraphQL Proxy : リクエストのマッピング、コンフリクトのハンドリング、 アクセスコントロール • Operation : Query / Mutation / Subscription など GraphQL のオペレーション • Action : GraphQL から Subscriber への通知 • Resolver : リクエスト / レスポンスの処理を記述する関数
  • 27.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AppSync Overview AWS AppSync Amazon DynamoDB AWS Lambda ElasticSearch subscriptions /graphql Resolvers DataSources HTTP Endpoinmt
  • 28.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. DynamoDBとAmazonES Amazon DynamoDB ElasticSearch /addPost /searchPosts データソースを組み合わせることで高度な検索にも対応可能。 キーワード検索、ファジー検索、地理空間検索…
  • 29.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Lambda, HTTP Endpointと3rd Party API /searchPosts LambdaをDataSourceとして扱えるため、なんでもできる 外部のWebAPIを叩くことも可能
  • 30.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. スケーラビリティ AppSyncバックグラウンドの Resolver,DataSourceの組合せは自由 開発における拡張性が非常に高く 迅速なプロトタイピングが可能
  • 31.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 迅速なプロトタイピング スキーマ 定義 DataSource との接続 クライアント の設定
  • 32.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 迅速なプロトタイピング スキーマ 定義 DataSource との接続 クライアント の設定
  • 33.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AppSync Overview AWS AppSync Amazon DynamoDB AWS Lambda ElasticSearch subscriptions /graphql Resolvers DataSources HTTP Endpoinmt
  • 34.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. スキーマ定義 ■ スキーマはサーバの機能を記述し、クエリが有効かどうかを判 断する為に使用されます。 ■ GraphQL API は1つの GraphQL スキーマで定義され、 SDL(Schema Definition Language)によって記述さる。
  • 35.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. スキーマの書き方 schema { query:Query mutation: Mutation subscription: Subscription } ルートスキーマを定義 Subscription(購読) Mutation(更新) Query(取得)
  • 36.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. タイプの書き方 type Query { getTodos: [Todo] } type Todo { id: ID! name: String description: String status: TodoStatus } enum TodoStatus { done pending } スカラー型、オブジェクト型、 列挙型などを利用可能 Not Nullは感嘆符で表現 ID! リストは角カッコで表現 [String!]
  • 37.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 迅速なプロトタイピング スキーマ 定義 DataSource との接続 クライアント の設定
  • 38.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AppSync Overview AWS AppSync Amazon DynamoDB AWS Lambda ElasticSearch subscriptions /graphql Resolvers DataSources HTTP Endpoinmt
  • 39.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. リゾルバーマッピングテンプレートとは!? • マッピングテンプレートは、GraphQLリクエストをデータソースの 命令に変換する方法と、データソースからの応答をGraphQLレスポ ンスに変換する方法を定義する • Apache Velocity Template Language(VTL) • プログラミングガイド https://docs.aws.amazon.com/appsync/latest/devguide/resolver- mapping-template-reference-programming-guide.html
  • 40.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. リゾルバーマッピングテンプレートで実施する例 • アクセスコントロール • 新規アイテムのデフォルト値 • 入力のバリデーション、フォーマット • データの変換と整形 • リスト、マップの加工 • ユーザーIDに基づいたレスポンスのフィルタリング/変更 • 複雑な権限チェック
  • 41.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 2種類のマッピングテンプレート • リクエストテンプレート (Request  DataSource命令) • レスポンステンプレート (DataSource応答  GraphQL Response)
  • 42.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. リクエストマッピング テンプレート(例) { "version" : "2017-02-28", "operation" : "GetItem", "key" : { "id" : { "S" : "${context.arguments.id}" } } }
  • 43.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. レスポンスマッピング テンプレート(例) ■ デフォルトで返す場合: ■ データを結合する場合: $utils.toJson($context.result) { "id" : ${context.data.id}, "title" : "${context.data.theTitle}", "content" : "${context.data.body1} ${context.data.body2}" }
  • 44.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. VTLを直接記述(自動補完)
  • 45.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. No-code GraphQL API Builderを利用 ・スキーマ内の定義済みのTypeから DynamoDB テーブルをプロビジョニング ・リゾルバを利用してフィルタ、検索、 比較などを簡単に組み込み
  • 46.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. スキーマからジェネレート
  • 47.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. DynamoDBからジェネレート
  • 48.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 迅速なプロトタイピング スキーマ 定義 DataSource との接続 クライアント の設定
  • 49.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AppSync Overview AWS AppSync Amazon DynamoDB AWS Lambda ElasticSearch subscriptions /graphql Resolvers DataSources HTTP Endpoinmt
  • 50.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. GraphQL Endpointへの接続情報 export default { "graphqlEndpoint": "https://**.appsync-api.**.amazonaws.com/graphql", "region": "us-east-1", "authenticationType": ”API_KEY", "apiKey": ”***" }
  • 51.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Clientの設定 const client = new AWSAppSyncClient({ url: awsconfig.ENDPOINT, region: AWS.config.region, auth: { type: AUTH_TYPE.AWS_IAM, credentials: Auth.currentCredentials() } }); const WithProvider = () => ( <ApolloProvider client={client}> <Rehydrated> <AppWithData /> </Rehydrated> </ApolloProvider> ); 自動でオフライン利用が可能に https://aws.github.io/aws-amplify/
  • 52.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Clientの認証 //API Key const client = new AWSAppSyncClient({ url: awsconfig.ENDPOINT, region: awsconfig.REGION, auth: { type: AUTH_TYPE.API_KEY, apiKey: awsconfig.apiKey} });
  • 53.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Clientの認証 //IAM認証 auth: { type: AUTH_TYPE.AWS_IAM, credentials: Auth.currentCredentials() } //Cognito User Pool 認証 auth: { type: AUTH_TYPE.AMAZON_COGNITO_USER_POOLS, jwtToken: Auth.currentSession().accessToke.jwtToken }
  • 54.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 纏め
  • 55.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 1.型指定されたスキーマ APIドキュメントを主導で記述する必要が無くなり、 APIを定義したスキーマをベースに自動生成 2.クライアントからのレスポンス形式の指定 ・オーバーフェッチ、アンダーフェッチが無くなる ・クリーンなインタフェース 3.サブスクリプションを利用したリアルタイム処理 クライアントはデータをサブスクライブする事でイベント・ ドリブンに処理を実装可能 纏め
  • 56.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Happy coding with AppSync
  • 57.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.