This project demonstrates a complete serverless backend using AWS AppSync with both GraphQL API and Event API capabilities, deployed with AWS SAM.
The application consists of:
- GraphQL API: Traditional GraphQL operations (queries, mutations) for CRUD operations
- Event API: Real-time event streaming for live notifications and updates
- DynamoDB: NoSQL database for data persistence
- Lambda Functions: Serverless compute for business logic and event publishing
- Authentication: Lambda-based auth for both APIs
- Create and fetch stories
- Add comments to stories
- Query stories with comments
- External API proxy functionality
- Real-time event publishing
- WebSocket connections for live updates
- Channel-based event routing
- API key authentication
- GraphQL mutations automatically publish events
- Real-time notifications when data changes
- Consistent authentication across both APIs
- AWS CLI configured with appropriate permissions
- Node.js 22+ and npm
- AWS SAM CLI
- Git
-
Clone and install dependencies:
git clone <repository-url> cd appsync-sam-example cd backend && npm install
-
Build the application:
make
-
Deploy to AWS:
sam deploy --profile your-aws-profile --region us-east-1
Replace
your-aws-profilewith your actual AWS CLI profile name.
After deployment, you'll get these outputs:
- GraphQL API URL:
AppSyncGraphQLUrl - Event API HTTP URL:
EventApiHttpURL - Event API WebSocket URL:
EventApiWebSocketURL - Event API Key:
EventApiKey
The project includes Bruno collections with ready-to-use GraphQL queries and mutations:
Available Operations:
-
Queries (
docs/appsync-sam-example/queries/):fetchStories.bru- Fetch paginated storiesgetStory.bru- Get single story with commentsgetUrlHttpDataSource.bru- Test HTTP data sourcegetUrlLambdaDataSource.bru- Test Lambda data source
-
Mutations (
docs/appsync-sam-example/mutations/):createStory.bru- Create a new storycreateStoryComment.bru- Add comment to story
To use the Bruno collections:
- Install Bruno
- Open the
docs/appsync-sam-example/directory in Bruno - Configure the environment with your GraphQL API URL and auth headers
- Run the requests to test the API
Connect via WebSocket:
wss://your-event-api-url/event/realtime
Subscribe to Events:
// Subscribe to story events
const subscription = client.subscribe({
channel: 'default/stories'
});
// Subscribe to comment events
const commentSub = client.subscribe({
channel: 'default/stories/story-id/comments'
});├── backend/
│ ├── src/
│ │ ├── graphql/
│ │ │ ├── schema.graphql
│ │ │ ├── modules/ # TypeScript resolvers
│ │ │ └── resolvers/ # Compiled JS resolvers
│ │ └── lambdas/ # Lambda functions
│ └── package.json
├── template.yaml # SAM template
├── Makefile # Build automation
└── README.md
- Linting:
npm run lint - Building:
make build - Testing: Deploy and test with GraphQL client or Event API client
- Real-time Updates: Event API provides live notifications
- Scalable: Serverless architecture auto-scales
- Secure: Lambda-based authentication
- Integrated: GraphQL mutations trigger events automatically
- Cost-effective: Pay-per-use pricing
- AWS AppSync (GraphQL + Event APIs)
- AWS Lambda (Node.js/TypeScript)
- Amazon DynamoDB
- AWS SAM
- TypeScript
- GraphQL