联邦图(Federation)
Apollo Federation 让您可以声明性地将多个 GraphQL API 组合成一个单一的联合图。联邦图使客户端能够通过单个请求与多个 API 进行交互。
GQLoom Federation 提供了 GQLoom 对 Apollo Federation 的支持。
安装
GraphQL 指令
Apollo Federation 指令(Directives )用于描述如何将多个 GraphQL API 组合成一个联合图。
在对象上声明指令
在 GQLoom 中,我们可以在对象和字段的 extensions
属性中的 directives
字段来声明 GraphQL 指令:
以上示例中,我们声明了一个 @key
指令,该指令将 User
对象的 id
字段标记为可解析字段,我们将得到如下的 Schema:
在解析器上声明指令
我们还可以使用解析器为对象声明指令:
为 Schema 添加指令
指令格式
我们有两种声明指令的格式:
- 使用数组:
- 使用键值对:
解析引用 | Resolve Reference
@gqloom/federation
提供了 resolveReference
函数来帮助您解析引用。
编织
从 @gqloom/federation
引入的 FederatedSchemaWeaver.weave
函数用于编织 Federation Schema。与 @gqloom/core
相比,@gqloom/federation
中的 FederatedSchemaWeaver.weave
函数将输出携带指令(Directives)的 Schema。
另外值得注意的是,我们需要使用从 @apollo/subgraph
引入的 printSubgraphSchema
函数将 Schema 转换为文本格式以保留指令(Directives)。