#[appsync_operation]
Expand description
Marks an async function as an AWS AppSync resolver operation, binding it to a specific Query, Mutation or Subscription operation defined in the GraphQL schema.
The marked function must match the signature of the GraphQL operation, with parameters and return type matching what is defined in the schema. The function will be wired up to handle requests for that operation through the AWS AppSync Direct Lambda resolver.
§Important
This macro can only be used in a crate where the appsync_lambda_main! macro has been used at the
root level (typically in main.rs
). The code generated by this macro depends on types and
implementations that are created by appsync_lambda_main!.
§Example Usage
use lambda_appsync::{appsync_operation, AppsyncError, ID};
// Execute when a 'getUser' query is received
#[appsync_operation(query(getUser))]
async fn get_user(id: ID) -> Result<User, AppsyncError> {
// Implement resolver logic
Ok(dynamodb_get_user(id).await?)
}
// Handle a 'createUser' mutation
#[appsync_operation(mutation(createUser))]
async fn create_user(name: String, email: String) -> Result<User, AppsyncError> {
Ok(dynamodb_create_user(name, email).await?)
}
// Keep the original function name available separately
#[appsync_operation(query(getUser), keep_original_function_name)]
async fn fetch_user(id: ID) -> Result<User, AppsyncError> {
// Can still call fetch_user() directly
Ok(dynamodb_get_user(id).await?)
}
The macro will ensure the function signature matches what is defined in the GraphQL schema, and wire it up to be called when AWS AppSync invokes the Lambda resolver for that operation.