apollo-server
apollo-server copied to clipboard
Fastify example provided in the documentation fails to start the server
Literally copy/pasted the example provided in a project with latest fastify 3.x, started the server, process ends right after.
I guess we need to add startApolloServer() to the example?
import { ApolloServer } from 'apollo-server-fastify';
import { ApolloServerPluginDrainHttpServer } from 'apollo-server-core';
import fastify from 'fastify';
function fastifyAppClosePlugin(app) {
return {
async serverWillStart() {
return {
async drainServer() {
await app.close();
},
};
},
};
}
async function startApolloServer(typeDefs, resolvers) {
const app = fastify();
const server = new ApolloServer({
typeDefs,
resolvers,
csrfPrevention: true,
plugins: [
fastifyAppClosePlugin(app),
ApolloServerPluginDrainHttpServer({ httpServer: app.server }),
],
});
await server.start();
app.register(server.createHandler());
await app.listen(4000);
console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`);
}
v16.15.0 ~/r/p/new-backend yarn start
yarn run v1.22.18
$ node server.mjs
✨ Done in 0.52s.
Well, you do need the call the function. The most appropriate way to call an async function from the top level in Node kind of depends on your Node version and how you've configured your environment --- if you're using the newer native ESM support you can do a top level await, and otherwise you need to do some then/catch stuff.
Perhaps the integrations page should have some content somewhere noting that all examples show async functions and providing advice about how to call them in various Node setups?
Hi @gkatsanos.
Have a look at @as-integrations/fastify It is compatiable with Fastify v4 and Apollo Server v4.
The docs for the new Fastify integration do show how to start the server. Also, I think we're getting a bit more comfortable with the idea of showing top-level await in our docs as time moves on.