Preparation

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

  1. Create a GraphQL API using the AWS AppSync console
  • Open the AWS AppSync console

  • Click Create API, select GraphQL APIs

CreateAPI

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

Designfromscratch

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

CreateAPI

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

CreateAPI

  • At the Review and create step, click Create API

CreateAPI

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

CreateAPI

  • 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!
}

CreateAPI

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

CreateAPI

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

CreateAPI CreateAPI