javascript解析器的响应映射模板



我有用JS编写的解析器,而不是用VTL,因为AWS增加了对它的支持。我使用的是AWS SAM模板,下面是它的样子

MyResolver:
Type: AWS::AppSync::Resolver
DependsOn: AppSyncSchema
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: addUser
DataSourceName: !GetAtt UsersTableDataSource.Name
RequestMappingTemplate: |
{
"operation": "PutItem",
"key": util.dynamodb.toMapValues({"userId": ctx.userId, "sortKey": ctx.sortKey}),
"attributeValues": util.dynamodb.toMapValues(ctx),
}
ResponseMappingTemplate: "ctx.result"

但是当我在Appsync控制台查询突变时,我得到以下错误

无法识别的令牌'util':正在等待(JSON字符串,数字,数组,对象或令牌'null', 'true'或'false')

我尝试了几个变化,我传递整个值作为字符串,但它没有工作。

在这个模板映射中我遗漏了什么或做错了什么?

编辑-我更新的答案:

MyResolver:
Type: AWS::AppSync::Resolver
DependsOn: AppSyncSchema
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: addUser
DataSourceName: !GetAtt UsersTableDataSource.Name
Code: |
import { util } from '@aws-appsync/utils';
export function request(ctx) {
return {
operation: 'PutItem',
key: util.dynamodb.toMapValues({"userId": 
ctx.userId, "sortkey": ctx.sortKey}),
attributeValues: util.dynamodb.toMapValues(ctx),
}
}

export function response(ctx) {
const { error, result } = ctx;
if (error) {
return util.appendError(error.message, error.type, result);
}
return ctx.result;
}
Runtime:
Name: APPSYNC_JS
RuntimeVersion: 1.0.0

认为JS解析器目前只能在管道解析器中使用,而不能在单元解析器中使用。此外,在管道解析器中,您可能需要以类似于下面的方式添加运行时。如果没有它,可能会默认为VTL。注意,没有在SAM中进行测试,只是根据Cloudformation和CDK文档进行猜测。

AWS: AppSync:解析器

AWS:: AppSync::解析器运行时

MyResolver:
Type: AWS::AppSync::Resolver
DependsOn: AppSyncSchema
Properties:
ApiId: !GetAtt AppSyncApi.ApiId
TypeName: Mutation
FieldName: addUser
DataSourceName: !GetAtt UsersTableDataSource.Name
RequestMappingTemplate: |
{
"operation": "PutItem",
"key": util.dynamodb.toMapValues({"userId": ctx.noteId, "selection": sk}),
"attributeValues": util.dynamodb.toMapValues(ctx),
}
ResponseMappingTemplate: "ctx.result"
Runtime:
Name: APPSYNC_JS
RuntimeVersion: 1.0.0

最新更新