Top Banner
© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. 3-H1-3-16 + AppSync Keisuke Tsukagoshi Mobile Specialist SA, Amazon Web Services Japan
56

+ AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Sep 01, 2019

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

3-H1-3-16

���� + AppSync�������� ���

Keisuke Tsukagoshi

Mobile Specialist SA, Amazon Web Services Japan

Page 2: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

About Me

���� (Tsukagoshi Keisuke)Specialist Solution ArchitectMobile / Serverless / DevOpsBackground

Web������������� ������

������

AWS AppSync

Page 3: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Overview

c GE E R Q Aa A G

I G IG

S T Q LPR I We G

Page 4: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

GraphQL���

Page 5: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

GraphQL ?

• GraphQL��API +���!-.����TypeSystem�$+����!�(,����#�#%� "�����

• GraphQL��� ��"���#�#���#��&)�'*�0/����� ����#�-.��

Page 6: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

� GraphQL�>C����� -REST API MB�LO-

• REST API�MBH�4AHN�LO• API 0=�$�*)-#E@�76• API�5�<�@K���76• API �$�*)-#�8J����� -�

• �+��-#MBH���/?• P(."�ID���13� API�5���������• �����'-#$,&-�2�����!.%.��;G�F9Request / Response�:����

Page 7: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

GraphQL���(� ��!��

• � ��"�,�#�#1��"�#��#���!#"���

• 0&�#�#����'+� �• API����"��%)�/�*1�$-���• API�,.����/�*1�'+� �

Page 8: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

��GraphQL������� – GraphQL� �-

•P )

A• A )

B A C• A I B A P

)

Page 9: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

��GraphQL������� – GraphQL� �-

•P )

A• A )

B A C• A I B A P

)

Page 10: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

type Todo {

id: ID!name: Stringdescription: Stringstatus: TodoStatus

}

type Query {getTodos: [Todo]

}

enum TodoStatus { done pending

}

I II D

!

Page 11: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

)

Page 12: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

)

Page 13: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

-

•P )

A• A )

B A C• A I B A P

)

Page 14: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 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: Stringdescription: Stringpriority: Intduedate: String

}

query {getTodos {

idnamepriority

}}

��������������

�������� ��� �����

�������������!���

Page 15: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

)� ������� ������

!= Graph databaseNoSQL, Relational, HTTP, etc.

��� ���� GraphQL/posts/postInfo/postJustTitle/postsByAuthor

/postNameStartsWithX/commentsOnPost

Page 16: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

-

•P )

A• A )

B A C• A I B A P

)

Page 17: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

) )��������� � &%

Mutation ��� �������� �� �- �� ��������� �� � "!��$#

mutation addPost( id:123title:”New post!”author:”Nadia”){idtitleauthor

}

data: [{id:123,title:”New Post!”author:”Nadia”

}]

Page 18: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 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 {

__typenameversiontitlecontentauthorurl

}}

Page 19: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

) )

Subscription NewPostSub {addedPost{…}

}

WebSocket URL and Connection Payload

