def generate_resolvers(self, graphql_api, type_name, data_source):
        """
        Creates AppSync resolvers for the given GraphQL type.  Resolvers are created by
        iterating through the `resolvers` directory in the API's build folder and
        parsing the resolver template filenames to find the operation name and type.

        :param type_name    The name of the GraphQL type.  This is the identifier which
                            appears after the `type` keyword in the schema.
        :param data_source  The AppSync data source Pulumi resource for the type.
        """
        resolvers_dir = self.amplify_api_build_dir.joinpath("resolvers")
        resolvers = []

        for req_file in resolvers_dir.iterdir():
            regex_match = match(
                f"^([a-zA-Z]+)\\.([a-zA-Z]+{type_name}s?)\\.req\\.vtl$",
                str(req_file.name),
            )

            if regex_match:
                operation_type = regex_match.group(1)
                operation_name = regex_match.group(2)
                res_file = resolvers_dir.joinpath(
                    f"{operation_type}.{operation_name}.res.vtl"
                )

                resolver = appsync.Resolver(
                    f"{self.stack_name}_{operation_name}_resolver",
                    api_id=graphql_api.id,
                    data_source=data_source.name,
                    field=operation_name,
                    type=operation_type,
                    request_template=req_file.read_text(),
                    response_template=res_file.read_text(),
                )

                resolvers.append(resolver)

        return resolvers
Example #2
0
## Link a data source to the Dynamo DB Table
data_source = appsync.DataSource("tenants-ds",
                                 name="TenantsDataSource",
                                 api_id=api.id,
                                 type="AMAZON_DYNAMODB",
                                 dynamodb_config={"table_name": table.name},
                                 service_role_arn=role.arn)

## A resolver for the [getTenantById] query
get_resolver = appsync.Resolver("get-resolver",
                                api_id=api.id,
                                data_source=data_source.name,
                                type="Query",
                                field="getTenantById",
                                request_template="""{
        "version": "2017-02-28",
        "operation": "GetItem",
        "key": {
            "id": $util.dynamodb.toDynamoDBJson($ctx.args.id),
        }
    }
    """,
                                response_template="$util.toJson($ctx.result)")

## A resolver for the [addTenant] mutation
add_resolver = appsync.Resolver("add-resolver",
                                api_id=api.id,
                                data_source=data_source.name,
                                type="Mutation",
                                field="addTenant",
                                request_template="""{
        "version" : "2017-02-28",