首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Apollo Federation Gateway:在构建超图时包含本地模式

Apollo Federation Gateway:在构建超图时包含本地模式
EN

Stack Overflow用户
提问于 2021-07-27 23:53:19
回答 1查看 250关注 0票数 1

在为Apollo Federation的网关构建supergraph时,您将创建一个.yaml配置文件,其中包含指向子图的路由urls。例如:https://github.com/apollographql/supergraph-demo/blob/main/subgraphs/inventory/inventory.js

代码语言:javascript
复制
//supergraph.yaml
subgraphs:
  inventory:
    routing_url: http://inventory:4000/graphql
    schema:
      file: ./subgraphs/inventory/inventory.graphql
  products:
    routing_url: http://products:4000/graphql
    schema:
      file: ./subgraphs/products/products.graphql
  users:
    routing_url: http://users:4000/graphql
    schema:
      file: ./subgraphs/users/users.graphql

在上面的例子中,他们为每个子图启动了一个Apollo服务器,并组成了一个超图。有没有可能在不启动Apollo服务器和只包含本地模式的情况下构建超图?

EN

回答 1

Stack Overflow用户

发布于 2021-07-28 05:14:11

你可以的。遵循本教程:https://www.apollographql.com/blog/backend/using-apollo-federation-with-local-schemas/

不使用超图和子图,而是使用serviceList并有条件地构建数据源。

代码语言:javascript
复制
const gateway = new ApolloGateway({
  serviceList: [
    { name: "products", url: "http://localhost:4002" },
    { name: "countries", url: "http://countries" },
  ],
  buildService: ({ url }) => {
    if (url === "http://countries") {
      return new LocalGraphQLDataSource(getCountriesSchema());
    } else {
      return new RemoteGraphQLDataSource({
        url,
      });
    }
  },
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68548103

复制
相关文章

相似问题

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