You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将现有AWS AppSync管道解析器导入CDK并添加新函数?

问题

我通过AWS Amplify创建了一个AppSync API,该API包含一个名为createSpeaker的突变,它已关联至一个管道解析器,Amplify为该解析器生成了4个AppSync函数。

我希望修改此解析器,添加一个额外的AppSync函数,当用户调用createSpeaker突变时触发该函数,用于在独立数据库中自动创建日志。

目前我卡在将新AppSync函数添加到解析器管道的步骤,因为无法弄清如何将现有的管道解析器导入CDK。我已在新CDK应用中完成了API导入等操作,代码如下:

/** importing the existing api */
const api = appsync.GraphqlApi.fromGraphqlApiAttributes(this, 'api', {
  graphqlApiId: props.appSyncId,
});

/** adding a lambda fn created earlier in this code as data source */
const lambdaDataSource = api.addLambdaDataSource(props.appName + ' - ' + props.appEnv + '-lambdaLoggerDataSource', props.createLoggLambdaFn);

/** 
 * creating a new Appsync fn with js as runtime, that will invoke the lambda function (which is supposed to handle the logic)
 * I want to connect this appSync fn to other resolvers, so that I can log data to DynamoDB
 */
const appSyncFunction = new appsync.AppsyncFunction(this, props.appName + ' - ' + props.appEnv + '-CreateLoggResolverFn', {
  api: api,
  runtime: appsync.FunctionRuntime.JS_1_0_0,
  dataSource: lambdaDataSource,
  name: props.appName + ' - ' + props.appEnv + '-CreateLoggResolverFn',
  code: appsync.Code.fromAsset('lib/appsync/resolver'),
  description: 'This function should be part of other resolvers to logg data to dynamoDB',
});

/** Next step, where I am stuck, is how to import the resolver from the api and add appSyncFunction to it */

我已通过控制台成功实现此逻辑,但希望在创建新环境时自动化该流程。最终我希望将应用中的所有突变都关联此日志功能(兼具安全与实用查询特性),目前这是独立的CDK应用,后续也会将其整合到Amplify自定义功能中。请问该如何解决导入现有解析器并添加新函数的问题?


解决方案

CDK目前不支持直接导入现有AppSync解析器进行修改,你需要通过重新定义管道解析器的方式,将原有Amplify生成的函数与新的日志函数组合起来,覆盖原有的createSpeaker突变解析器。具体步骤如下:

  1. 导入Amplify生成的原有AppSync函数
    先通过AWS控制台的AppSync函数列表,或执行AWS CLI命令aws appsync list-functions --api-id <你的API ID>,获取Amplify为createSpeaker生成的4个函数的ID。

    在CDK中使用appsync.AppsyncFunction.fromAppsyncFunctionAttributes方法逐个导入这些函数:

    // 示例:导入第一个Amplify生成的函数,替换为实际函数ID
    const existingFn1 = appsync.AppsyncFunction.fromAppsyncFunctionAttributes(this, 'ExistingFn1', {
      functionId: 'xxxxxx',
      api: api,
    });
    
    // 重复导入剩下3个函数
    const existingFn2 = appsync.AppsyncFunction.fromAppsyncFunctionAttributes(this, 'ExistingFn2', {
      functionId: 'yyyyyy',
      api: api,
    });
    
    const existingFn3 = appsync.AppsyncFunction.fromAppsyncFunctionAttributes(this, 'ExistingFn3', {
      functionId: 'zzzzzz',
      api: api,
    });
    
    const existingFn4 = appsync.AppsyncFunction.fromAppsyncFunctionAttributes(this, 'ExistingFn4', {
      functionId: 'aaaaaa',
      api: api,
    });
    
  2. 创建新的管道解析器并组合所有函数
    使用api.addResolver方法重新定义createSpeaker突变的管道解析器,将原有函数和新的日志函数按需求顺序加入pipelineConfig。如果日志需要在数据操作完成后执行,就把日志函数放在最后;若需前置校验则放在最前面。

    api.addResolver('CreateSpeakerPipelineResolver', {
      typeName: 'Mutation',
      fieldName: 'createSpeaker',
      pipelineConfig: [existingFn1, existingFn2, existingFn3, existingFn4, appSyncFunction],
      // 复用Amplify默认管道代码或使用空逻辑代码,因为核心逻辑由各个子函数处理
      code: appsync.Code.fromInline(`
        export function request(ctx) { return {}; }
        export function response(ctx) { return ctx.prev.result; }
      `),
      runtime: appsync.FunctionRuntime.JS_1_0_0,
    });
    
  3. 批量为所有突变添加日志功能
    若要给所有突变统一添加日志,可维护一个突变字段列表,遍历列表批量生成解析器:

    // 替换为实际需要添加日志的突变字段
    const mutationFields = ['createSpeaker', 'updateSpeaker', 'deleteSpeaker'];
    
    mutationFields.forEach(fieldName => {
      // 导入当前突变对应的原有管道函数
      const existingFns = [/* 该突变的原有函数数组 */];
    
      api.addResolver(`PipelineResolver-${fieldName}`, {
        typeName: 'Mutation',
        fieldName: fieldName,
        pipelineConfig: [...existingFns, appSyncFunction],
        code: appsync.Code.fromInline(`
          export function request(ctx) { return {}; }
          export function response(ctx) { return ctx.prev.result; }
        `),
        runtime: appsync.FunctionRuntime.JS_1_0_0,
      });
    });
    
  4. 后续整合到Amplify的建议
    当需要将此逻辑整合到Amplify时,可将CDK代码封装为Amplify自定义资源,或使用Amplify的override.ts文件扩展AppSync配置,减少环境差异的手动维护成本。

内容的提问来源于stack exchange,提问作者Engam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 20:22:48