Preparation
- Create a table in DynamoDB using the following command:
aws cloudformation create-stack \
--stack-name AWSAppSyncTutorialForAmazonDynamoDB \
--template-url https://s3.us-west-2.amazonaws.com/awsappsync/resources/dynamodb/AmazonDynamoDBCFTemplate.yaml \
--capabilities CAPABILITY_NAMED_IAM
After running this command, an AppSyncTutorial-Post table is created.
- Create a GraphQL API using the AWS AppSync console

- In the GraphQL APIs section, select Design from scratch, then click Next

- At the Specify API details step, enter a name for the API (e.g.,
AWSAppSyncTutorial), then click Next

- At the Specify GraphQL resources step, continue by clicking Next

- At the Review and create step, click Create API

- After the API is successfully created, select the Schema tab from the left menu

- Replace all the default content with the code below, then click Save Schema
schema {
query: Query
mutation: Mutation
}
type Query {
getPost(id: ID): Post
}
type Mutation {
addPost(
id: ID!
author: String!
title: String!
content: String!
url: String!
): Post!
}
type Post {
id: ID!
author: String
title: String
content: String
url: String
ups: Int!
downs: Int!
version: Int!
}

- Next, to connect the API with DynamoDB, select the Data Sources tab from the left menu
- Click Create data source

- Configure the Data source as follows:
- Data source name: Enter
PostDynamoDBTable - Data source type: Select Amazon DynamoDB table
- Region: Select the region where you ran the CloudFormation template
- Table name: Select the AppSyncTutorial-Post table
- IAM role: Select Choose an existing role, then find and select the role created by CloudFormation which is AppSyncTutorialAmazonDynamoDBRole
- Click Create to finish creating the Data source.
