【问题标题】:How to Integrate Rest Api with GraphQL Gateway and send Context如何将 Rest Api 与 GraphQL 网关集成并发送上下文
【发布时间】: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


【解决方案1】:

你的问题不是很清楚,但让我试一试。

您可以在现有网关之上使用 WunderGraph 之类的服务,或者您可以创建一个新网关并获取 GraphQL 联合微服务。然后您可以introspect the REST API并将其接收到您的网关中。 .

【讨论】:

    猜你喜欢
    • 2019-07-14
    • 2015-01-16
    • 2015-02-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2022-12-13
    • 2017-04-08
    • 2017-08-01
    相关资源
    最近更新 更多