Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApolloServer from async function #172

Open
lanesawyer opened this issue May 17, 2024 · 2 comments
Open

ApolloServer from async function #172

lanesawyer opened this issue May 17, 2024 · 2 comments

Comments

@lanesawyer
Copy link

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
Copy link
Author

lanesawyer commented May 20, 2024

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant