对于使用 Cfn<> Cloud Front CDK 构建的 AWS AppSync CDK 项目,我们需要向 API 添加新的 JS 解析器,并逐步淘汰 VTL 解析器,如下所示:
let resolver = new CfnResolver(this, `${r.typeName}_${r.fieldName}`, {
apiId: graphql.attrApiId,
typeName: r.typeName,
fieldName: r.fieldName,
dataSourceName: r.dataSourceName,
requestMappingTemplate: readVtl(reqPath, contextVariables),
responseMappingTemplate: readVtl(resPath, contextVariables),
});
这里的最新示例AWS CDK示例都直接以这种风格使用以下AppSync CDK。
new appsync.Resolver(this, 'pipeline-resolver-create-posts', {
api,
typeName: 'Mutation',
fieldName: 'createPost',
code: appsync.Code.fromInline(`
export function request(ctx) {
return {};
}
export function response(ctx) {
return ctx.prev.result;
}
`),
runtime: appsync.FunctionRuntime.JS_1_0_0,
pipelineConfig: [add_func_2],
});
我正在努力让 CfnResolver 接受新的 JS 解析器。我已尝试了尽可能多的以下变体,并在此处查找 AWS 文档
let resolver = new CfnResolver(this, `${r.typeName}_${r.fieldName}`, {
apiId: graphql.attrApiId,
typeName: r.typeName,
fieldName: r.fieldName,
dataSourceName: r.dataSourceName,
code: readJs(reqPath, contextVariables),
runtime: CfnResolver.AppSyncRunTime.JS_1_0_0 <-- DOESNT WORK
});
有人可以建议我做错了什么,或者我是否应该放弃 Cfn CDK 并转向直接 AppSync CDK?非常感谢。
您的
runtime
中的 CfnResolver
语法不正确。使用 FunctionRuntime 实用程序类一次性设置运行时名称 (APPSYNC_JS
) 和版本 (1.0.0
):
runtime: FunctionRuntime.JS_1_0_0,
正如 @gshpychka 在评论中所说,使用 L2 Resolver 构造几乎总是更好。
Resolver
构造函数在底层创建了 L1 CfnResolver
资源,但提供了更好的 DX。例如,您可以将解析器代码放在单独的文件中,并使用 Code.fromAsset('path/to/code.js') 引用它,这样可以更好地支持 linting、testing 和 Typescript .