如何输入从 Amplify GraphQL 调用发送的无服务器解析器的事件

问题描述 投票:0回答:1

我正在尝试输入来自 AWS Amplify/NextJS 前端的事件对象。它通过 Amplify/GraphQL 发送并在 NodeJS AWS Lambda 中接收。

import { AppSyncResolverEvent } from "aws-lambda";
中包含的打字稿接口 - 但这与正在接收的事件对象不太匹配。有人可以告诉我我做错了什么吗?

详情:

我使用

aws-amplify
@aws-amplify/api-graphql
包成功从前端 (NextJS) 触发了一个事件:

import { graphqlOperation, GraphQLResult } from "@aws-amplify/api-graphql"; // "version": "2.3.11",
import { API } from "aws-amplify"; // "aws-amplify": "^4.3.28",

...
    // Retrieve schemas from Lambda
    const response = (await API.graphql<{ myAppSyncFunctionResponse: any }>(
      graphqlOperation(myAppSyncFunction),
      { input: "foo" }
    )) as GraphQLResult<{ myAppSyncFunctionResponse: string }>;

我的 Serverless Lambda 已成功接收该事件,类似于以下内容:

{
  typeName: "Mutation",
  fieldName: "myAppSyncFunction",
  arguments: { input: { ... } },
  identity: {...},
  source: ...,
  request: ...,
  prev:...
}

但是,当我尝试在我的nodejs Lambda环境中使用

aws-lambda
时:

import { AppSyncResolverEvent } from "aws-lambda";

export async function eventBridgeResolver(
  event: AppSyncResolverEvent
) {...}

AppSyncResolverEvent
包含不同的属性:

// node_modules/@types/aws-lambda/trigger/appsync-resolver.d.ts
{
 arguments: TArguments;
    identity?: AppSyncIdentity;
    source: TSource;
    request: {
        headers: AppSyncResolverEventHeaders;
    };
    info: {
        selectionSetList: string[];
        selectionSetGraphQL: string;
        parentTypeName: string;
        fieldName: string;
        variables: { [key: string]: any };
    };
    prev: { result: { [key: string]: any } } | null;
    stash: { [key: string]: any };
}
amazon-web-services aws-lambda graphql aws-amplify serverless
1个回答
0
投票

我通过基于 aws-lambda 中的 AppSyncResolverEvent 接口创建自己的接口解决了这个问题

import {
  type AppSyncIdentity,
  type AppSyncResolverEventHeaders,
} from 'aws-lambda'

/**
 * See https://docs.amplify.aws/cli/graphql/custom-business-logic/#lambda-function-resolver
 *
 * @param TArguments type of the arguments
 * @param TSource type of the source
 */
export interface AmplifyResolverEvent<
  TArguments,
  TSource = Record<string, any> | null,
> {
  /**
   * typeName: The name of the parent object type of the field being resolved.
   */
  typeName: string
  /**
   * fieldName: The name of the field being resolved.
   */
  fieldName: string
  /**
   * arguments: A map containing the arguments passed to the field being resolved.
   */
  arguments: TArguments
  /**
   * identity: A map containing identity information for the request. Contains a nested key 'claims' that will contains the JWT claims if they exist.
   */
  identity?: AppSyncIdentity
  /**
   * source: When resolving a nested field in a query, the source contains parent value at runtime. For example when resolving Post.comments, the source will be the Post object.
   */
  source: TSource
  /**
   * request:   The AppSync request object. Contains header information.
   */
  request: {
    headers: AppSyncResolverEventHeaders
    /** The API's custom domain if used for the request. */
    domainName: string | null
  }
  /**
   * prev: When using pipeline resolvers, this contains the object returned by the previous function. You can return the previous value for auditing use cases.
   */
  prev: { result: Record<string, any> } | null
}

我通过查看 aws-lambda 中的接口 AppSyncResolverEvent 收集了这一点,其中特别提到了维护者的注释:其中一些属性与 Amplify 解析器共享。可能值得检查此处的更改是否也适用于那里。

在 AppSync 映射模板解析器中,我们可以看到此类型是如何构建的:

映射模板之前:

## [Start] Stash resolver specific context.. **
$util.qr($ctx.stash.put("typeName", "Mutation"))
$util.qr($ctx.stash.put("fieldName", "executeSearch"))
{}
## [End] Stash resolver specific context.. **

请求映射模板:

## [Start] Invoke AWS Lambda data source: ExecuteSearchLambdaDataSource. **
{
  "version": "2018-05-29",
  "operation": "Invoke",
  "payload": {
      "typeName": $util.toJson($ctx.stash.get("typeName")),
      "fieldName": $util.toJson($ctx.stash.get("fieldName")),
      "arguments": $util.toJson($ctx.arguments),
      "identity": $util.toJson($ctx.identity),
      "source": $util.toJson($ctx.source),
      "request": $util.toJson($ctx.request),
      "prev": $util.toJson($ctx.prev)
  }
}
## [End] Invoke AWS Lambda data source: ExecuteSearchLambdaDataSource. **

我已将此信息发布到 https://github.com/aws-amplify/amplify-js/issues/11113#issuecomment-1748041303,因此希望很快就会被合并。

© www.soinside.com 2019 - 2024. All rights reserved.