我在node.js中使用mongodb,我试图返回一个自定义对象列表的字段。我曾尝试过无论是否有过#Array()'在查询结束时但似乎没有改变结果
我有一个自定义类型,我的GraphQL架构看起来大致相似(少了根商店/查看器的代码等)
let addressType = new GraphQLObjectType({
name: 'Address',
fields: () => ({
addressType: {type: GraphQLString, resolve: (obj) => obj.AddressType },
addressLineOne: {type: GraphQLString, resolve: (obj) => obj.AddressLineOne}
});
});
let userType = new GraphQLObjectType({
name: 'User',
fields: () => ({
CustomId: {type: GraphQLID, resolve: (obj) => obj.CustomID},
name: {type: GraphQLString},
addresses: {
type: GraphQLList(addressType),
resolve: (user) => {
let findParams = {
CustomID: user.CustomID
};
let returnParams = {
USER_ADDRESSES: 1
}
return db.collection("User").find(findParams, returnParams).toArray();
}
}
})
});
我的mongo文档看起来像......
"Name":"Joe",
"CustomID":"12345",
"USER_ADDRESSES":
[
{
"AddressType" : "Home",
"AddressLineOne" : "123 Maple st"
},
{
"AddressType" : "AlternateAddress",
"AddressLineOne" : "456 Main St"
}
]
使用graphiql测试查询的模式
{
store{
user(CustomID:"12345"){
name,
addresses {
addressType,
addressLineOne
}
}
}
}
哪一回......
{
"data":{
"store": {
"user": {
"name": "Joe",
"addresses" : [
"addressType": null,
"addressLineOne": null
]
}
}
}
}