2. Traditional Data Fetching
Data
ü Trivial to set up
ü Standard HTTP Calls
x Relationships
x Lists with reduced information
x Query support
x Ordering and pagination
x Notifications
/posts
/postInfo
/postJustTitle
/postsByAuthor
/postNameStartsX
/postByTag
/commentsOnPost
REST Endpoints
5. What Is GraphQL?
Agnostic of underlying
Data Store
Optimized for network
performance and
flexibility
Query language for your
API and a runtime for
fulfilling queries with
existing data
6. How does GraphQL work?
type Query {
getTodos: [Todo]
}
type Todo {
id: ID!
name: String
description: String
priority: Int
duedate: String
}
Model data with application
schema
query {
getTodos {
id
name
priority
}
}
Client requests what it needs
{
"id": "1",
"name": "Get Milk",
"priority": "1"
},
{
"id": "2",
"name": "Go to gym",
"priority": "5"
},…
Only that data is returned
7. GraphQL Operations
query {
search(q: “harry
potter”) {
title
}
}
Queries Read data
mutation {
create(title:
“new book”) {
id
}
}
Mutations Write data
subscription {
onCreate {
id
title
}
}
Subscriptions Listen for data
9. GraphQL vs REST
Include vs Endpoint & Reduction in call Volumes
/posts?include=title,authors
/posts?include=title,authors,authors.firstname, authors.lastname posts {
title
authors {
firstname
lastname
}
}
{
...
“author”:{
“_links”: {
“self”:https://api.example.com/api/author/foo
}
}
...
}
REST/RPC GraphQL
Hypermedia
10. GraphQL vs REST
/graphql
posts {
title
author
}
Posts (maxSize: 10) {
title
authorName
content
}
posts {
title
authorName
content
}
Posts (maxSize: 10)
{
title
author {
firstName
lastName
imageUrl
}
content
}
Posts (maxSize: 10) {
title
author {
firstName
lastName
imageUrl(size:80)
}
content
comments{
user
text
}
}
11. What is AWS AppSync?
Managed Serverless
GraphQL service
Connect to data sources in
your account
Add data sync, real-time and
offline capabilities
GraphQL façade for
any AWS service
Conflict detection and
resolution in the
cloud
Enterprise security
features (IAM,
Cognito, API keys)
12. Why GraphQL with AppSync?
Data
Aggregation
Bandwidth
Optimization
Rapid
Prototyping
Easily evolve
APIs
Client-defined
Data Selection
13. Apps you can build with AppSync
Real Time Apps
Dashboard
Leaderboards
Field Service
Apps
Geo Apps Apps with
complex data
structure and
types
Chat Apps
17. AWS AppSync Authorization Rules
type Post
@model
@auth(
rules: [
{allow: owner, ownerField: "owner",
mutations: [create, update, delete],
queries: [get, list]},
])
{
id: ID!
title: String!
content: String!
rating: Int
owner: String
}
type Post
@model
@auth(
rules: [
{allow: groups, groupsField: "groups"}
])
{
id: ID!
title: String!
groups: String
}
get: If the record’s owner is not the same as the logged in user (via
$ctx.identity.username), throw $util.unauthorized()
get: In the response mapping template verify that the result’s groups
attribute contains at least one of the caller’s claimed groups via
$ctx.identity.claims.get("cognito:groups")
18. AWS AppSync Real-time data
GraphQL subscriptions enable real-time communication
AWS AppSync doubles as a pub-sub broker via MQTT over WebSockets
type Mutation {
publish: Message
}
type Subscription {
subscribe: Message
@aws_subscribe(mutations: [“publish”])
}
19. Jane
Version : 2 Updated Document
Jane
Version : 2 Updated Document
Version : 3 Updated Document
Version : 1 New Document
Time
John
John
Jane goes offline
Jane comes back online
Version : 4 Updated Document
John
Jane
AWS AppSync Offline mutuation
20. Conflict resolution in the cloud
1. Server wins
2. Silent reject
3. 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:
AWS AppSync Conflict Resolution and sync