partial update mutation #962
Answered
by
carlocorradini
Sed9yDataBank
asked this question in
Q&A
-
I am trying to update my position document which has 6 fields. I don't want to force updating all the fields whenever I call the update mutation. @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;
} @InputType()
export class PositionInput implements Partial<Position> {
@Field()
positionName: string;
@Field()
positionOfficeLocation: string;
@Field()
positionDepartment: string;
@Field()
positionTotalNeeded: number;
@Field()
positionStatus: string;
} What do I do if I only want to update the positionName, or if I am only want to update the positionStatus. |
Beta Was this translation helpful? Give feedback.
Answered by
carlocorradini
Jul 26, 2021
Replies: 1 comment 2 replies
-
Set each field to Moreover, in the input class, each field that can be nullable/omitted should be set to optional using @InputType()
export class PositionUpdateInput implements Partial<Position> {
@Field({ nullable: true })
positionName?: string;
@Field({ nullable: true })
positionOfficeLocation?: string;
@Field({ nullable: true })
positionDepartment?: string;
@Field({ nullable: true })
positionTotalNeeded?: number;
@Field({ nullable: true })
positionStatus?: string;
} @Mutation(() => Boolean, { nullable: false })
async updatePosition(
@Arg("positionId", type => ObjectIdScalar) positionId: ObjectId,
@Arg("position", () => PositionUpdateInput) positionUpdateInput: PositionInput) {
const position = await PositionModel.findOneAndUpdate({
_id: positionId as any
}, positionUpdateInput);
if (position)
return true;
else return false;
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Sed9yDataBank
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Set each field to
nullable: true
: if it's omitted no error fromGraphQL
is thrown.Moreover, in the input class, each field that can be nullable/omitted should be set to optional using
?
.