Typegoose: What is the type of query result

Created on 3 Sep 2020  路  3Comments  路  Source: typegoose/typegoose


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.

Code

@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();
}

Versions

  • typegoose: 7.2.0
  • mongoose: 5.9.19
question graphql / type-graphql

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 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

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohamedGamalAbuGalala picture mohamedGamalAbuGalala  路  5Comments

kerolloz picture kerolloz  路  6Comments

richarddd picture richarddd  路  8Comments

hmgibson23 picture hmgibson23  路  10Comments

AbderrazzakB picture AbderrazzakB  路  8Comments