ApolloServer: createAPIGatewayProxyEventV2RequestHandler — how to set up CORS?

ApolloServer: createAPIGatewayProxyEventV2RequestHandler — how to set up CORS?


0

I’m splitting a project into client/server sub projects, so now I have to deal with CORS issues between the client and server running under different ports on localhost. I’m using ApolloServer 4, and can’t find any documentation for setting the Access-Control-Allow-Origin header for an AWS lambda server. This is the setup:

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 has an example using a standalone server running express middleware that configures CORS, but I can’t find a similar example of the lambda setup above. I’ve searched through the code for createAPIGatewayProxyEventV2RequestHandler, but didn’t find anything regarding CORS or headers.

1 Answer
1


0

Looks like this problem is addressed in the Issues tab of the GitHub repo.



Leave a Reply

Your email address will not be published. Required fields are marked *