Apollo Server合并同类型远程Schema,如何通过pk关联解析跨服务字段?
解决方案
你需要在mergeSchemas的配置中新增跨服务字段解析委托逻辑,通过resolvers参数指定关联规则,具体调整如下:
步骤1:拆分远程Schema实例
首先调整createRemoteExecutableSchemas的返回结构,确保可以单独获取到auth服务的可执行Schema实例:
const createRemoteExecutableSchemas = async () => { const shopSchema = await getRemoteSchema('shop服务接口地址'); const authSchema = await getRemoteSchema('auth服务接口地址'); return { shopSchema, authSchema }; };
步骤2:新增跨服务解析器配置
给UserType下所有auth服务独有的字段指定解析逻辑:解析时会先从shop服务返回的用户数据中提取pk字段,再用该pk作为参数委托到auth服务查询对应字段值。
const createNewSchema = async () => { const { shopSchema, authSchema } = await createRemoteExecutableSchemas(); return mergeSchemas({ schemas: [shopSchema, authSchema], resolvers: { UserType: { username: { // 声明解析该字段需要先获取UserType的pk值 fragment: `fragment UserPk on UserType { pk }`, resolve(parent, args, context, info) { return info.mergeInfo.delegateToSchema({ schema: authSchema, operation: 'query', // 替换为auth服务中根据pk查询用户的查询字段名 fieldName: 'userByPk', args: { pk: parent.pk }, context, info }); } }, email: { fragment: `fragment UserPk on UserType { pk }`, resolve(parent, args, context, info) { return info.mergeInfo.delegateToSchema({ schema: authSchema, operation: 'query', fieldName: 'userByPk', args: { pk: parent.pk }, context, info }); } } // 其余auth服务的UserType字段按相同规则补充即可 } } }); };
优化:批量生成解析器
如果auth服务的UserType字段较多,可以通过遍历Schema定义批量生成解析器,避免重复编码:
// 获取auth服务UserType的所有字段,过滤掉两个服务共有的id、pk字段 const authUserType = authSchema.getType('UserType'); const authUniqueFields = Object.keys(authUserType.getFields()) .filter(field => !['id', 'pk'].includes(field)); // 批量生成解析器配置 const userResolvers = Object.fromEntries( authUniqueFields.map(field => [ field, { fragment: `fragment UserPk on UserType { pk }`, resolve(parent, args, context, info) { return info.mergeInfo.delegateToSchema({ schema: authSchema, operation: 'query', fieldName: 'userByPk', args: { pk: parent.pk }, context, info }); } } ]) ); // 最后把userResolvers传入mergeSchemas的resolvers配置即可
注意事项
- 你需要提前在auth服务的Schema中实现根据
pk查询单个用户的查询接口,即上述代码中用到的userByPk字段,入参为pk: Int!,返回值为UserType。 - 若使用较新版本的Apollo Server,更推荐改用Apollo联邦实现多服务Schema合并,天然支持跨服务实体关联,仅需给
UserType添加@key(fields: "pk")指令即可实现自动关联,无需手动编写委托逻辑。
内容的提问来源于stack exchange,提问作者Kim Rop
相关产品推荐
相关产品推荐

