I'm using typegoose with typegraphql, and I need to fetch a document based on a previous query(find or findById), but the returns type is not the type of the typegoose class but an object I think it's a mongoose document and document inside a _doc property.
@Query(() => [Meter])
async meters(): Promise<Meter[]> {
return await MeterModel.find();
}
@Query(() => Meter, {nullable: true})
async meter(@Arg('id') id: string): Promise<Meter | null> {
return await MeterModel.findById(id);
}
In this field resolver, I need to know the type of meter that returned by the functions aboves
@FieldResolver(() => Member)
async owner(@Root() meter: any) {
const ownerId = meter._doc.owners.find(
(owner: any) => owner.to === undefined
)?.member;
return await MemberModel.findById(ownerId).lean();
}
Not sure this is the same problem, but I think it might be the one I also ran into. The type for your field resolver's root object should be your Meter class. The issue is, the returned object from mongoose isn't a plain typed object, but rather it is, as you are guessing, a document.
Check out the TypeGraphQL example and what Michal does with the returned results from Mongoose. That, I believe, might help you. :) It did for me.
Scott
I tried the solution you mentioned and it did work, thank you.
Though by using the typegoose-middleware it did strips the queries from handful mongoose methods, so I had to drop it.
Most helpful comment
Not sure this is the same problem, but I think it might be the one I also ran into. The type for your field resolver's root object should be your
Meterclass. The issue is, the returned object from mongoose isn't a plain typed object, but rather it is, as you are guessing, a document.Check out the TypeGraphQL example and what Michal does with the returned results from Mongoose. That, I believe, might help you. :) It did for me.
Scott