AWS AppSync GraphQL API Example: A Step-by-Step Guide

AWS AppSync simplifies building serverless applications by providing a managed GraphQL API service. It allows you to query, mutate, and subscribe to real-time data updates effortlessly. In this article, we’ll walk through a practical example of creating and using an AWS AppSync GraphQL API to manage a basic "Todo" application.


What Is AWS AppSync?

AWS AppSync is a managed service for building scalable and secure GraphQL APIs. It offers:

  • Real-time data synchronization using subscriptions.
  • Integration with AWS services like DynamoDB, Lambda, and S3.
  • Simplified authorization with Amazon Cognito, API keys, and IAM.

AWS AppSync GraphQL API Example

Objective

We’ll create a GraphQL API for a "Todo" app that:

  1. Stores data in a DynamoDB table.
  2. Allows users to create, read, update, and delete (CRUD) todo items.
  3. Supports real-time updates with subscriptions.

Step 1: Create an AppSync API

  1. Navigate to the AWS Management Console:
    • Go to the AppSync ConsoleCreate API.
  2. API Configuration:
    • Choose Start from scratch.
    • Enter a name (e.g., TodoAppAPI).
    • Select Create.

Step 2: Define the GraphQL Schema

In the Schema tab, define the GraphQL schema for the "Todo" app:

type Todo {
  id: ID!
  title: String!
  description: String
  completed: Boolean!
}

type Query {
  getTodoById(id: ID!): Todo
  listTodos: [Todo]
}

type Mutation {
  createTodo(title: String!, description: String, completed: Boolean!): Todo
  updateTodo(id: ID!, title: String, description: String, completed: Boolean): Todo
  deleteTodo(id: ID!): Todo
}

type Subscription {
  onTodoCreated: Todo @aws_subscribe(mutations: ["createTodo"])
}

schema {
  query: Query
  mutation: Mutation
  subscription: Subscription
}
  • Queries: Fetch todos (getTodoById, listTodos).
  • Mutations: Create, update, and delete todos.
  • Subscription: Receive real-time updates when new todos are created.

Step 3: Configure the Data Source

  1. Go to the Data Sources Tab:
    • Click Create Data Source.
  2. Configure DynamoDB as the Data Source:
    • Choose Amazon DynamoDB.
    • Create a new DynamoDB table with the following configuration:
      • Table Name: TodoTable
      • Primary Key: id (String)
    • Attach the data source to the API.

Step 4: Create Resolvers

Resolvers map API requests to data operations. Set up resolvers for the schema fields:

  1. Mutations (e.g., createTodo):
    • Choose the createTodo mutation.
    • Attach the DynamoDB data source.
  2. Repeat for other fields (getTodoById, updateTodo, etc.) using appropriate DynamoDB operations like GetItem, UpdateItem, and DeleteItem.

Use this for the Response:

$util.toJson($context.result)

Use the following resolver mapping template for the Request:

{
  "version": "2017-02-28",
  "operation": "PutItem",
  "key": {
    "id": { "S": "$util.autoId()" }
  },
  "attributeValues": $util.dynamodb.toMapValues($context.arguments)
}

Step 5: Test the GraphQL API

  1. Navigate to the Queries Tab:
    • Use the built-in AppSync GraphQL editor to test the API.
  2. Sample Queries:

Subscribe to New Todos:

subscription {
  onTodoCreated {
    id
    title
    description
    completed
  }
}

List Todos:

query {
  listTodos {
    id
    title
    description
    completed
  }
}

Create a Todo:

mutation {
  createTodo(title: "Learn AWS AppSync", description: "Build a Todo app", completed: false) {
    id
    title
    description
    completed
  }
}

Step 6: Add Authentication

  1. Go to the Settings Tab:
    • Select Authentication.
  2. Choose an Authentication Method:
    • API Key: Good for development.
    • AWS IAM: Best for server-to-server integration.
    • Amazon Cognito: Recommended for user-based authentication.

Best Practices for AWS AppSync

  1. Enable Caching: Use AppSync caching to improve performance for read-heavy queries.
  2. Monitor and Debug: Use CloudWatch for API monitoring and debugging.
  3. Use Subscriptions Wisely: Subscriptions are powerful but can increase costs if overused.
  4. Optimize DynamoDB: Use indexes for efficient querying in large datasets.

Conclusion

AWS AppSync makes it easy to build scalable, real-time GraphQL APIs integrated with other AWS services. By following this example, you can quickly set up a functional API for a "Todo" app, complete with CRUD operations and real-time updates. AWS AppSync empowers developers to focus on building features while leaving the heavy lifting of API management to AWS.