DEV Community

Discussion on: Three in one (code first) : NestJs & GraphQl & Mongoose

Collapse
 
lotfi profile image
Lotfi

There is a way, mentioned in NestJs doc, to create a combine type but it does not work with combining a scalar and object types. ex :

export const ResultUnion = createUnionType({
  name: 'ResultUnion',
  types: () => [Hobby, String],
});
Enter fullscreen mode Exit fullscreen mode

That's why I chose to add the populate attribute, so if we just want the ids, we set it to false, which will save db access for the mongoose populate operation.

Collapse
 
radovansurlak profile image
Radovan Surlak

Right, I was looking into that as well.

The issue for me was that if we use
populate: false, the resolver would be returning an array of IDs, which GraphQL would complain about, since it's not of type Hobby.

I have personally used an approach where I specify extra GraphQL field populatedHobbies of type [Hobby] and keep hobbies endpoint, which would return [ID]

Thread Thread
 
lotfi profile image
Lotfi

Good idea !