首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有可能有部分联邦网关吗?

有可能有部分联邦网关吗?
EN

Stack Overflow用户
提问于 2019-08-01 08:17:26
回答 1查看 1.9K关注 0票数 8

我希望联合服务,但让联邦网关也包含自己的模式和逻辑,这些模式和逻辑将代理REST端点以实现简单性。现在看来,我需要联邦网关服务、联邦graphql服务和其余的<->graphql桥服务。无论如何,在我们的例子中,rest-graphql网关至少暂时可以生活在联邦网关中,以避免不必要的引导和维护。

看起来阿波罗联邦网关有localServiceList,似乎正是为了这个目的。一个示例配置:

代码语言:javascript
复制
const gateway = new ApolloGateway({
    serviceList: [
        { name: "some-service", url: "http://localhost:40001/graph" }
    ],
    localServiceList: [
        { name: "rest-bridge", typeDefs }
    ]
});

但是它并没有做到这一点:如果有localServiceList,它跳过serviceList。

,所以问题是:在阿波罗联邦网关中,这也有可能保存自己的模式和逻辑吗?

EN

回答 1

Stack Overflow用户

发布于 2020-05-06 13:58:42

是的,可以这样做:

代码语言:javascript
复制
import { buildFederatedSchema } from '@apollo/federation';
import {
  ApolloGateway,
  LocalGraphQLDataSource,
  RemoteGraphQLDataSource
} from '@apollo/gateway';
import gql from 'graphql-tag';

const localServices = {
  foo: {
    schema: {
      typeDefs: gql`
        // ...
      `,
      resolvers: {
        // ...
      }
    }
  },
  bar: {
    schema: {
      typeDefs: gql`
        // ...
      `,
      resolvers: {
        // ...
      }
    }
  }
};

const remoteServices = {
  baz: {
    url: 'http://baz.local/graphql'
  },
  qux: {
    url: 'http://qux.local/graphql'
  }
};

const services = {
  ...localServices,
  ...remoteServices
};

// By providing a protocol we trick ApolloGateway into thinking that this is a valid URL;
// otherwise it assumes it's a relative URL, and complains.
const DUMMY_SERVICE_URL = 'https://';

const gateway = new ApolloGateway({
  // We can't use localServiceList and serviceList at the same time,
  // so we pretend the local services are remote, but point the ApolloGateway
  // at LocalGraphQLDataSources instead...
  serviceList: Object.keys(services).map(name => ({
    name,
    url: services[name].url || DUMMY_SERVICE_URL
  })),
  buildService({ name, url }) {
    if (url === DUMMY_SERVICE_URL) {
      return new LocalGraphQLDataSource(
        buildFederatedSchema(
          services[name].schema
        )
      );
    } else {
      return new RemoteGraphQLDataSource({
        url
      });
    }
  }
});

const apolloServer = new ApolloServer({
  gateway,
  subscriptions: false
});
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57304658

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档