Prz*_*ach 4 aws-lambda graphql apollo-server
我正在尝试在我的 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,
});
Run Code Online (Sandbox Code Playgroud)
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
}
}
]
});
Run Code Online (Sandbox Code Playgroud)
当我使用适当的查询发布到端点时,出现以下错误:
{
"statusCode": 400,
"body": "Cannot read property 'method' of undefined"
}
Run Code Online (Sandbox Code Playgroud)
我看到 lambda 内的日志记录符合预期,并且我可以确认 startServerAndCreateLambdaHandler() 中的“结果”中返回了错误。此代码基于 @as-integrations/aws-lambda 库的示例。我不明白为什么这会失败。
Prz*_*ach 11
需要使用:
handlers.createAPIGatewayProxyEventRequestHandler()
Run Code Online (Sandbox Code Playgroud)
代替:
handlers.createAPIGatewayProxyEventV2RequestHandler()
Run Code Online (Sandbox Code Playgroud)
所以最终的代码是:
export const handler = startServerAndCreateLambdaHandler(
server,
handlers.createAPIGatewayProxyEventRequestHandler(),
{
middleware: [
async (event) => {
console.log('###? received event=' + JSON.stringify(event))
}
]
}
);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
997 次 |
| 最近记录: |