使用无服务器框架在 YML 中的代码块中使用 Javascript Resolver 时出现 AWS::Appsync:Resolver Cloudformation 错误

2023-12-02

我在使用 Javascript 解析器在无服务器框架中创建 AWS::Appsync:Resolver Cloudformation 时遇到问题。

My Javascript Resolver我附加到的根目录 /src/resolvers/jsResolver.js 中的代码AWS::AppSync::Resolver代码块中的cloudformation。我还在我的 package.json 中安装了 appsync utils 的 npm 插件

import { util } from '@aws-appsync/utils';

 export function request(ctx) {
    const {source, args} = ctx
    return {
    operation: 'Invoke',
    payload: { field: ctx, arguments: args, source },
  };
}

export function response(ctx) {
    util.error("Failed to fetch relatedPosts", "LambdaFailure", ctx.prev.result)
    return ctx.result;
  }

My AWS::AppSync::ResolverCloudformation 位于 YML 文件下方,如果我将其声明为,我也使用代码作为其强制项APPSYNC_JS 运行时

AppSyncJsResolver:
  Type: AWS::AppSync::Resolver 
  Properties:
    ApiId: !GettAtt Graphql.APiId
    Kind: PIPELINE
    Code: ./src/resolvers/jsResolver.js <—- Here my code is breaking up with error contains one or more error
    TypeName: Query
    FieldName: getInfo
    Runtime:
      Name: APPSYNC_JS
      RuntimeVersion: 1.0.0
    PipelineConfig:
      Functions:
        - !GetAtt AppSyncFunction.FunctionId 

我已经按照 AWS Cloudformation 文档尝试了上面的代码,他们在其中提到了 AppsyncAWS::AppSync::Resolver用于使用 Cloudformation 创建 Javascript Resolver,如下属性之一。我已将其包含在我的AWS::AppSync::Resolver

Code  
  The resolver code that contains the request and response functions. When code is 
  used, the runtime is required. The runtime value must be APPSYNC_JS.

Required: No

Type: String

Required: No

Type: String

所以我尝试过这个,但找不到足够的关于 Javascript 解析器的解决方案,所有这些解决方案都适用于特定的 VTL 模板。

使用上面的代码,我的 CloudFormation 构建失败并出现以下错误:发生错误:AppSyncJSResolver - 代码包含一个或多个错误。(服务:AWSAppSync;状态代码:400;错误代码:BadRequestException;请求 ID:0245d64d-...;代理:null)

  • AppSync JS Resolver - 这是我的AWS::AppSync::Resolver在上面的代码中。我有代码属性,这给了我一个错误。我已经通过多个来源进行了验证,并且我没有发现我的任何错误JavaScript 解析器我在其中声明的文件 /src/resolvers/jsResolver.jsAppSync JS 解析器 AWS::AppSync::Resolver在代码属性中,我不确定为什么会收到此错误,任何帮助都会很棒。

为了回答我自己的问题,我通过两种方式解决它。

1.我们可以在 YAML Cloudformation 中编写整个解析器代码代码属性像下面这样。确保您的解析器代码应该在您的 Code 属性内并使用“|”特殊字符(多行代码)在代码属性之后。

AppSyncJsResolver:
  Type: AWS::AppSync::Resolver 
  Properties:
    ApiId: !GettAtt Graphql.APiId
    Kind: PIPELINE
    Code: | 
         import { util } from '@aws-appsync/utils';
         export function request(ctx) {
         const {source, args} = ctx
         return {
         operation: 'Invoke',
         payload: { field: ctx, arguments: args, source },
        };
        }
        export function response(ctx) {
        util.error("Failed to fetch relatedPosts", "LambdaFailure",ctx.prev.result)
        return ctx.result;
        }
    TypeName: Query
    FieldName: getInfo
    Runtime:
      Name: APPSYNC_JS
      RuntimeVersion: 1.0.0
    PipelineConfig:
      Functions:
        - !GetAtt AppSyncFunction.FunctionId

2.如果你想让您的业务逻辑远离 YML文件并将其分开,然后您可以使用代码S3位置javascript 解析器中的属性如下所示。

first 在S3中创建存储桶并将您的 javascript 解析器文件和解析器代码存储在存储桶中。确保您向 appsync 授予足够的 IAM 权限以访问您的 S3 存储桶。

完成上述步骤后,您可以重写 YML Cloudformation,如下所示

AppSyncJsResolver:
  Type: AWS::AppSync::Resolver 
  Properties:
    ApiId: !GettAtt Graphql.APiId
    Kind: PIPELINE
    CodeS3Location:s3://my-bucket-name/my-filename.js
    TypeName: Query
    FieldName: getInfo
    Runtime:
      Name: APPSYNC_JS
      RuntimeVersion: 1.0.0
    PipelineConfig:
      Functions:
        - !GetAtt AppSyncFunction.FunctionId

希望这可以帮助其他人,并为 Javascript Resolver 做出更多贡献,以便其他人更容易找到更复杂的解决方案并获得尽可能多的关于 Javascript Resolver 的资源。感谢@Graham Hesketh 的建议。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用无服务器框架在 YML 中的代码块中使用 Javascript Resolver 时出现 AWS::Appsync:Resolver Cloudformation 错误 的相关文章