type Author {
id: Int!
firstName: String
lastName: String
}
type Mutation {
updateAuthor(authorId: Int!, firstName: String, LastName: String): Author
}
主要内容:
更新操作的时候,firstName 和 lastName 并不是必须的,有可能出现我没传lastName,数据库更新的时候就给我设置成null了
所以这里得判断一下
updateAuthor: (_, {authorId, firstName, lastName}) => {
const author = find(authors, { id: authorId });
if (!author) {
throw new Error(`Couldn't find author with id ${authorId}`);
}
if (firstName !== undefined) {
author.firstName = firstName;
}
if (lastName !== undefined) {
author.lastName = lastName;
}
return author;
}
原文:https://medium.com/workflowgen/graphql-mutations-partial-updates-implementation-bff586bda989