我有一个已缝合的graphql模式。某些类型字段使用info.mergeInfo.delegateToSchema
下面是一个示例(来自apollo docs):
const mergedSchema = mergeSchemas({
schemas: [
transformedChirpSchema,
authorSchema,
linkTypeDefs,
],
resolvers: {
User: {
chirps: {
fragment: `... on User { id }`,
resolve(user, args, context, info) {
return info.mergeInfo.delegateToSchema({
schema: chirpSchema,
operation: 'query',
fieldName: 'chirpsByAuthorId',
args: {
authorId: user.id,
},
context,
info,
});
},
},
},
});
是否可以在root
解析器中访问chirps
?这样在root
中有所有父字段吗?当然,另一种方法是为此目的使用context
,但是从代码角度来看,我想使用root会更好,因为在某些情况下我已经使用root
值。
答案 0 :(得分:0)
内幕info.mergeInfo.delegateToSchema
可以调用远程GraphQL应用程序(更多details)。
因此,根据设计,远程解析器无权访问本地root / context / info / arg,因此需要在用于远程字段的参数中发送所有必需的数据。例如:
const mergedSchema = mergeSchemas({
schemas: [
transformedChirpSchema,
authorSchema,
linkTypeDefs,
],
resolvers: {
User: {
chirps: {
fragment: `... on User { id }`,
resolve(user, args, context, info) {
return info.mergeInfo.delegateToSchema({
schema: chirpSchema,
operation: 'query',
fieldName: 'chirpsByAuthorId',
args: {
// author is InputType at remove schema with similar user structure
author: user,
},
context,
info,
});
},
},
},
});
我不知道您的情况,但在使用删除架构时不会忘记schema-transforms。