type-graphql update mutation with typegoose #953
-
Hello, @Mutation(() => Position, { nullable: true })
async updatePosition(
@Arg("positionId", type => ObjectIdScalar) positionId: ObjectId,
@Arg("position", () => PositionInput, { nullable: true }) positionInput: PositionInput
): Promise<Position | null> {
const position = await PositionModel.findById(positionId);
if (!position) {
return null;
}
if (typeof positionInput !== "undefined") {
await PositionModel.updateOne({ positionId }, { ...positionInput }, { returnOriginal: false });
}
return position;
} @InputType()
export class PositionInput implements Partial<Position> {
@Field()
positionName!: string;
@Field({ nullable: true })
positionOfficeLocation?: string;
@Field({ nullable: true })
positionDepartment?: string;
@Field({ nullable: false })
positionTotalNeeded!: number;
@Field({ nullable: false })
positionStatus!: string;
@Field(() => [ProgressionInput])
progressions: ProgressionInput[];
@Field(() => [CandidateInput])
candidates: CandidateInput[];
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I don't know how this is a problem with TypeGraphQL. const position = await PositionModel.findById(positionId);
return position; That's why you receive old document. You return it. Read again from database after updating to return updated document. |
Beta Was this translation helpful? Give feedback.
-
After multiple attempts I got it working: @Mutation(() => Boolean, { nullable: false })
async updatePosition(
@Arg("positionId", type => ObjectIdScalar) positionId: ObjectId,
@Arg("position", () => PositionInput, { nullable: true }) positionInput: PositionInput
) {
const position = await PositionModel.findOneAndUpdate({
_id: positionId as any
}, positionInput);
if (position)
return true;
else return false;
} I have not changed anything in the PositionInput. |
Beta Was this translation helpful? Give feedback.
After multiple attempts I got it working:
I have not changed anything in the PositionInput.
Thank you for your time @MichalLytek !