如何将 apollo 联合网关与中间件一起使用

发布于 2025-01-16 14:42:01 字数 1052 浏览 3 评论 0原文

我正在使用 apollo federation + typescript 来实现带有子图的 graphql 服务器。目前我正在开发网关,我想对其应用中间件,它将执行令牌交换功能。问题是我无法运行我的网关。这是测试代码。

async function startGateway(port: number) {
    const app = express();
    const httpServer = http.createServer(app);

    app.use(cors({
        origin: '*',
        credentials: true,
        exposedHeaders: ['token']
    }));
    app.use(jwtMiddleware)

    const gateway = new ApolloGateway({
        supergraphSdl: new IntrospectAndCompose({
            subgraphs: [
                { name: 'subgraph', url: 'http://localhost:8081'}
            ]
        })
    });
        
    const server = new ApolloServer({
        gateway,
        plugins: [ ApolloServerPluginDrainHttpServer({ httpServer })]
    });

    await server.start();

    server.applyMiddleware({ app });

    return new Promise((resolve, reject) => {
        httpServer.listen(port)
        .once('listening', resolve)
        .once('error', reject);
    })
  }

当我运行代码时,我没有收到任何错误或警告,但我无法通过 graphql 客户端连接到我的网关。问题是什么以及如何解决?先感谢您。

I'm using apollo federation + typescript to implement a graphql server with subgraphs. Currently I'm working on the gateway, and I want to apply middleware to it, which will perform a token exchange functionality. The problem is that I can't get my gateway running. Here is the test code.

async function startGateway(port: number) {
    const app = express();
    const httpServer = http.createServer(app);

    app.use(cors({
        origin: '*',
        credentials: true,
        exposedHeaders: ['token']
    }));
    app.use(jwtMiddleware)

    const gateway = new ApolloGateway({
        supergraphSdl: new IntrospectAndCompose({
            subgraphs: [
                { name: 'subgraph', url: 'http://localhost:8081'}
            ]
        })
    });
        
    const server = new ApolloServer({
        gateway,
        plugins: [ ApolloServerPluginDrainHttpServer({ httpServer })]
    });

    await server.start();

    server.applyMiddleware({ app });

    return new Promise((resolve, reject) => {
        httpServer.listen(port)
        .once('listening', resolve)
        .once('error', reject);
    })
  }

when I run the code I get no errors or warnings, but I cannot connect to my gateway via graphql client. What is the problem and how can It be fixed? Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失而复得 2025-01-23 14:42:01

如果您想在网关和子图之间共享令牌,

可以通过以下方式完成。

class AuthenticatedDataSource extends RemoteGraphQLDataSource {
  willSendRequest({ request, context }) {
    // The header set here is transferred to the subgraph.
    request.http.headers.set('x-user-id', context.userId);
  }
}

const gateway = new ApolloGateway({
  // ...other options...
  buildService({ name, url }) {
    return new AuthenticatedDataSource({ url });
  },
});

文档网址

If you want to share tokens between the gateway and the subgraph,

you can do it in the following way.

class AuthenticatedDataSource extends RemoteGraphQLDataSource {
  willSendRequest({ request, context }) {
    // The header set here is transferred to the subgraph.
    request.http.headers.set('x-user-id', context.userId);
  }
}

const gateway = new ApolloGateway({
  // ...other options...
  buildService({ name, url }) {
    return new AuthenticatedDataSource({ url });
  },
});

Document url

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文