Search code examples
velocityserverless-frameworkaws-appsync

When setting up an AppSyncAPI with serverless-framework is it possible to have more than one velocity template in a single file?


Following instructions on the repository https://github.com/sid88in/serverless-appsync-plugin I was able to set an AWS AppSyncAPI successfully, but It turns I end up with a lot of mapping-templates, eg. customer-create.vtl, as each domain object needs a request and response .vtl file to be referred on serverless.yml.

I wonder if is it possible to put more than one template in a single file?

I mean, instead of having customer-create.vtl and customer-delete.vtl paste both contents in a single file, like customer.vtl and then point to template by a property name.

Have tried this, but it didn't worked:

{"create":{
  "version": "2017-02-28",
  "operation": "PutItem",
  "key": {
    "customerId": $util.dynamodb.toDynamoDBJson($ctx.identity.username),
    "attributeValues": $util.dynamodb.toMapValuesJson($ctx.args.input),  
  },
},
"delete":{
  "version": "2017-02-28",
  "operation": "DeleteItem",
  "key": {
    "customerId": $util.dynamodb.toDynamoDBJson($ctx.args.input.customerId)
  },
 }
}
mappingTemplates:
  # Customer
  - dataSource: Dynamo_customer
    type: Mutation 
    field: createCustomer
    request: ${file(customer-templates.vtl):create}
    response: common-response.vtl
  - dataSource: Dynamo_customer
    type: Mutation 
    field: deleteCustomer
    request: ${file(customer-templates.vtl):delete}
    response: common-response.vtl 
  - dataSource: Dynamo_customer

Solution

  • Unfortunately, this is not possible with AppSync at this time. I've noted this as a feature request for the service, though, and will bring it back to the team for prioritization in a future release.

    Thanks for the feedback!