AWS AppSync
Core concept: AppSync is a fully managed GraphQL API service โ ideal for real-time, multi-data-source, and mobile/web apps.
AppSync vs API Gatewayโ
| Feature | AppSync | API Gateway |
|---|---|---|
| Protocol | GraphQL | REST / HTTP / WebSocket |
| Real-time | โ Native subscriptions (WebSocket) | WebSocket API only |
| Multiple data sources | โ Single query, multiple sources | โ One integration per endpoint |
| Offline sync | โ Conflict resolution built-in | โ |
| Caching | โ Server-side per-resolver | โ Stage-level |
| Use case | Mobile/web apps, real-time dashboards | REST APIs, microservices |
Data Sourcesโ
| Source | Description |
|---|---|
| DynamoDB | Direct DynamoDB access via VTL mapping |
| Lambda | Any custom logic, any backend |
| RDS / Aurora Serverless | SQL queries via Data API |
| OpenSearch | Full-text search |
| HTTP | Any REST API |
| None | Local resolvers (computed values) |
Resolversโ
Resolvers map GraphQL operations to data source operations using VTL (Velocity Template Language) or JavaScript (APPSYNC_JS):
// JavaScript resolver โ DynamoDB GetItem
export function request(ctx) {
return {
operation: 'GetItem',
key: util.dynamodb.toMapValues({ id: ctx.args.id })
};
}
export function response(ctx) {
return ctx.result;
}
Pipeline Resolversโ
Chain multiple data sources in one resolver:
Query: getOrderWithProducts
โ Step 1: DynamoDB (get order)
โ Step 2: DynamoDB (get product details)
โ Step 3: Lambda (calculate price with tax)
โ Return merged result
Real-Time Subscriptionsโ
# Schema
type Subscription {
onOrderUpdated(customerId: ID!): Order
@aws_subscribe(mutations: ["updateOrder"])
}
# Client subscribes via WebSocket
subscription {
onOrderUpdated(customerId: "CUST-123") {
orderId
status
updatedAt
}
}
- Client connects over WebSocket (MQTT over WebSocket)
- AppSync pushes updates automatically when mutation fires
Authorization Modesโ
| Mode | Use Case |
|---|---|
| API Key | Public APIs, development |
| AWS IAM | Server-to-server, IAM roles |
| Cognito User Pools | User authentication (most common) |
| OIDC | Third-party identity providers |
| Lambda Authorizer | Custom auth logic |
Multiple modes can be configured simultaneously, with one as default.
Cachingโ
Per-resolver TTL: 0 โ 3600 seconds
Cache key: resolved query arguments
Enables: Cache frequently-requested data
Reduces: Lambda invocations, DynamoDB reads
๐ฏ DVA-C02 Exam Tipsโ
- AppSync vs API Gateway: If you need real-time WebSocket Subscriptions natively tied to data changes, choose AppSync. If you need simple REST API microservices, choose API Gateway.
- Data Source unification: AppSync allows a single GraphQL endpoint to aggregate data from DynamoDB, RDS, and Lambda simultaneously.
- Resolvers: Use Pipeline Resolvers to sequence operations across multiple data sources.
๐งช Practice Questionsโ
Q1. A mobile app needs real-time order status updates pushed to connected clients whenever an order is updated. Which service is BEST suited?
A) API Gateway WebSocket API
B) SQS + client polling
C) AppSync with GraphQL Subscriptions
D) SNS mobile push notifications
โ Answer & Explanation
C โ AppSync Subscriptions automatically push mutation events to connected WebSocket clients โ purpose-built for this pattern. API Gateway WebSocket requires managing connection IDs and routing manually.
Q2. A single GraphQL query needs to aggregate data from DynamoDB and call a Lambda for business logic. Which AppSync feature enables this without client-side orchestration?
A) Multiple AppSync APIs
B) Pipeline Resolver chaining DynamoDB and Lambda functions
C) Lambda resolver that calls DynamoDB
D) REST API Gateway passthrough
โ Answer & Explanation
B โ Pipeline Resolvers chain multiple resolver functions (each connecting to a different data source) into a single GraphQL operation. The client makes one request; AppSync orchestrates the steps.