Secure Websocket Connection (wss://)

Page 20: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

const AllPostsWithData = compose(graphql(AllPostsQuery, { options: { fetchPolicy: 'cache-and-network‘ },

props: (props) => ({posts: props.data.posts,subscribeToNewPosts: params => {

props.data.subscribeToMore({document: NewPostsSubscription,updateQuery: (prev, { subscriptionData: { newPost } }) => ({

...prev,posts: [newPost, ...prev.posts.filter(post => post.id !== newPost.id)]

})});

});…//more code

)

Page 21: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Demo: ��������

Page 22: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

AppSync���

Page 23: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

AWS AppSync��

������� GraphQL ������ GraphQL���������

Page 24: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

6="�="

• 9�:%�2• LI�ED���('��&( 50=-

• �9�:%�2�+=%�KI

• �80;= 7<• TH6=�=�@CQV�S��/9�= 7<

• -�53<,�N?�*�",3(#=!������<*<)%�/�RBKI

• $= 4:3+��• $= 4:3+���'4(,

• TH6=�=U��3(#=!<�OM��1=,

• �.8�<J���/9�= 7<�G>���AFPJ�RB Sync

Page 25: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

AppSync��(���

• AWS App Sync Client : 10���#�('������+��Client• Data Source : DynamoDB / Elasticsearch / Lambda• Identity : GraphQL Proxy ��$�����10• GraphQL Proxy : $�����!��(���(�$����(�$(�� ����(�')%

• Operation : Query / Mutation / Subscription �� GraphQL���&)�"(• Action : GraphQL� Subscriber ��3.• Resolver : $���� / &� (��*-�/2�4,

Page 26: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

AppSync Overview

AWS AppSync

AmazonDynamoDB

AWSLambda

Elasticsearch

subscriptions

/graphql

Resolvers

DataSources

Page 27: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

dynamoDB�AmazonES

AmazonDynamoDB

ElasticSearch

/addPost

/searchPosts

������ #�������&���"����$�������"������"�� !%�"�e.t.c

Page 28: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Lambda3rdPartyAPI

/searchPosts��api

Lambda�DataSource������ ���������WebAPI�������

Page 29: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

��������

!���������"��#�

� ���� �����

Page 30: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

�����������

������

����������

�������

Page 31: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

�����������

������

����������

�������

Page 32: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

������

AWS AppSync

AmazonDynamoDB

AWSLambda

Elasticsearch

subscriptions

/graphql

Resolvers

DataSources

Page 33: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

�� �$)

• �� ��� ��'*�+,������&#�����"%����!(�����

• GraphQL API �1�� GraphQL�� � $)���SDL(Schema Definition Language)���+,���

Page 34: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

�������

schema {query:Querymutation: Mutationsubscription: Subscription

}

��������

Query : �Mutation : � Subscription : ��

Page 35: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

���!�

type Query {getTodos: [Todo]

}

type Todo {id: ID!name: Stringdescription: Stringstatus: TodoStatus

}

enum TodoStatus { done pending

}

��������� ����������#�%

Not Null���$�&"ID!

����'����&"

[String!]

Page 36: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

��"��������

�����

���������

��� ��!�

Resolver Mapping Template������

Page 37: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

AppSync Overview

AWS AppSync

AmazonDynamoDB

AWSLambda

Elasticsearch

subscriptions

/graphql

Resolvers

DataSources

Page 38: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

�!�%���$��$�"%���2

• ���$��$�"%���GraphQL �����%��%��'&�(+�,-���%��%����*.GraphQL"��$��(+�,-)/�

• Apache Velocity Template Language0VTL1• �#���$� ��

• https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-programming-guide.html

Page 39: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

)�* .%�!-��-#+.������/• ����-�,.*• :> ��&�"�*�0• 12 )�.�(-�"�.%��• �.�58�97• )���%�#36• '.�.ID4���+�$-�"�*�)-�/5;

• =@�<?����

Page 40: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

�������������

• ���������• ����������

Page 41: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

� : ������� �� ���

{

"version" : "2017-02-28",

"operation" : "GetItem",

"key" : {

"id" : { "S" : "${context.arguments.id}" }

}

}

Page 42: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 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}"

}

����������

����������

Page 43: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

����������

Page 44: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

DynamoDB��������

Page 45: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

�����������

������

����������

�������

Page 46: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

AppSync Overview

AWS AppSync

AmazonDynamoDB

AWSLambda

Elasticsearch

subscriptions

/graphql

Resolvers

DataSources

Page 47: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 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": ”***"

}

Page 48: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Cl ient�� 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/

Page 49: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Cl ient���//API Keyconst client = new AWSAppSyncClient({

url: awsconfig.ENDPOINT, region: awsconfig.REGION,auth: { type: AUTH_TYPE.API_KEY,

apiKey: awsconfig.apiKey}});

Page 50: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Cl ient���//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}

Page 51: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Demo: GraphQL��������

Page 52: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

���������

• AWS AppSync GraphQL Photo Sample• https://github.com/aws-samples/aws-amplify-graphql

• GraphQL starter application• https://github.com/aws-samples/aws-mobile-appsync-

events-starter-react

Page 53: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

AWS AppSync GraphQL Photo Sample

Page 54: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

���

•P )

A• A )

B A C• A I B A P

)

Page 55: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

Happy coding with AppSync!

AWS AppSync

AmazonDynamoDB

AWSLambda

Elasticsearch

subscriptions

/graphql

Resolvers

DataSources

AWS AppSync ������ ������GraphQL API �����������

Page 56: + AppSync · © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved. © 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

© 2018, Amazon Web Services, Inc. or its affiliates. All rights reserved.

k a R Q Sb Q c

c F

3 b A Q S