Last active
November 1, 2021 13:50
-
-
Save mmccall10/68f8572495a797a18a44849685ab0221 to your computer and use it in GitHub Desktop.
Helper to build AppSync CDK mapping templates in a single
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#set($id = $util.autoId()) | |
#set($pk = "PRODUCT#$id") | |
{ | |
"version" : "2018-05-29", | |
"operation" : "PutItem", | |
"key" : { | |
"PK" : $util.dynamodb.toDynamoDBJson($pk), | |
"SK" : $util.dynamodb.toDynamoDBJson($pk) | |
}, | |
"attributeValues" : $util.dynamodb.toMapValuesJson($ctx.args.input) | |
} | |
## res ## | |
#if($ctx.error) | |
$util.error($ctx.error.message, $ctx.error.type, $ctx.result) | |
#else | |
#set($pound = $ctx.result.PK.indexOf('#')) | |
#set($pound = $pound + 1) | |
#set ($id = $ctx.result.PK.substring($pound)) | |
$util.qr($ctx.result.put("id", $id)) | |
$util.toJson($ctx.result) | |
#end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { MappingTemplate } from '@aws-cdk/aws-appsync' | |
import * as tmp from 'tmp' | |
import * as fs from 'fs' | |
export default class SingleFileMappingTemplates { | |
static splitTemplate (template: string) { | |
const [req, res] = template.split('## res ##') | |
if (!req) { | |
throw Error('Request is missing from the single file mapping template') | |
} | |
if (!res) { | |
throw Error('Response is missing from the single file mapping template') | |
} | |
return [req, res] | |
} | |
static fromFile (template: string) { | |
const body = fs.readFileSync(template, 'utf8') | |
const [req, res] = this.splitTemplate(body) | |
const reqTmpfile = tmp.fileSync() | |
const respTmpFile = tmp.fileSync() | |
fs.writeFileSync(reqTmpfile.name, req.trim()) | |
fs.writeFileSync(respTmpFile.name, res.trim()) | |
return { | |
requestMappingTemplate: MappingTemplate.fromFile(reqTmpfile.name), | |
responseMappingTemplate: MappingTemplate.fromFile(respTmpFile.name) | |
} | |
} | |
static fromString (template: string) { | |
const [req, res] = this.splitTemplate(template) | |
return { | |
requestMappingTemplate: MappingTemplate.fromString(req), | |
responseMappingTemplate: MappingTemplate.fromString(res) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// example usage | |
props.api.createResolver({ | |
fieldName: 'createProduct', | |
typeName: 'Mutation', | |
dataSource: mainDS, | |
...SingleFileMappingTemplates.fromFile( | |
'src/merchant-service/vtl/Mutation.createMerchant.vtl' | |
) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment