如何使用 GraphQL Playground 运行 ApolloServer 中的突变?

How to run a mutation in ApolloServer using the GraphQL Playground?

我正在使用 node.js、express 和 apollo-server-express。使用以下代码:

const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
const typeDefs = gql`
  type Book { title: String author: String }
  type Query { books: [Book] }
  type Mutation { change_title(new_title: String): Book }
`;

const books = [
  { title: 'The Awakening', author: 'Kate Chopin', },
  { title: 'City of Glass', author: 'Paul Auster', },
];

const resolvers = {
  Query: { books: () => books, },
  Mutation: {
    change_title: (parent, args) => {
      books[0].title = args.new_title
      return books[0]
    }
  }
};

const server = new ApolloServer({ typeDefs, resolvers, });
const app = express();
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
  console.log(`Server ready at http://localhost:4000${server.graphqlPath}`)
);

当我在 GraphQL Playground 中输入 mutation 时,像这样:

{
    change_title(new_title: "Something") {
    title
  }
}

我收到以下错误:“无法在类型“查询”上查询字段“change_title”。

我的目标是能够 运行 突变。如果我应该以另一种方式做,或者如果有错误,请告诉我。谢谢!

GraphQL playground 将所有类型视为查询,除非另有说明。

mutation {
    change_title(new_title: "Something") {
    title
  }
}