Apollo 4, CORS, and aws-lambda handler

I’m trying to set up a lambda function server separate from the web client, which means having to deal with CORS. Scenario:

  1. server running on localhost:8881
  2. client running on localhost:8888

Relevant server code:

import { ApolloServer } from "@apollo/server";
import {
    startServerAndCreateLambdaHandler,
    handlers,
} from "@as-integrations/aws-lambda";

const server = new ApolloServer({
    typeDefs,
    resolvers,
    introspection: !production,
    playground: !production,
    debug: !production,
});

const graphqlHandler = startServerAndCreateLambdaHandler(
    server,
    requestHandler,
    {
        middleware: [corsMiddleware],
    }
);

The corsMiddleware is set up according to a solution provided here, but I have not had success in getting it to work.

const allowedOrigins = ["http://localhost:8888"];
// const requestHandler = handlers.createAPIGatewayProxyEventV2RequestHandler();
const requestHandler = handlers.createAPIGatewayProxyEventRequestHandler();

const corsMiddleware = async (event) => {
    const origin = event.headers.origin;

    console.dir(event, { depth: 5 });

    if (origin && allowedOrigins.includes(origin)) {
        return (result) => {
            result.headers = {
                ...result.headers,
                "Access-Control-Allow-Origin": origin,
                Vary: "Origin",
            };

            console.dir(result, { depth: 5 });
            return Promise.resolve();
        };
    }

    return () => Promise.resolve();
};

CORS support is pretty important if you want to set up a GraphQL server, and I’m surprised the Apollo Docs don’t address it with a working example.