1

有没有人成功地实现了 apollo express 服务器(作为网关)和 graphQL 服务器的联合?

我的身份验证方式是通过网关上的 REST 端点提供的,因此我将 apollo-express 服务器作为顶层运行,但是当尝试查询数据时,它不会从其他服务返回任何内容。当我切换到让网关作为纯粹的 Apollo 服务器运行时,它会运行并可以从属于连接到它的 apollo 联邦的服务中获取数据。但这意味着我必须忽略我在服务中使用的身份验证和其他 REST 端点的实现。

当前设置:

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);
  }

};
4

0 回答 0