Apollo GraphQL Lambda 处理程序在访问 http 端点时无法读取 undefined 的属性“method”

Apollo GraphQL Lambda Handler Cannot read property 'method' of undefined when accessing http endpoint

提问人:chaitanya Kiran 提问时间:11/17/2023 最后编辑:John Rotensteinchaitanya Kiran 更新时间:11/18/2023 访问量:22

问:

我的代码,

索引.js ........................


import { ApolloServer } from '@apollo/server';
import { startServerAndCreateLambdaHandler, handlers, middleware } from '@as-integrations/aws-lambda';

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


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

const server = new ApolloServer({
  typeDefs,
  resolvers,
});


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


export { graphqlHandler as handler  }

服务器.yml ........................

service: apollo-lambda
provider:
  name: aws
  runtime: nodejs16.x
  httpApi:
    cors: true
functions:
  graphql:
    handler: src/server.handler
    events:
      - httpApi:
          path: /cep
          method: POST
      - httpApi:
          path: /cep
          method: GET
plugins:
  - serverless-offline

package.json .........................

  "dependencies": {
    "@apollo/server": "^4.9.5",
    "@as-integrations/aws-lambda": "^3.1.0",
    "graphql": "^16.8.1"
  },
  "devDependencies": {
    "@types/node": "^20.8.10",
    "serverless-offline": "^13.2.0"
  }

部署到 AWS Lambda 并点击终端节点后,抛出以下错误。代码打包为 .zip 文件,并通过 AWS 控制台手动部署到 Lambda。

Cannot read properties of undefined (reading 'method')

可能是什么问题?

亚马逊网络服务 aws-lambda cors apollo-server

评论


答:

0赞 Michel Floyd 11/17/2023 #1

我发现 es6 导出与无服务器很敏感。尝试:

exports.handler = graphqlHandler

index.js