tnc-ca-geo / animl-api

Backend for https://animl.camera
4 stars 0 forks source link

TypeScript: Resolvers(query.task/stats/exportErrors/exportAnnotations): Return toObject() in order to convert `_id` to string #200

Closed alukach closed 3 weeks ago

alukach commented 3 weeks ago

For any method where a Task is returned, we see the following error:

Type 'Document<unknown, {}, { type: "GetStats" | "ExportAnnotations" | "ExportImageErrors" | "CreateDeployment" | "UpdateDeployment" | "DeleteDeployment"; user: string; projectId: string; status: "SUBMITTED" | ... 2 more ... | "COMPLETE"; created: Date; updated: Date; output?: any; }> & { ...; } & { ...; }' is not assignable to type 'Task'.
  Types of property '_id' are incompatible.
    Type 'ObjectId' is not assignable to type 'string'.ts(2322)

This seems to be an issue where GraphQL expected the _id to be a string:

https://github.com/tnc-ca-geo/animl-api/blob/27b3e3d955208ffdaa061aeced28689e76644ad4/src/@types/graphql.ts#L1038 https://github.com/tnc-ca-geo/animl-api/blob/27b3e3d955208ffdaa061aeced28689e76644ad4/src/@types/graphql.ts#L10

However, Mongoose shows it as an mongoose.Types.ObjectId. Calling .toObject() on the response object rectifies this. This may become a common pattern for our resolvers... I would like to imagine that there's a better way to handle this, but I'm not sure what that would be, perhaps something with the Scalars for the output?

alukach commented 3 weeks ago

I actually think the reason that this conversion works is because of https://github.com/Automattic/mongoose/issues/12883, ie it's masking an actual issue.