Mais conteúdo relacionado Semelhante a Introduzione a GraphQL (20) Mais de Commit University (20) Introduzione a GraphQL1. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Introduzione a GraphQL
AWS AppSync & Amplify
2. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Brief intro to Serverless
3. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What is serverless?
No infrastructure provisioning,
no management
Automatic scaling
Pay for value Highly available and secure
4. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS
Lambda
AWS
Fargate
Amazon
API Gateway
Amazon
SNS
Amazon
SQS
AWS
Step Functions
COMPUTE
DATA STORES
INTEGRATION
Amazon Aurora
Serverless
Amazon
S3
Amazon
DynamoDB
AWS
AppSync
5. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL
6. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
/posts /comments /authors
REST API
posts comments authors
GraphQL API
What is GraphQL?
7. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL APIs
Open, declarative data-fetching specification
!= Graph database
Use NoSQL, Relational, HTTP, etc.
Traditional data-fetching GraphQL
/posts
/postInfo
/postJustTitle
/postsByAuthor
/postNameStartsWithX
/commentsOnPost
/posts
8. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Spec - Schema
{
"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
}
}
Model data with
application schema
Client requests what it
needs
Only that data is
returned
9. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL
Int: A signed 32‐bit integer.
Float: A signed double-precision floating-point value.
String: A UTF‐8 character sequence.
Boolean: true or false.
ID: The ID scalar type represents a unique identifier, often
used to refetch an object or as the key for a cache. The ID
type is serialized in the same way as a String; however,
defining it as an ID signifies that it is not intended to be
human‐readable.
Scalar types
GraphQL + AppSync
AWSDate: accepts date strings of the form YYYY-MM-DD.
AWSTime: accepts time strings of the form hh:mm:ss.sss.
AWSDateTime: accepts datetime strings of the form YYYY-MM-
DDThh:mm:ss.sssZ.
AWSTimestamp: represents the number of seconds that have
elapsed since 1970-01-01T00:00Z.
AWSEmail: represents an Email
AWSJSON: represents a JSON string that complies with RFC
8259.
AWSURL: represents a valid URL string.
AWSPhone: represents a valid Phone Number
AWSIPAddress: scalar type represents a valid IPv4 or IPv6
address string.
10. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Spec - Types
Schema
type Event {
id: ID!
name: String
where: String
when: String
description: String
comments: [Comment]
}
type Comment {
commentId: String!
eventId: ID!
content: String!
createdAt: String!
}
11. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Spec - Mutation
Schema
Mutation
type Mutation {
createEvent(
name: String!,
when: String!,
where: String!,
description: String!
): Event
deleteEvent(id: ID!): Event
commentOnEvent(
eventId: ID!,
content: String!,
createdAt: String!
): Comment
}
12. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Spec - Query
Schema
Mutation
Query
type Query {
getEvent(id: ID!): Event
listEvents(
limit: Int,
nextToken: String
): EventConnection
}
13. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Spec - Subscription
Schema
Mutation
Query
Subscription type Subscription {
onComments(eventId: String!): Comment
@aws_subscribe(mutations: ["commentOnEvent"])
}
14. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Spec - Subscription
Schema
Mutation
Query
Subscription type Subscription {
onComments(eventId: String!): Comment
@aws_subscribe(mutations: ["commentOnEvent"])
}
type Mutation {
…
commentOnEvent(
eventId: ID!,
content: String!,
createdAt: String!
): Comment
}
15. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL - Summary
Schema
Mutation
Query
Subscription
Realtime? YES
Batching? YES
Pagination? YES
Relations? YES
Aggregations? YES
Search? YES
Offline? YES
16. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What are the GraphQL benefits?
• Rapid prototyping and iteration
• Co-location of data requirements & application views
o Implementations aren’t encoded in the server
• Data behavior control
o Batching, request/response and real-time
• Bandwidth optimization (N+1 problem)
17. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
I want to run my own GraphQL server!!
18. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS AppSync – Intro
19. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What is AWS AppSync?
AWS AppSync is a managed service for application data
using GraphQL with real-time capabilities and an offline
programming model.
Real-time
Collaboration
Offline Programming
Model with Sync
Flexible Database
Options
Fine-grained
Access Control
20. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS AppSync Benefits
Get many resources from
many data sources with a
single request
Self-documenting APIs with
Introspection
Clients receive the data
they ask for. Nothing
more, nothing less
React Native, Android,
iOS, and Web (JS) using
the Apollo GraphQL client
Data persistence across
application restarts
Write-through mutations with
optimistic UI
21. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
How does AWS AppSync work?
22. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS AppSync and GraphQL Subscription
Near Realtime updates of data
Event based mode, triggered by Mutations
- Scalable model, designed as a platform for common use-cases
Can be used with ANY data source in AppSync
- Lambda, DynamoDB, Elasticsearch
mutation addPost(
id: 123
title: ”New post!”
author: ”Nadia”)
{
id
title
author
}
data: [{
id: 123,
title: ”New Post!”
author: ”Nadia”
}]
Websockets over MQTT
23. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS AppSync – Data Sources, Auth and more
DynamoDB
Lambda
Function Elasticsearch
Service
GraphQL
Schema
Upload
Schema
GraphQL
Query
Mutation
Subscription
Real-time
Offline
AppSync
API
Cognito
User Pool
Legacy
Application
Amazon RDS
HTTPS
endpoint
24. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
How does AppSync connect to my data sources?
{
"version": "2017-02-28",
"operation": "GetItem",
"key": {
"company_id": $util.dynamodb.toDynamoDBJson($ctx.args.id),
}
}
25. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS AppSync – Debug Resolver Flow
Amazon CloudWatch logs
26. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
What about Offline access?
27. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AppSync Client Storage
• Offline is a write-through "Store"
• Persistent storage mediums back the Apollo normalized
cache
• Local Storage for web
• AsyncStorage for React Native
• SQLite on native platforms
• Database can be preloaded
• Offline client can be configured
• WiFi only
• WiFi and cellular
28. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS AppSync – Conflict Resolution
Conflict resolution in the cloud
a. Server wins
b. Client wins
c. Silent reject
d. Custom logic (AWS Lambda)
- Optimistic version check
- Extend with your own checks
Optional
• Client callback for Conflict Resolution is
still available as a fallback
{
"version" : "2017-02-28",
"operation" : "PutItem",
"key" : {
"id" : { "S" : "1" }
},
"condition" : {
"expression" : "attribute_not_exists(id)"
}
}
Example: Check that an ID doesn’t already exist:
"condition" : {
"expression" : "someExpression"
"conditionalCheckFailedHandler" : {
"strategy" : "Custom",
"lambdaArn" : "arn:..."
}
}
Run Lambda if version wrong:
29. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amplify Framework
30. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amplify Framework
31. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amplify Framework
ü CLI
ü Client libraries (native and JS support)
ü Console – Continuous deployment
and hosting
ü Prebuilt UI components
ü Toolchain
ü JS framework support
32. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Create, update, and delete cloud services
• Manage multiple environments
• GraphQL Transform
• GraphQL Codegen
Amplify Framework
CLI
33. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Categories
Authentication
Analytics
Interactions (chat bots)
API (REST)
API (GraphQL)
Amplify CLI
Serverless functions
Storage
XR
Push notifications
Video
AI/ML
34. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
$ amplify add auth
$ amplify push
$ amplify configure auth
$ amplify delete
$ amplify codegen add
Amplify CLI
35. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
$ amplify add api
–
# schema.graphql
type Post @model {
id: ID!
title: String!
}
36. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
@model
Top-level entity; creates DynamoDB table, resolvers, and additional schema (queries,
mutations, and subscriptions) for base type
@connection
Enables relationships between @model types
@auth
Enables set of authorization rules
@searchable
Handles streaming the data of an @model object type to Amazon Elasticsearch Service
and configures search resolvers
@versioned
Enables versioning
@function
Enables adding a Lambda function as a data source
–
37. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
type Post {
id: ID!
content: String
description: String
ups: Int
downs: Int
}
38. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
createPost
readPost
updatePost
deletePost
type Post
@model {
id: ID!
content: String
description: String
ups: Int
downs: Int
}
39. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
mutations
queries
createPost
readPost
updatePost
deletePost
type Post
@model
@auth(rules: [{allow:
owner}]){
id: ID!
content: String
description: String
ups: Int
downs: Int
}
40. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
searchPosts
mutations
queries
createPost
readPost
updatePost
deletePost
type Post
@model
@auth(rules: [{allow: owner}])
@searchable{
id: ID!
content: String
description: String
ups: Int
downs: Int
}
41. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
GraphQL Transform: Mix and match data sources
searchPosts
mutations
queries
createPost
readPost
updatePost
deletePost
type Post
@model
@auth(rules: [{allow:
owner}])
@searchable{
id: ID!
content: String
description: String
ups: Int
downs: Int
}
42. © 2019, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Resources
• https://docs.aws.amazon.com/appsync/lates
t/devguide/
• https://github.com/aws-amplify/amplify-js
• https://github.com/aws/aws-appsync-
community
• https://github.com/awslabs
• https://www.amplify.com/