aws / aws-appsync-community

The AWS AppSync community
https://aws.amazon.com/appsync
Apache License 2.0
506 stars 32 forks source link

Add environment variables support for JS resolvers #267

Open sashee opened 1 year ago

sashee commented 1 year ago

When a script needs some information about other resources, the primary way is to use environment variables. For example, I have this Terraform script:

resource "aws_appsync_function" "Mutation_pairUsers_1" {
    # ...
  code = <<EOF
import {util} from "@aws-appsync/utils";
export function request(ctx) {
    return {
        version: "2018-05-29",
        operation: "TransactWriteItems",
        transactItems: [
            {
                // needs the table name here
                table: "${aws_dynamodb_table.user.name}",
                operation: "UpdateItem",
                key: {
                    id : {S: ctx.args.user1}
                },
                // ...
            },
            {
                // ...
            }
        ]
    }
}
// ...
EOF
}

The TransactWriteItems needs the table name which is part of the environment. Currently, the only choice is string concatenation or templating (table: "${aws_dynamodb_table.user.name}",) but there could be a separate block for these values that are available under the process.env, similarly to how Lambda functions work.

onlybakam commented 1 year ago

hey @sashee , for somethinig like this, how would you want to define the env variables? would you want to do this as a configuration on the AppSync API, or on a specific resolver?

sashee commented 1 year ago

Hey @onlybakam , that's a great question and I haven't thought about it.

Probably adding it to individual resolvers gives the greatest flexibility and I don't see many use-cases when API-wide variables are better. When I need to inject something from the environment it is usually because of the Transact*Items DDB calls need the table names but even these are rare (maybe ~10% of all resolvers). So overall both would work.