我有一个问题,基于 https:/www.apollographql.comdocsgraphql-toolsschema-stitching
假设我有以下模式(注意,这是为了举例而建立的,这样的结构没有意义)。
//First schema
type User {
id
cars: [Car]!
}
type Car {
id
name: String
model: String
}
//Second schema
type Color {
id
name: String
rgb: String
opacity: Int
}
//Defined in Apollo Server
extend type Car {
color: Color
}
以及以下解析器
resolvers: {
Car: {
color: {
fragment: '... on Car { id }',
resolve(parent, args, context, info) {
return delegateToSchema({
schema: secondSchema,
operation: 'query',
fieldName: 'colorByUserAndCarId',
args: {
id: parent.id,
userId: ?
},
context,
info,
});
},
},
},
}
如何获得用户ID,是在用户类型上,而不是汽车类型上?
当我在搜索这个问题的答案时,我想到了一些解决方案,但我不知道如何使它们中的任何一个工作。
Color
一部分 Car
因此,我将直接在汽车上设置每个颜色的字段,所以我想我应该根据用户来设置解析器...?'... on User'
,而在类型上 Car
但到目前为止还不行。Car
通过扩展它,但我无论如何也找不到如何获取userId的方法。从根目录下更改模式和类型是不行的,所有的修改都需要在Apollo服务器内进行。
写出问题和潜在的解决方案有助于我更好地理解它的工作原理和如何实现。
我可以像为 "color "添加解析器一样为 "car "添加一个解析器,当我进入car解析器时,"car "的对象值已经在那里了。parent
价值会像 { id: x, car: {...}}
, id
作为 userId
因此,我可以只做这个在我的车解析器。
let myCar = parent.car;
myCar.userId = parent.id;
return myCar;
当我在我的颜色解析器中,我将能够做的是 parent.userId
.