如何将 apollo 联合网关与中间件一起使用
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想在网关和子图之间共享令牌,
可以通过以下方式完成。
文档网址
If you want to share tokens between the gateway and the subgraph,
you can do it in the following way.
Document url