Skip to content

ApolloServer from async function #172

Open
@lanesawyer

Description

@lanesawyer

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions