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...
For further actions, you may consider blocking this person and/or reporting abuse
@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.
Thank you for your remark. Article updated!
You are welcome!
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
Interesting, I will check Typegoose, I didn't know it. Thank you for your comment.
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
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 :
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.
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 typeHobby
.I have personally used an approach where I specify extra GraphQL field
populatedHobbies
of type[Hobby]
and keephobbies
endpoint, which would return[ID]
Good idea !
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?
Thank you so much!
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.
Did you get the error on trying to run the project in the git repository?
Thank you , one of the best article on NestJs and Graphql based api so far.
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
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.
This is exactly what I was trying to do but couldn't figure out without a proper example. Thank you so much!
~Cheers
Hi, i cant use find on list it shows me that the filter from the input doesnt match the signature
Did you try to test the final projet on github repo ?