Hi everyone! 😊
import { modelOptions, prop } from '@typegoose/typegoose';
import mongoose from 'mongoose';
import { Field, ID, ObjectType } from 'type-graphql';
@ObjectType()
@modelOptions({ schemaOptions: { versionKey: false } })
export class UserEntity {
@Field()
@prop()
public email?: string;
@Field()
@prop()
public firstName?: string;
@Field(() => ID)
@prop()
public id: mongoose.Types.ObjectId;
@Field()
@prop()
public lastName?: string;
}
mutation Register {
register(password: "password1234", email: "mail", lastName: "lasr", firstName: "first") {
id
}
}
I'm using type-graphl
and typegoose
to provide an UserEntity
as a result of a mutation. After creating a user in the database there is an autogenerated _id in the document as expected. The result of the create function has a id
property having a string representation of id
. The mutation works as expected, but if I query for id I get this exception:
Cannot return null for non-nullable field UserEntity.id.
How do I solve this? What decorators do I have to use to see the proper id
?
I renamed id
to _id
and removed @prop()
from the property. This way one can access the generated _id
from the database.
import { modelOptions, prop } from '@typegoose/typegoose';
import mongoose from 'mongoose';
import { Field, ID, ObjectType } from 'type-graphql';
@ObjectType()
@modelOptions({
schemaOptions: {
versionKey: false,
},
})
export class UserEntity {
@Field(() => ID)
public _id: mongoose.Types.ObjectId;
@Field()
@prop()
public email: string;
@Field()
@prop()
public firstName: string;
@Field()
@prop()
public lastName: string;
}