ApolloServer:createAPIGatewayProxyEventV2RequestHandler -- 如何设置CORS?

问题描述 投票:0回答:1

我将一个项目拆分为客户端/服务器子项目,所以现在我必须处理在本地主机上不同端口下运行的客户端和服务器之间的 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 或标头的信息。

aws-lambda graphql cors apollo-server
1个回答
0
投票

看起来这个问题已在 GitHub 存储库的问题选项卡中得到解决。

© www.soinside.com 2019 - 2024. All rights reserved.