简体   繁体   中英

Parent is only ID in GraphQL resolver

So I have a relational mongo database that looks something like this

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

Shops {
    _id: ...
    ...
}

Also I already have my GraphQL Setup in place and the 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?

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. Now my question: Do I really have to look up the parent with mongoose again to get the ShopIDs? 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. If the Child ID is undefined it probably caused by the mongoose model itself. So if you have a similar issue: Doublecheck your schemas and make sure the field is actually defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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