这个问题是由于在使用Apollo Server Lambda时,无法识别事件源导致的。为了解决这个问题,需要在Lambda的事件映射中明确指定事件源。
请参阅以下示例代码:
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ event, context }) => {
// Determine event source and pass it to context
const eventSource =
event && event.headers && event.headers['X-Event-Source'];
return { eventSource, context };
},
});
exports.handler = server.createHandler({
cors: {
origin: '*',
credentials: true,
},
// Explicitly map event source to "AWS_API_GATEWAY"
// if using API Gateway as event source
eventSourceName: 'AWS_API_GATEWAY',
});
在上面的代码中,我们在Context中传递了事件源。为了显式指定事件源,我们也在Lambda的事件映射中指定事件源名称。
这样就可以正常使用Apollo Server Lambda了,而且不再出现事件源无法识别的问题。
上一篇:apollo-server-lambda: 无法读取未定义的属性 'content-type'。
下一篇:apollo-server-micro:响应缺少头部字段'access-control-allow-methods:POST'