graphql-compose-mongoose
graphql-compose-mongoose copied to clipboard
Add relation to nested ObjectID array
I have a Schema as follows:
export const ProductSchema = new Schema(
{
colours: {
type: [
{
colour: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Colour',
},
price: { type: String },
},
],
required: false,
}
},
{
collection: 'products',
}
);
I am trying to use the addRelation
function to populate the colour object inside each array item, however because it's within a nested array (not an object) I'm struggling to make it work.
Am I missing something?
Thanks!
You have to add a releation to your ObjectTypeComposer object, like this
TC.getFieldOTC("colours").addRelation("colour", { resolver: () => ColorTC.mongooseResolvers.findByIds(), prepareArgs: { _ids: (source) => source.colours.map((o) => o._id) || null, }, projection: { colour: true }, });