I'm using TypeScript with NestJS on this project:
https://github.com/EricKit/nest-user-auth
I'm attempting to add the _id property to the GraphQL Schema:
type User {
username: String!
email: String!
permissions: [String!]!
createdAt: Date!
updatedAt: Date!
enabled: Boolean!
_id: String!
}
Now, NestJS generate a type file for a user from this schema
export abstract class User {
username: string;
email: string;
permissions: string[];
createdAt: Date;
updatedAt: Date;
enabled: boolean;
_id: string;
}
The problem now is I want to create an interface for a UserDocument that adds the mongoDB specific fields and defines a document
export interface UserDocument extends User, Document {
// Declaring everything that is not in the GraphQL Schema for a User
_id: string; // TODO: This should actually be Types.ObjectId
password: string;
lowercaseUsername: string;
lowercaseEmail: string;
passwordReset?: {
token: string;
expiration: Date;
};
checkPassword(password: string): Promise<boolean>;
}
I can't define the _id as a mongoose.Type.ObjectID because it produces the error:
Interface 'UserDocument' incorrectly extends interface 'User'.
Types of property '_id' are incompatible.
Type 'ObjectId' is not assignable to type 'string'.
This makes sense, I'd like to find a way to keep it as an ObjectId on my UserDocument schema, keep it as a String in the Schema, and still be able to extend the GraphQL schema type. Is this possible?