我正在尝试在我的 AWS lambda 中运行 Apollo GraphQL 服务器。我正在使用来自的库here https://www.npmjs.com/package/@as-integrations/aws-lambda。我还使用 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
}
}
]
});
当我使用适当的查询发布到端点时,出现以下错误:
{
"statusCode": 400,
"body": "Cannot read property 'method' of undefined"
}
我看到 lambda 中的日志记录符合预期,并且我可以确认 startServerAndCreateLambdaHandler() 中的“结果”中返回了错误。此代码基于 @as-integrations/aws-lambda 库的示例。我不明白为什么这会失败。