📖 Scenario: You are building a simple backend for a book store using AWS AppSync and DynamoDB. The store needs to keep track of books with their titles and authors.
🎯 Goal: Create a DynamoDB table and a GraphQL schema to store books with titles and authors and query books by ID using AppSync.
📋 What You'll Learn
Create a DynamoDB table named Books with id as the primary key
Define a GraphQL schema with a Book type containing id, title, and author
Add a query getBookById to fetch a book by its id
Add a mutation addBook to add a new book to the table
💡 Why This Matters
🌍 Real World
This project simulates building a backend for a book store app using AWS AppSync and DynamoDB, common in serverless web and mobile applications.
💼 Career
Understanding how to connect GraphQL APIs with DynamoDB is valuable for cloud developers, backend engineers, and anyone working with AWS serverless technologies.
Progress0 / 4 steps
1
Create DynamoDB Table
Create a DynamoDB table named Books with a primary key called id of type String.
DynamoDB
Hint
Use TableName to name the table and define id as the HASH key with type S for string.
2
Define GraphQL Schema
Define a GraphQL type called Book with fields id (ID!), title (String!), and author (String!).
DynamoDB
Hint
Use type Book { ... } to define the fields with exclamation marks to mark them as required.
3
Add Query and Mutation
Add a query called getBookById that takes id: ID! and returns a Book. Add a mutation called addBook that takes id: ID!, title: String!, and author: String! and returns a Book.
DynamoDB
Hint
Define type Query and type Mutation with the specified fields and arguments.
4
Complete AppSync Resolver Mapping
Add the resolver mapping templates for getBookById query and addBook mutation to connect AppSync with the DynamoDB Books table. Use id as the key for getBookById and pass id, title, and author as the item for addBook.
DynamoDB
Hint
Use GetItem operation with id key for the query resolver. Use PutItem with id, title, and author attributes for the mutation resolver.
Practice
(1/5)
1. What is the main benefit of using AWS AppSync with DynamoDB in an application?
easy
A. It allows apps to request only the data they need using GraphQL.
B. It automatically creates SQL tables for data storage.
C. It requires manual scaling of the database.
D. It replaces the need for any backend code.
Solution
Step 1: Understand AppSync's role
AppSync uses GraphQL to let apps ask for exactly the data they need, avoiding over-fetching.
Step 2: Understand DynamoDB's role
DynamoDB stores data quickly and scales automatically, but does not create SQL tables or replace backend code entirely.
Final Answer:
It allows apps to request only the data they need using GraphQL. -> Option A
Quick Check:
AppSync + GraphQL = precise data requests [OK]
Hint: Remember: AppSync uses GraphQL for precise data fetching [OK]
Common Mistakes:
Thinking DynamoDB creates SQL tables
Believing manual scaling is needed
Assuming AppSync removes all backend code
2. Which of the following is the correct way to define a DynamoDB data source in an AWS AppSync GraphQL schema?
easy
A. type Query { getItem(id: ID!): Item @aws_dynamodb(tableName: MyTable) }
B. type Query { getItem(id: ID!): Item @aws_lambda(functionName: "MyFunction") }
C. type Query { getItem(id: ID!): Item @aws_dynamodb(tableName: "MyTable", region: "us-east-1") }
D. type Query { getItem(id: ID!): Item @aws_s3(bucketName: "MyBucket") }
Solution
Step 1: Identify the correct directive for DynamoDB
The @aws_dynamodb directive is used to link a DynamoDB table as a data source in AppSync.
Step 2: Check syntax correctness
type Query { getItem(id: ID!): Item @aws_dynamodb(tableName: "MyTable", region: "us-east-1") } correctly includes the tableName and region parameters, which are valid for specifying the DynamoDB data source.
Final Answer:
type Query { getItem(id: ID!): Item @aws_dynamodb(tableName: "MyTable", region: "us-east-1") } -> Option C
Quick Check:
@aws_dynamodb with tableName and region is correct syntax [OK]
Hint: Look for @aws_dynamodb with tableName and region parameters [OK]
Common Mistakes:
Using @aws_lambda instead of @aws_dynamodb for DynamoDB
Forgetting quotes around tableName value
Using @aws_s3 which is unrelated to DynamoDB
3. Given this GraphQL query in AppSync linked to DynamoDB:
query GetBook { getBook(id: "123") { id title author } }
5. You want to create a real-time chat app using AppSync and DynamoDB. Which approach best ensures that clients receive new messages instantly without polling?
hard
A. Have clients poll DynamoDB every few seconds for new messages.
B. Use AppSync subscriptions linked to DynamoDB streams to push updates to clients.
C. Use a Lambda function to write messages to DynamoDB and notify clients via email.
D. Store messages in DynamoDB and refresh the UI only when the app restarts.
Solution
Step 1: Understand real-time updates in AppSync
AppSync supports GraphQL subscriptions that can push data to clients when data changes.
Step 2: Connect DynamoDB streams to subscriptions
DynamoDB streams capture table changes and can trigger AppSync subscriptions to notify clients instantly.
Step 3: Evaluate other options
Polling wastes resources, email notifications are slow and unrelated, and UI refresh on restart is not real-time.
Final Answer:
Use AppSync subscriptions linked to DynamoDB streams to push updates to clients. -> Option B