You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为联邦架构下的多个GraphQL服务生成聚合的schema.json?

联邦多GraphQL服务聚合schema下载实现方案

首先 Apollo 官方原生的 apollo schema:download 命令不支持多端点参数同时下载并聚合联邦子图 schema 的需求,你可以通过以下两种稳定方案实现:

方案1:使用Apollo官方推荐的联邦管理工具Rover CLI实现

  • 第一步:安装Apollo Rover CLI后,依次拉取所有子图的schema文件:
# 拉取子图1 schema
rover subgraph introspect http://localhost:9001/graphql > subgraph1_schema.graphql
# 拉取子图2 schema,以此类推拉取所有子图
rover subgraph introspect http://localhost:9002/graphql > subgraph2_schema.graphql
  • 第二步:在本地创建超图配置文件 supergraph.yaml,配置所有子图信息:
federation_version: 2
subgraphs:
  subgraph1:
    routing_url: http://localhost:9001/graphql
    schema:
      file: ./subgraph1_schema.graphql
  subgraph2:
    routing_url: http://localhost:9002/graphql
    schema:
      file: ./subgraph2_schema.graphql
  # 有更多子图继续按格式添加即可
  • 第三步:执行聚合命令生成合并后的超图schema
rover supergraph compose --config ./supergraph.yaml > supergraph_schema.graphql

如果需要JSON格式的schema文件,可直接使用GraphQL相关工具将生成的graphql格式schema转为标准schema.json即可。

方案2:自研脚本实现聚合

  • 先用原有命令批量下载所有子图的独立schema.json文件,分别命名存储
  • 引入@apollo/federation依赖包,调用buildSubgraphSchema、composeSubgraphs方法加载所有子图schema,按照联邦规则聚合后输出为合并后的schema.json即可

注意:不要直接手动拼接多个子图的schema内容,联邦子图包含@key、@extends等专属联邦指令,普通拼接会丢失规则信息,必须使用官方联邦组合逻辑处理才能保证聚合后的schema可用。

内容的提问来源于stack exchange,提问作者Prashant Kumar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 11:18:04