我将一个项目拆分为客户端/服务器子项目,所以现在我必须处理在本地主机上不同端口下运行的客户端和服务器之间的 CORS 问题。我正在使用 ApolloServer 4,找不到任何用于为 AWS lambda 服务器设置 Access-Control-Allow-Origin 标头的文档。这是设置:
import { ApolloServer } from "@apollo/server";
import { startServerAndCreateLambdaHandler, handlers } from '@as-integrations/aws-lambda';
import typeDefs from "./schema.js"
import resolvers from "./resolvers.js"
import crypto from 'crypto'
const getUser = (token) => {
// security stuff
}
const production = process.env.NODE_ENV === 'production'
const server = new ApolloServer({
typeDefs,
resolvers,
introspection: !production,
playground: !production,
debug: !production,
context: ({ event: { headers } }) => {
const token = headers.authorization || '';
const user = getUser(token);
return { user };
},
});
const graphqlHandler = startServerAndCreateLambdaHandler(
server,
// We will be using the Proxy V2 handler
handlers.createAPIGatewayProxyEventV2RequestHandler(),
);
export { graphqlHandler as handler }
Apollo 有一个使用运行快速中间件的独立服务器来配置 CORS 的示例,但我找不到上面的 lambda 设置的类似示例。我搜索了 createAPIGatewayProxyEventV2RequestHandler 的代码,但没有找到任何有关 CORS 或标头的信息。
看起来这个问题已在 GitHub 存储库的问题选项卡中得到解决。