如何解决"mutation returning data null"?

How to solve "mutation returning data null"?

我正在使用 apollo-server 并想学习 graphql 模式、查询和突变,但我没有获得正确的资源来理解突变的工作原理以及如何在解析器中定义突变

我曾尝试在解析器中添加类似于 "query" 的 "mutation",但没有用。

#schema
  const typeDefs = gql`
  type Book {
    title: String
    author: String
  }

  type Mutation {
    addBook(title: String, author: String): Book
  }

  type Query {
    getBooks: [Book]
  }
`;

#resolvers
const resolvers = {
  Query: {
    getBooks: () => books
  }
};


#querying  in graphql playground
mutation{
  addBook(  title: "a sad love story",author:"pavan kalyan"){
    title
    author
  }
}

#result i got
{
  "data": {
    "addBook": null
  }
}

我想在结果中获得与我在查询中传递的参数相同的标题和作者 并且没有错误消息

您需要在解析器中定义变更:

const resolvers = {
  Query: {
    getBooks: () => books,
  },
  Mutation: {
    addBook: (_, {input}) => {
      // ... code to add book somewhere
      const addedBook = insert(input.title, input.author);
      return addedBook; // i.e. {title: input.title, author: input.author};
    }
  }
}