0
0
DynamoDBquery~5 mins

AppSync with DynamoDB (GraphQL)

Choose your learning style9 modes available
Introduction

AppSync lets you easily get and change data using GraphQL. DynamoDB stores your data fast and safely. Together, they help build apps that work well and update quickly.

You want to build a mobile app that shows live data updates.
You need a simple way to ask for only the data you want from a database.
You want to connect your app to a database without writing complex backend code.
You want to store data that can grow quickly and still be fast to access.
You want to use GraphQL queries and mutations to manage your data.
Syntax
DynamoDB
type Query {
  getItem(id: ID!): Item
}

type Mutation {
  addItem(id: ID!, name: String!): Item
}

type Item {
  id: ID!
  name: String!
}

This is a simple GraphQL schema defining how to get and add items.

AppSync connects these queries and mutations to DynamoDB tables behind the scenes.

Examples
This query asks for the item with id "123" and wants its id and name back.
DynamoDB
query GetItem {
  getItem(id: "123") {
    id
    name
  }
}
This mutation adds a new item with id "456" and name "Book" to the database.
DynamoDB
mutation AddItem {
  addItem(id: "456", name: "Book") {
    id
    name
  }
}
If the item with id "999" does not exist, the result will be null.
DynamoDB
query GetItem {
  getItem(id: "999") {
    id
    name
  }
}
Sample Program

This schema defines how to get and add items. The query asks for an item with id "1". The mutation adds an item with id "1" and name "Notebook".

DynamoDB
# GraphQL schema for AppSync with DynamoDB

schema {
  query: Query
  mutation: Mutation
}

type Query {
  getItem(id: ID!): Item
}

type Mutation {
  addItem(id: ID!, name: String!): Item
}

type Item {
  id: ID!
  name: String!
}

# Example query to get an item
query GetItem {
  getItem(id: "1") {
    id
    name
  }
}

# Example mutation to add an item
mutation AddItem {
  addItem(id: "1", name: "Notebook") {
    id
    name
  }
}
OutputSuccess
Important Notes

AppSync automatically connects GraphQL operations to DynamoDB tables using resolvers.

Time complexity depends on DynamoDB's performance, which is usually very fast for single items.

Common mistake: forgetting to set up the correct permissions for AppSync to access DynamoDB.

Use AppSync with DynamoDB when you want a managed, scalable backend with flexible data queries.

Summary

AppSync uses GraphQL to let apps ask for exactly the data they need.

DynamoDB stores data quickly and scales automatically.

Together, they make building real-time, scalable apps easier without complex backend code.