apollo-server-integrations / apollo-server-integration-aws-lambda

An integration to use AWS Lambda as a hosting service with Apollo Server
MIT License
46 stars 9 forks source link

ApolloServer from `async` function #172

Open lanesawyer opened 3 months ago

lanesawyer commented 3 months ago

Hi there, I'm trying to upgrade to Apollo v4 and this lambda integration is the last piece that has me stumped.

Our Apollo server has to deal with a promise on the schema (we're stitching our local schema together with a remote one):

new ApolloServer({
    schema: await this.getExecutableSchema(),
    plugins: [telemetryPlugin, playground],
});

Due to the Promise on building that server, for Apollo v3 we are set up like this:

export const handler: Handler = async (event, context, callback) => {
    const config = {
        telemetryLogger: new TelemetryConsoleLogger(),
    };
    const graphqlServer = await new BffGraphqlServer().getLambdaServer(config);
    const serverHandler = graphqlServer.createHandler();
    return serverHandler(event, context, callback);
};

In an ideal v4 world using this plugin, I'd love to be able to pass a Promise<ApolloServer> directly into the main function:

export default startServerAndCreateLambdaHandler(new BffGraphqlServer().getLambdaServer(config), handlers.createAPIGatewayProxyEventV2RequestHandler());

Or, that not being an option, have a way to manually create the handler, do my await, then call this server integration's code:

export const handler = async (event, context, callback) => {
    const config = {
        telemetryLogger: new TelemetryConsoleLogger(),
    };
    const graphqlServer = await new BffGraphqlServer().getServer(config);
    // But where to feed in event, context, and callback??
    return startServerAndCreateLambdaHandler(graphqlServer, handlers.createAPIGatewayProxyEventV2RequestHandler());
};

My last-ditch item will be to enable top-level await on Node for the whole project so I could wait for the server to be created, but we're not in a great position to do that with the state of our codebase at the moment.

I'm hoping I'm simply overlooking something in the documentation, but if not, this would be a feature request to support async server creation.

Thanks in advance for the help!

lanesawyer commented 3 months ago

Dug around in the codebase and tried calling the startServerAndCreateLambdaHandler function that's returned, passing in event, context, and callback.

export const handler = async (event, context, callback) => {
    const config = {
        telemetryLogger: new TelemetryConsoleLogger(),
    };
    const graphqlServer = await new BffGraphqlServer().getServer(config);
    return startServerAndCreateLambdaHandler(graphqlServer, handlers.createAPIGatewayProxyEventV2RequestHandler())(event, context, callback);
};

That at least built successfully! I got a ServerStarted event from my telemetry logger, but when I try to load the playground I just get the following error in my browser, with no errors being logged to the console running the local serverless function:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

Cannot read properties of undefined (reading 'method')`

Not sure if that's because I'm using it in a weird way or something else is wrong in my lambda setup (the Docker version we have with all the Apollo v4 code currently works perfectly, it's just the lambda having issues).

lanesawyer commented 3 months ago

Alright, took out the async part of the ApolloServer and did it according to the docs for this repo and still get that error. Means something else is up with my setup. I'll go dig into that!

Regardless, it would be nice to have an example on this tool for what to do when you have an async ApolloServer!