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

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

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

© 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

© 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

© 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

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

GraphQL���

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

GraphQL ?

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

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

© 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�:����

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

GraphQL���(� ��!��

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

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

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

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

•P )

A• A )

B A C• A I B A P

)

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

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

•P )

A• A )

B A C• A I B A P

)

© 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

!

© 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

)

© 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

}}

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

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

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

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

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

!= Graph databaseNoSQL, Relational, HTTP, etc.

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

/postNameStartsWithX/commentsOnPost

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

-

•P )

A• A )

B A C• A I B A P

)

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

}]

© 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

}}

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

) )

Subscription NewPostSub {addedPost{…}

}

WebSocket URL and Connection Payload

Secure Websocket Connection (wss://)

© 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

)

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

Demo: ��������

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

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.

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

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

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

AppSync Overview

AWS AppSync

AmazonDynamoDB

AWSLambda

Elasticsearch

subscriptions

/graphql

Resolvers

DataSources

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

dynamoDB�AmazonES

AmazonDynamoDB

ElasticSearch

/addPost

/searchPosts

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

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

Lambda3rdPartyAPI

/searchPosts��api

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

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

������

AWS AppSync

AmazonDynamoDB

AWSLambda

Elasticsearch

subscriptions

/graphql

Resolvers

DataSources

© 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:Querymutation: Mutationsubscription: Subscription

}

��������

Query : �Mutation : � Subscription : ��

© 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!]

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

��"��������

�����

���������

��� ��!�

Resolver Mapping Template������

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

AppSync Overview

AWS AppSync

AmazonDynamoDB

AWSLambda

Elasticsearch

subscriptions

/graphql

Resolvers

DataSources

© 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

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

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

• =@�<?����

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

}

}

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

����������

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

DynamoDB��������

© 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

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

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/

© 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}});

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

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

Demo: GraphQL��������

© 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

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

AWS AppSync GraphQL Photo Sample

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

���

•P )

A• A )

B A C• A I B A P

)

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

© 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