Apollo-server 4 with expressMiddleware

Hello!

I’m trying to setup meteor with apollo-server 4 with the expressMiddleware:

  1. mkdir apollo4 && cd apollo4
  2. meteor create --apollo .
  3. replace apollo-express-server with @apollo/server and @apollo/server/express4, and add json body-parser:
import { json } from "body-parser";
import { ApolloServer } from "@apollo/server";
import { expressMiddleware } from "@apollo/server/express4";
  1. change the startApolloServer function to this:
export async function startApolloServer() {
  await server.start();
  const app = WebApp.connectHandlers;

  app.use("/graphql", json(), expressMiddleware(server));
}

But i’m getting a 405 Method not allowed. Has anyone figured out how to use apollo4 with meteor?

It does work when using a separate express server:

    const app = express();
    const httpServer = http.createServer(app);
    const apolloServer = new ApolloServer({
        typeDefs,
        resolvers,
        introspection: Meteor.isDevelopment,
        plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
    });

    await apolloServer.start();

    app.use(
        '/graphql',
        bodyParser.json(),
        expressMiddleware(apolloServer),
    );

    await new Promise((resolve) => httpServer.listen({ port: 3010 }, resolve));

Is there a relevant downside when using an additional express server, other than needing another port?

That was discussed here

1 Like

Thank you @rjdavid ! That solves it for me!

The Apollo skeleton has been updated for Meteor 2.11, see the PR to see how you can adjust your current Meteor app to do the same:

2 Likes