graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
708 stars 94 forks source link

Projection on addRelation not working for Discriminators #441

Open danimayfield opened 6 months ago

danimayfield commented 6 months ago

I have a discriminator called BProduct which is a discriminator off the base of AProduct. And this discriminator has a property I'm trying to add a relation to:

BProductTC.getFieldOTC("metadata").addRelation(
  "amenities",
  {
    resolver: () => AmenityTC.mongooseResolvers.findByIds(),
    prepareArgs: {
      _ids: (source: any) => source.amenities ?? [],
      sort: null,
      limit: null,
    },
    extensions: { projection: { amenities: true } }, <-- This doesn't work
  }
);

However the projection on this discriminator (BProduct) doesn't work at all regardless of if I use extensions or not.

The below relation on the base model of AProduct works perfectly with the use of extension though:

AProductTC.addRelation("tags", {
  resolver: () => TagTC.mongooseResolvers.findByIds()
  prepareArgs: {
    _ids: (source) => source.tags,
    sort: null,
    limit: null,
  },
  extensions: { projection: { tags: true } }, <-- works as projection should
});

Does anyone have a solution for adding relations on discriminator properties?

These are my dependancies:

  "graphql": "^16.8.1",
    "graphql-compose": "^9.0.10",
    "graphql-compose-mongoose": "^10.0.0",
    "mongodb": "^6.2.0",
    "mongoose": "^8.2.0",

Originally posted by @danimayfield in https://github.com/graphql-compose/graphql-compose-mongoose/issues/377#issuecomment-2023124409