Macro appsync_lambda_main

Source
appsync_lambda_main!() { /* proc-macro */ }
Expand description

Generates the code required to handle AWS AppSync Direct Lambda resolver events based on a GraphQL schema.

This macro takes a path to a GraphQL schema file and generates the complete foundation for implementing an AWS AppSync Direct Lambda resolver:

  • Rust types for all GraphQL types (enums, inputs, objects)
  • Query/Mutation/Subscription operation enums
  • AWS Lambda runtime setup with logging to handle the AWS AppSync event
  • Optional AWS SDK client initialization

§Options

  • batch = bool: Enable/disable batch request handling (default: true)
  • hook = fn_name: Add a custom hook function for request validation/auth
  • exclude_lambda_handler = bool: Skip generation of Lambda handler code
  • only_lambda_handler = bool: Only generate Lambda handler code
  • exclude_appsync_types = bool: Skip generation of GraphQL type definitions
  • only_appsync_types = bool: Only generate GraphQL type definitions
  • exclude_appsync_operations = bool: Skip generation of operation enums
  • only_appsync_operations = bool: Only generate operation enums
  • field_type_override = Type.field: CustomType: Override type of a specific field

§Examples

Basic usage with authentication hook:

use lambda_appsync::{appsync_lambda_main, AppsyncEvent, AppsyncResponse, AppsyncIdentity};

 fn is_authorized(identity: Option<&AppsyncIdentity>) -> bool {
    todo!()
}

// If the function returns Some(AppsyncResponse), the Lambda function will immediatly return it
// Else, the normal flow of the AppSync operation processing will continue
// This is primarily intended for advanced authentication checks that AppSync cannot perform,
// such as verifying that a user is requesting their own ID for example.
async fn auth_hook(
    event: &lambda_appsync::AppsyncEvent<Operation>
) -> Option<lambda_appsync::AppsyncResponse> {
    // Verify JWT token, check permissions etc
    if !is_authorized(event.identity.as_ref()) {
        return Some(AppsyncResponse::unauthorized());
    }
    None
}

appsync_lambda_main!(
    "schema.graphql",
    hook = auth_hook,
    dynamodb() -> aws_sdk_dynamodb::Client
);

Generate only types for lib code generation:

use lambda_appsync::appsync_lambda_main;
appsync_lambda_main!(
    "schema.graphql",
    only_appsync_types = true
);

Override field types and use multiple AWS clients:

use lambda_appsync::appsync_lambda_main;
appsync_lambda_main!(
    "schema.graphql",
    dynamodb() -> aws_sdk_dynamodb::Client,
    s3() -> aws_sdk_s3::Client,
    // Use String instead of the default lambda_appsync::ID
    field_type_override = Player.id: String,
);

Disable batch processing:

lambda_appsync::appsync_lambda_main!(
    "schema.graphql",
    batch = false
);