1.1.14 AWS AppSync
AWS AppSync — Managed GraphQL API
Tổng quan
AppSync là fully managed GraphQL service cho phép build real-time APIs kết nối nhiều data sources.
GraphQL vs REST
| Feature | GraphQL (AppSync) | REST (API Gateway) |
|---|
| Data fetching | Client chọn exact fields | Server quyết định response |
| Over-fetching | ❌ Không | ✅ Thường xuyên |
| Under-fetching | ❌ Không | ✅ Cần multiple calls |
| Real-time | ✅ Subscriptions (WebSocket) | WebSocket API riêng |
| Schema | ✅ Strongly typed | ❌ Tùy implementation |
| Caching | ✅ Server-side | ✅ Stage-level |
Core Concepts
Client → AppSync API → Resolver → Data Source
├── DynamoDB
├── Lambda
├── RDS (Aurora Serverless)
├── OpenSearch
├── HTTP endpoint
└── EventBridge
| Concept | Mô tả |
|---|
| Schema | GraphQL schema (types, queries, mutations, subscriptions) |
| Resolver | Map GraphQL operation → data source |
| Data Source | Backend service (DynamoDB, Lambda, RDS, HTTP, etc.) |
| Pipeline Resolver | Chain multiple functions trong 1 resolver |
Schema Example
type Order {
id: ID!
productName: String!
quantity: Int!
status: String!
}
type Query {
getOrder(id: ID!): Order
listOrders(status: String): [Order]
}
type Mutation {
createOrder(productName: String!, quantity: Int!): Order
}
type Subscription {
onCreateOrder: Order
@aws_subscribe(mutations: ["createOrder"])
}
Real-time Subscriptions
- WebSocket-based, tự động managed
- Client subscribe → nhận updates khi mutation xảy ra
- Use cases: chat apps, live dashboards, collaborative editing
Authorization Modes
| Mode | Use Case |
|---|
| API_KEY | Public APIs, development |
| AWS_IAM | AWS service-to-service |
| AMAZON_COGNITO_USER_POOLS | User authentication |
| OPENID_CONNECT | Third-party identity providers |
| AWS_LAMBDA | Custom authorization logic |
Caching
- Server-side caching (TTL configurable)
- Per-resolver caching
- Full request caching
- Encryption at rest và in transit
Conflict Detection (Offline)
- Versioned data sources
- Auto merge strategies: Optimistic Concurrency, Automerge, Lambda
- Amplify DataStore integration cho offline-first apps
Exam Tip: AppSync = GraphQL API. Cần real-time subscriptions = AppSync. Cần flexible data fetching (avoid over/under-fetching) = GraphQL/AppSync. REST API = API Gateway. AppSync hỗ trợ multiple data sources trong 1 API (DynamoDB + Lambda + RDS).