GraphQL mutations Update 更新操作

版权声明:本文为Martin原创文章,未经Martin允许不得转载。 https://blog.csdn.net/qq_36279445/article/details/88345504
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

猜你喜欢

转载自blog.csdn.net/qq_36279445/article/details/88345504