Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Conversation

alukach
Copy link
Contributor

@alukach alukach commented Jun 5, 2024

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:

_id: Scalars['ID']['output'];

ID: { input: string; output: string; }

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 alukach changed the title TypeScript: Resolvers: Cast to objectect in order to convert _id to string TypeScript: Resolvers(query.task/stats/exportErrors/exportAnnotations): Cast to objectect in order to convert _id to string Jun 5, 2024
@alukach alukach changed the title TypeScript: Resolvers(query.task/stats/exportErrors/exportAnnotations): Cast to objectect in order to convert _id to string TypeScript: Resolvers(query.task/stats/exportErrors/exportAnnotations): Return toObject() in order to convert _id to string Jun 5, 2024
@alukach
Copy link
Contributor Author

alukach commented Jun 5, 2024

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

@alukach alukach closed this Jun 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant