繁体   English   中英

Apollo GraphQL Lambda 处理程序无法读取未定义的属性“方法”

[英]Apollo GraphQL Lambda Handler Cannot read property 'method' of undefined

我正在尝试在我的 AWS lambda 中运行 Apollo GraphQL 服务器。我正在使用此处的库。 我还使用 CDK 部署我的 lambda 和 REST API 网关。

我的基础设施如下:

const helloFunction = new NodejsFunction(this, 'lambda', {
    entry: path.join(__dirname, "lambda.ts"),
    handler: "handler"
});

new LambdaRestApi(this, 'apigw', {
    handler: helloFunction,
});

lambda实现如下:

const typeDefs = `#graphql
  type Query {
  hello: String
}`;

const resolvers = {
    Query: {
        hello: () => 'world',
    },
};


const server = new ApolloServer({
    typeDefs,
    resolvers,
    introspection: true,
})

console.log('###? running lambda')

export const handler = startServerAndCreateLambdaHandler(
    server,
    handlers.createAPIGatewayProxyEventV2RequestHandler(), {
        middleware: [
            async (event) => {
                console.log('###? received event=' + JSON.stringify(event, null, 2))
                return async (result) => {
                    console.log(("###? result=" + JSON.stringify(result, null, 2)))
                    result
                }
            }
        ]
    });

当我使用适当的查询 POST 到我的端点时,我收到此错误:

{
  "statusCode": 400,
  "body": "Cannot read property 'method' of undefined"
}

我按预期在 lambda 中看到了我的日志记录,并且我可以确认在 startServerAndCreateLambdaHandler() 中的“结果”中返回了错误。 此代码基于 @as-integrations/aws-lambda 库的示例。 我不明白为什么这会失败。

需要使用:

handlers.createAPIGatewayProxyEventRequestHandler()

代替:

 handlers.createAPIGatewayProxyEventV2RequestHandler()

所以最终代码是:

export const handler = startServerAndCreateLambdaHandler(
    server,
    handlers.createAPIGatewayProxyEventRequestHandler(),
    {
      middleware: [
        async (event) => {
          console.log('###? received event=' + JSON.stringify(event))
        }
      ]
  }
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM