2

Has anyone succesfully implemented an apollo express server (as the gateway) together with a federation of graphQL servers?

My means of authentication is present via a REST endpoint on my gateway so I am running an apollo-express server as a top layer, but when trying to query the data it doesn't return anything from the other services. When I switch to having the gateway operate as purely an Apollo server, then it functions and can get data from the services belonging to the apollo federation connected to it. But this means I have to disregard my implementation of authentication and other REST endpoints I am using with the service.

Current setup:

const main = async () => {

    try {

    const app = express();

    app.use(function(_req, res, next) {
      res.header("Access-Control-Allow-Origin", `${config.frontend_url}`);
      res.header('Access-Control-Allow-Methods', "GET,HEAD,OPTIONS,POST,PUT");
      res.header("Access-Control-Allow-Credentials", "true");
      res.header('Access-Control-Allow-Headers', "Access-Control-Allow-Origin, Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization");
      next();
    });


    app.use(cookieParser());
    app.use(cookieSession({
      maxAge: 24 * 60 * 60 * 1000,
      keys: [config.cookieKey]
    }));


    app.use('/auth', loginRouter);


    const serviceList = [
      {name: "service", url: config.service_url + "/graphql"},
      {name: "service", url: config.service_url + "/graphql"},
    ]

    const gateway = new ApolloGateway({
      serviceList,
    });

    const { schema, executor } = await gateway.load();

    // const schema = await createSchema();

    app.use(
      '/graphql',
      checkToken,
      graphqlHTTP(req => ({
        schema,
        graphiql: true,
        context: req
      }))
    );

    app.use('/', ((_req, res) => {
      res.sendStatus(200);
    }));


    const apolloServer = new ApolloServer({ schema, executor, playground: true, tracing: false , subscriptions: false}) as any;
    apolloServer.applyMiddleware({ app });
    app.listen(config.port, () => {
      console.log(` Server ready at api.backoffice.myos.co:${config.port}${apolloServer.graphqlPath}`)
    });

  } catch (e) {
    Logger.error(">>> >>> >>> createSchema() error:", e);
  }

};
BURGERFLIPPER101
  • 1,231
  • 4
  • 24
  • 41

0 Answers0