1

我正在使用带有 AWS CDK 的 typescript 为 api 网关生成 cloudFormation 模板。我有一个 Apache Velocity 模板,可以帮助我转换我的响应。当我使用打字稿创建 API 网关时。如何从代码本身传递模板。我需要在需要字符串的 IntegrationOptions 接口中的 responseTemplates 中传递我的模板。我一直无法找到任何合理的方法将其转换为字符串。

{
    "sellableQuantity": $inputRoot.quantity),
    "reservedQuantity": $inputRoot.reservedQuantity)
    "marketplaceInventories": [
        #foreach( $marketplaceInventory in $inputRoot.marketplaceInventories) )
            {
                "sellableQuantity": $marketplaceInventory.sellableQuantity,
                "marketplaceAttributes": {
                    #set( $marketplaceAttributes = $marketplaceInventory.marketplaceAttributes )
                    "marketplaceName": "$marketplaceAttributes.marketplaceName",
                    "channelName": "$marketplaceAttributes.channelName"
                }
            }
            #if( $foreach.hasNext ) , #end
        #end
    ] 
}
4

2 回答 2

2

你的问题真的是“我如何定义一个长字符串而不用担心在 javascript 中转义特殊字符?”

我认为javascript 模板文字是最好的选择,因为它可以让您不必担心转义或行继续。在您的字符串周围使用反引号,String.raw您可以确保您定义的内容将逐字传递:

let myVelocityTemplate = String.raw`{
    "sellableQuantity": $inputRoot.quantity),
    "reservedQuantity": $inputRoot.reservedQuantity)
    "marketplaceInventories": [
        #foreach( $marketplaceInventory in $inputRoot.marketplaceInventories) )
            {
                "sellableQuantity": $marketplaceInventory.sellableQuantity,
                "marketplaceAttributes": {
                    #set( $marketplaceAttributes = $marketplaceInventory.marketplaceAttributes )
                    "marketplaceName": "$marketplaceAttributes.marketplaceName",
                    "channelName": "$marketplaceAttributes.channelName"
                }
            }
            #if( $foreach.hasNext ) , #end
        #end
    ] 
}`
于 2020-01-20T03:11:17.303 回答
0

java,非常简单:

private GraphQLApi CreateGraphQLApi(String API_NAME) {
    return GraphQLApi.Builder.create(this, API_NAME + "_AppSyncApi")
            .name(API_NAME.concat("_AppSyncApi"))
            .schemaDefinitionFile(Constants.SCHEMA_PATH)
            .build();
}

您可以传递架构路径并让cdk加载和部署资源。

我认为typescriptAPI 与其他语言有 1 对 1 的匹配

于 2020-02-26T16:49:51.053 回答