Schema Stitching 与 Federation 互操作桥接层
解决已有Stitching服务逐步迁移到Federation时的双向兼容问题。 · 难度:入门 · +10XP
构建混合架构的桥接网关
当部分团队仍使用Schema Stitching而另一部分采用Apollo Federation时,需要一种中间层协调两者。本教程将创建代理网关,将Stitching的extend类型转换为Federation的@key指令,并处理实体解析的差异。你将学习如何通过自定义Link在网关层重写查询,使双方服务无需修改即可互操作。
const { stitchSchemas } = require('@graphql-tools/stitch');
const { buildFederationSchema } = require('@apollo/federation');
const bridgeSchema = stitchSchemas({
subschemas: [
{ schema: stitchingSchema },
{ schema: federationSchema },
],
typeMerging: {
User: {
field: 'user',
selectionSet: '{ id }',
key: ({ id }) => ({ __typename: 'User', id })
}
}
});