【发布时间】:2022-12-18 18:25:59
【问题描述】:
我使用 GraphQL Gateway 与 GraphQL Federation Microservices 集成。 但出于某种原因我使用了一些 Rest API 代码。喜欢(刷新令牌,上传图片休息)
问题是:如何与来自 graphql 网关的其他服务的 Rest API 通信,以及如何将上下文发送到控制器(rest api)服务器。
import { IntrospectAndCompose, RemoteGraphQLDataSource } from '@apollo/gateway';
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '@nestjs/apollo';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { AppController } from './app.controller';
@Module({
imports: [
GraphQLModule.forRoot<ApolloGatewayDriverConfig>({
driver: ApolloGatewayDriver,
server: {
// ... Apollo server options
context: ({ req, res }) => ({
authorization:req.headers.authorization,
req,
res,
url: req.protocol + '://' + req.headers.host,
}),
cors: true,
},
gateway: {
buildService({ name, url }) {
return new RemoteGraphQLDataSource({
url,
willSendRequest({ request, context }) {
request.http.headers.set('authorization',context['authorization'] );
}
});
},
supergraphSdl: new IntrospectAndCompose({
subgraphs: [
{ name: 'Service1', url: 'http://localhost:3001/graphql' },
{ name: 'Service2', url: 'http://localhost:3002/graphql' },
{ name: 'Service3' , url: 'http://localhost:3003/graphql' }
],
}),
},
}),
],controllers:[AppController]
})
export class AppModule { }
注意:如果我从 Url 中删除 '/graphql' 以访问原始 url ,它会给我错误 [Couldn't load service definitions for service1] 。
此代码适用于 GraphQL,但不适用于 Rest。
服务器:NestJS。
谢谢..
【问题讨论】:
-
你的问题不清楚。你问如何通过 GQL 网关发送休息电话?
-
@KrishanthaDinesh 是的,没错
-
我认为首先你应该将其余的 api 包装在一个子图中(Apollo RESTDataSource),然后从 apollo 网关调用它
标签: rest microservices nestjs api-gateway graphql-federation