graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Add relation to nested ObjectID array

Open pixel-mattp opened this issue 3 years ago • 1 comments

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!

pixel-mattp avatar Jun 14 '21 12:06 pixel-mattp

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 }, });

henrikwikstrom avatar Jun 27 '21 16:06 henrikwikstrom