How can I create an AppSync Subscription with AWS CDK? I am able to successfully create Queries and Mutations, but I'm having trouble with Subscriptions. Is it possible? If so, how can I achieve it?
This is how I'm currently attempting it:
cdk:
const projectAddedSubscription = new appsync.Resolver(this, 'ProjectAddedSubscription', {
api: api,
typeName: 'Subscription',
fieldName: 'onCreateProject',
dataSource: appDbDataSource,
requestMappingTemplate: appsync.MappingTemplate.fromFile('appsync/resolvers/request/onCreateProjectRequest.vtl'),
responseMappingTemplate: appsync.MappingTemplate.fromFile('appsync/resolvers/response/onCreateProjectResponse.vtl')
});
GraphQL:
type Project {
PK: ID!
SK: ID!
name: String!
createdAt: String!
createdBy: String!
}
type Mutation {
createProject(input: CreateProjectInput!): CreateProjectResponse!
}
type Subscription {
onCreateProject: Project
}
request VTL:
{
"version": "2018-05-29",
"operation": "Subscription",
"payload": {
"field": "onCreateProject"
}
}
response VTL:
#if($ctx.error)
$util.error($ctx.error.message, $ctx.error.type)
#else
$util.toJson($ctx.result.data.onCreateProject)
#end