简体   繁体   English

Parent只是GraphQL解析器中的ID

[英]Parent is only ID in GraphQL resolver

So I have a relational mongo database that looks something like this 所以我有一个看起来像这样的关系mongo数据库

Location {
    _id: ...
    shopIDs: <List of Shop IDs in another collection>
}

Shops {
    _id: ...
    ...
}

Also I already have my GraphQL Setup in place and the Schema. 此外,我已经安装了GraphQL安装程序和Schema。 But to get a Query like 但要获得一个类似的查询

locations {
    shops {
        name
    }
}

I need to create another Resolver that connects the shopIDs field to the shop collection, right? 我需要创建另一个将shopIDs字段连接到商店集合的Resolver,对吧?

So I did something like: 所以我做了类似的事情:

Location: {
        shopIDs: async ({ shopIDs }, args, context) => {
            return await Shop.find({ _id: { $in: shopIDs } });
        }
},

But the shopIDs is undefined and when I console logged the parent object it was only the ID of the parent. 但是shopID是未定义的,当我控制台记录父对象时,它只是父对象的ID。 Now my question: Do I really have to look up the parent with mongoose again to get the ShopIDs? 现在我的问题是:我是否真的必须再次使用mongoose查找父母以获取ShopID? I have not find any related Question, so I hope this have not yet been answered. 我没有找到任何相关的问题,所以我希望这还没有得到回答。 Thank you! 谢谢!

I am not completely sure why, but the issue got resolved. 我不完全确定原因,但问题得到了解决。 The first parameter is definetly the parent, so you can access the ID from there and can look up the child, without looking up the parent again. 第一个参数肯定是父参数,因此您可以从那里访问ID并查找子项,而无需再次查找父项。 If the Child ID is undefined it probably caused by the mongoose model itself. 如果未定义子ID,则可能是由猫鼬模型本身引起的。 So if you have a similar issue: Doublecheck your schemas and make sure the field is actually defined. 因此,如果您遇到类似问题:请仔细检查您的架构并确保该字段已实际定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM