DEV Community

Cover image for Three in one (code first) : NestJs & GraphQl & Mongoose

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

Lotfi on November 01, 2020

Follow me on Twitter, happy to take your suggestions and improvements. I decided to write this blog post after working on a personal project. As...
Collapse
 
gastonmorixe profile image
Gaston Morixe • Edited

@Prop({ type: [Types.ObjectId], ref: Hobby.name })

Should be

@Prop({ type: [mongoose.Schema.Types.ObjectId], ref: Hobby.name })

Otherwise the references end up as strings instead of ObjectId.

Collapse
 
lotfi profile image
Lotfi

Thank you for your remark. Article updated!

Collapse
 
gastonmorixe profile image
Gaston Morixe

You are welcome!

Collapse
 
smolinari profile image
Scott Molinari

Nice article. I'd like to throw in another possible set of bricks for getting NestJS and Mongoose working. It's called Typegoose.

typegoose.github.io/typegoose/
(and corresponding NestJS Module for it: kpfromer.github.io/nestjs-typegoose/

Be aware though, using Mongoose with TypeScript is an adventure. They just moved their types to their own a few months ago and they are about 97% ok. But, there might be 3% where you'll be scratching your head. The Typegoose maintainer though works hard to work with the Mongoose team to get these type issues resolved and the upcoming 8.0 of Typegoose is shaping up to be pretty cool.

Hopefully, one day, we'll have a Mongoose written in TypeScript. Crossing my fingers!

Scott

Collapse
 
lotfi profile image
Lotfi

Interesting, I will check Typegoose, I didn't know it. Thank you for your comment.

Collapse
 
radovansurlak profile image
Radovan Surlak

Thank you for the article!

I have found an issue however - once you query for Person's hobbies without "populate: true", you will get errors from the GraphQL API.

If anyone knows how to allow the endpoint to return both IDs and populated documents, it would help out a lot

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 !

Collapse
 
jbool24 profile image
Justin Bellero

Anyone using this technique of overlaying Graphql/Mongoose models using the mongoose discriminators shcema type? I'd like to have one "users" collection that discriminates document data base on the user kind. Is it even possible to get that working? Am I trying to create a nightmare for myself?

I'm testing things out now but having issues with graphql resolution since the gql schema needs to implement union types (I think). I might be going about it wrong. Any suggestions?

Collapse
 
zyfyy profile image
zyfyy

Thank you so much!

Collapse
 
atifmahmood1566 profile image
AtifMahmood1566

Overall it is a good article for a beginner to understand about nest and graphql. The only problem i am facing while running the project is the following error.

Error: Cannot determine a GraphQL input type nullfor the "_id". Make sure your class is decorated with an appropriate decorator.

Collapse
 
lotfi profile image
Lotfi

Did you get the error on trying to run the project in the git repository?

Collapse
 
mehtabahmed176 profile image
Mehtab Ahmed

Thank you , one of the best article on NestJs and Graphql based api so far.

Collapse
 
aniganesh profile image
Aniganesh • Edited

Not sure if this has been pointed out but execPopulate has now been removed. Could you please caveat the execPopulate references?
stackoverflow.com/a/69444464/10032950
mongoosejs.com/docs/migrating_to_6...

Great article by the way. I found it useful

Collapse
 
dantereve profile image
Mathias Oviève

This blog post is exactly what I was looking for. You did a fantastic job explaining how to achieve this! Thank You! 🙇
If you find the time to write the sequel, I would be a happy reader.

Collapse
 
phyberapex profile image
PhyberApex

This is exactly what I was trying to do but couldn't figure out without a proper example. Thank you so much!

~Cheers

Collapse
 
fburner profile image
FBurner

Hi, i cant use find on list it shows me that the filter from the input doesnt match the signature

Collapse
 
lotfi profile image
Lotfi

Did you try to test the final projet on github repo ?