lambda-appsync
A type-safe framework for AWS AppSync Direct Lambda resolvers — generates Rust types, operation dispatch, and Lambda runtime glue from a GraphQL schema at compile time.
About
The lambda-appsync crate provides procedural macros that read a GraphQL schema file at compile time and generate:
- Rust types for all GraphQL objects, inputs, and enums
- An
Operationenum covering every query, mutation, and subscription field, with argument extraction and dispatch logic - A
Handlerstrait andDefaultHandlersstruct for wiring up the AWS Lambda runtime
You write resolver functions annotated with #[appsync_operation(...)] and the framework validates their signatures against the schema, handles deserialization, and produces properly formatted AppSync responses.
Features
- ✨ Type-safe GraphQL schema conversion to Rust types at compile time
- 🔒 Compile-time validation of resolver function signatures against the schema
- 🚀 Full AWS Lambda runtime integration with batch support
- 🔔 AWS AppSync enhanced subscription filters
- 🔐 Comprehensive support for all AWS AppSync auth types (Cognito, IAM, OIDC, Lambda, API Key)
- 📦 Composable macro architecture — use
make_appsync!for simple setups ormake_types!/make_operation!/make_handlers!individually for multi-crate projects - 🛡️ Customizable request handling via the
Handlerstrait (authentication hooks, logging, etc.) - 🔧 Type and name overrides for fine-grained control over generated code
- 📊 Configurable logging with
log/env_loggerandtracingsupport - 🧩
const fnenum utilities (all(),index(),COUNT) for generated enums
Known Limitations
- GraphQL unions are not supported and will be ignored by the macro
- GraphQL interfaces are not directly supported, though concrete types that implement interfaces will work correctly
- Arguments in fields of non-operation types (i.e. not Query, Mutation, or Subscription) are ignored by the macro
If your project requires union or interface support, or you have ideas on how the macro could use field arguments for regular types, please open a GitHub issue detailing your use case.
Installation
Add this dependency to your Cargo.toml:
[]
= "0.10.0"
Or using cargo:
Note: Starting with v0.10.0, the default feature set is empty. The crate works out of the box without any features. Enable
env_logger,tracing, orlogonly if you need their re-exports or logging integration. See Feature Flags for details.
Quick Start
- Create your GraphQL schema file (e.g.
schema.graphql):
type Query {
players: [Player!]!
gameStatus: GameStatus!
}
type Player {
id: ID!
name: String!
team: Team!
}
enum Team {
RUST
PYTHON
JS
}
enum GameStatus {
STARTED
STOPPED
}
- Generate types, operations, and handlers from the schema:
use ;
// Generate everything from the schema
make_appsync!;
// Implement resolver functions:
async
async
// Wire up the Lambda runtime:
async
When in a workspace context, all relative schema paths are resolved relative to the workspace root directory.
The framework's macros verify that your function signatures match the GraphQL schema at compile time and automatically wire everything up to handle AWS AppSync requests.
Example Project
Check out the benchmark-game sample project that demonstrates lambda-appsync in action. This full-featured demo implements a GraphQL API for a mini-game web application using AWS AppSync and Lambda, showcasing:
- 🎮 Real-world GraphQL schema
- 📊 DynamoDB integration
- 🏗️ Infrastructure as code with AWS CloudFormation
- 🚀 CI/CD pipeline configuration
Clone the repo to get started with a production-ready template for building serverless GraphQL APIs with Rust.
Additional Examples
Custom Handler with Authentication Hook
Override the Handlers trait to add pre-processing logic such as authentication checks:
use ;
use ;
make_appsync!;
;
async
async
This replaces the old hook parameter from appsync_lambda_main!. See the make_handlers! documentation for the full migration table.
Composable Macros for Multi-Crate Projects
For larger projects where you share GraphQL types across multiple Lambda functions, use the individual macros:
use ;
// Step 1: Generate types (could live in a shared lib crate)
make_types!;
// Step 2: Generate Operation enum and dispatch logic
make_operation!;
// Step 3: Generate Handlers trait and DefaultHandlers
make_handlers!;
async
async
A typical multi-crate layout:
my-project/
├── schema.graphql
├── shared-types/ # lib crate using make_types!
│ └── src/lib.rs
├── lambda-a/ # bin crate using make_operation! + make_handlers!
│ └── src/main.rs
└── lambda-b/ # another bin crate
└── src/main.rs
When types live in a different module, use the type_module parameter on make_operation!:
make_operation!;
Type and Name Overrides
Override generated Rust types or names for specific GraphQL elements:
make_appsync!;
Controlling Default Traits and Derives
Control which traits are derived on generated types:
make_appsync!;
Default traits for structs: Debug, Clone, Serialize, Deserialize.
Default traits for enums: Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash, Display, FromStr.
Subscription Filters
Build type-safe enhanced subscription filters:
use ;
use ;
async
Important: When using enhanced subscription filters, your AppSync Response mapping template must contain:
#if($context.result.data) $extensions.setSubscriptionFilter($context.result.data) #end null
Accessing the AppSync Event
Access the full AppSync event context in operation handlers with the with_appsync_event flag:
use ;
async
Original Function Preservation
By default, #[appsync_operation] preserves the original function alongside the generated impl Operation method. You can call it directly elsewhere in your code:
async
// fetch_players() is still available as a regular function
If you want the function to be removed (its body is inlined into the generated method), use inline_and_remove. This can be handy when generating handlers with macro_rules!, where you don't want the function name to collide with itself:
game_status_mut!;
game_status_mut!;
game_status_mut!;
Note: When the
compatfeature is enabled, the old default behavior is restored (inline and remove), and thekeep_original_function_nameparameter is available to opt back into preservation.
AWS SDK Error Support
AWS SDK errors are automatically converted to AppsyncError, allowing use of the ? operator:
async
Error Merging
Combine multiple errors using the pipe operator:
let err = new
| new;
// error_type: "ValidationError|DatabaseError"
// error_message: "Invalid email\nUser not found in database"
Macro Reference
| Macro | Kind | Purpose |
|---|---|---|
make_appsync! |
All-in-one | Generate types, Operation enum, and Handlers trait from a schema |
make_types! |
Composable | Generate Rust structs and enums from schema type definitions |
make_operation! |
Composable | Generate the Operation enum and dispatch logic |
make_handlers! |
Composable | Generate the Handlers trait and DefaultHandlers struct |
#[appsync_operation] |
Attribute | Bind an async function to a specific GraphQL operation |
appsync_lambda_main! |
Legacy (compat) |
Deprecated monolithic macro — prefer make_appsync! for new code |
For detailed syntax, options, and examples for each macro, see the API documentation on docs.rs.
When to Use Which Macro
| Scenario | Recommendation |
|---|---|
| Single Lambda function, all code in one crate | make_appsync! |
| Shared types library + multiple Lambda binaries | make_types! in the lib, make_operation! + make_handlers! in each binary |
| Custom handler logic only | make_appsync! + override Handlers trait methods |
| Need different operations per Lambda | make_types! shared, separate make_operation! per Lambda |
Feature Flags
| Feature | Default | Description |
|---|---|---|
compat |
❌ | Enables the deprecated appsync_lambda_main! macro and re-exports aws_config. Not required for make_appsync! or the composable macros. |
log |
❌ | Re-exports the log crate so resolver code can use log::info! etc. without a separate dependency. Enables log::error! in generated dispatch code. |
env_logger |
❌ | Re-exports env_logger for local development. Implies log and compat. |
tracing |
❌ | Re-exports tracing and tracing-subscriber. When enabled, the generated Handlers trait wraps each event dispatch in a tracing::info_span! for per-operation observability. |
# No features needed for basic usage
= "0.10.0"
# Enable tracing instrumentation
= { = "0.10.0", = ["tracing"] }
# Enable log + env_logger (similar to pre-0.10 defaults)
= { = "0.10.0", = ["env_logger"] }
# Use both tracing and env_logger (migration scenarios)
= { = "0.10.0", = ["env_logger", "tracing"] }
# Just the log crate re-export, bring your own logger
= { = "0.10.0", = ["log"] }
Migrating from appsync_lambda_main!
The legacy appsync_lambda_main! macro (now behind the compat feature) combined type generation, operation dispatch, Lambda runtime setup, logging initialization, and AWS SDK client initialization into a single call. The new architecture splits these concerns, giving you explicit control over each part.
Before (v0.9)
use ;
use ;
async
appsync_lambda_main!;
async
After (v0.10)
use ;
use ;
// 1. Generate types, Operation enum, and Handlers trait
make_appsync!;
// 2. Hook → custom Handlers impl
;
// 3. Resolver functions — unchanged
async
// 4. main() — you own the runtime, logging, and SDK clients
async
Migration cheat sheet
appsync_lambda_main! option |
New approach |
|---|---|
hook = fn_name |
Override Handlers::appsync_handler |
log_init = fn_name |
Call your init function in main() |
event_logging = true |
Add logging in your Handlers impl or main() |
dynamodb() -> Client |
Initialize AWS SDK clients in main() |
only_appsync_types = true |
Use make_types! alone |
exclude_appsync_types = true |
Use make_operation! + make_handlers! |
batch = false |
make_appsync!("schema.graphql", batch = false) or make_handlers!(batch = false) |
Cargo.toml changes
[dependencies]
- lambda-appsync = { version = "0.9.0", features = ["tracing"] }
+ lambda-appsync = { version = "0.10.0", features = ["tracing"] }
+ tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
+ lambda_runtime = "1.0"
+ # Add AWS SDK crates you use directly:
+ aws-config = { version = "1.5", features = ["behavior-version-latest"] }
+ aws-sdk-dynamodb = "1.59"
Note:
tokioandlambda_runtimeare re-exported by the crate (lambda_appsync::tokio,lambda_appsync::lambda_runtime), so adding them as direct dependencies is optional but recommended for clarity.
FAQ
Q: Why are the default features empty now?
The crate works without any logging framework. This gives you full control — add log, env_logger, or tracing only when you need them. If you were relying on the old defaults, add features = ["env_logger"] to restore the previous behavior.
Q: Do I need tokio and lambda_runtime as direct dependencies?
The crate re-exports both tokio and lambda_runtime, so you can use lambda_appsync::tokio and lambda_appsync::lambda_runtime without adding them to your Cargo.toml. However, adding them directly is fine too.
Q: Why do I have to write my own main() now?
Generating main(), initializing loggers, and instantiating AWS SDK clients are application-level concerns — they have nothing to do with AppSync type generation or operation dispatch. The old appsync_lambda_main! bundled all of this together, which meant every new user preference (a different logging framework, a custom tracing setup, an SDK interceptor) required yet another macro option and feature flag. That approach doesn't scale.
The new design draws a clear boundary: lambda-appsync generates the types, the operation dispatch, and a handler trait. Everything else — runtime setup, logging, SDK clients, middleware — lives in your main() where it belongs.
A concrete example: the awssdk-instrumentation crate provides out-of-the-box OpenTelemetry/X-Ray tracing for AWS SDK calls via a Tower layer wrapping the Lambda runtime. If lambda-appsync owns main(), there is no way to insert that layer. With the new design, composing the two is straightforward:
use ;
use DefaultInterceptor;
use ;
make_appsync!;
async
async
Q: How does batch processing work?
By default (batch = true), the generated service_fn deserializes the Lambda payload as a Vec<AppsyncEvent> and processes events concurrently via tokio::spawn. Set batch = false if your AppSync API is not configured for batch invocations.
Minimum Supported Rust Version (MSRV)
This crate requires Rust 1.82.0 or later.
Contributing
We welcome contributions! Please read our Contributing Guidelines before submitting pull requests.
Git Hooks
This project uses git hooks to ensure code quality. The hooks are automatically installed when you enter a development shell using nix flakes and direnv.
The following checks run before each commit:
- Code formatting (
cargo fmt) - Linting (
cargo clippy) - Doc generation (
cargo doc) - Tests (
cargo test)
To manually install the hooks:
License
Distributed under the MIT License. See LICENSE for more information.
Authors
- Jérémie RODON (@JeremieRodon)
- RustyServerless organization