I'm trying to work with class-transformer and NestJS. I'm building an API based on Mongoose and GraphQL.
This is how I'm using Exclude decorator in one of my GraphQL objects:
@ObjectType('user')
export class User extends Teacher {
@Field()
login: string;
@Field()
@Exclude()
password: string;
}
And this is my method in UserResolver used with ClassSerializerInterceptor:
@UseInterceptors(ClassSerializerInterceptor)
@Mutation(returns => User, { name: 'editUserById', nullable: true })
async editById(
@Args('id') id: string,
@Args({ name: "item", type: () => UserInputType }) item: UserInputType
) {
const user = await this.usersService.editById(id, item);
return user;
}
What I'm trying to do is to get from this mutation user fields without a password (which is excluded in GraphQL object). But unfortunately, all fields are null. Error example from GraphQL playground:
{
"errors": [
{
"message": "Cannot return null for non-nullable field user.firstName.",
"locations": [
{
"line": 3,
"column": 5
}
],
"path": [
"editUserById",
"firstName"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR"
}
}
],
"data": {
"editUserById": null
}
}
Teacher object:
@ObjectType('teacher')
@InputType()
export class Teacher {
@Field()
_id: string;
@Field()
firstName: string;
@Field()
lastName: string;
@Field(type => [String], { nullable: true })
schoolsIds: string[];
@Field(type => [School], { nullable: true })
schools: School[];
}
Without interceptor, everything is working fine (except that password is still visible). Any ideas what I'm doing wrong?
question from:
https://stackoverflow.com/questions/65647447/nestjs-problem-with-classserializerinterceptor-in-graphql 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…