我使用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中输入突变时,如下所示:
{
change_title(new_title: "Something") {
title
}
}
我得到以下错误:"不能查询字段"change_title"输入"查询";
我的目标是能够运行突变。如果我应该用另一种方法来做或者如果有错误,请告诉我。谢谢!
GraphQL playground将所有类型视为查询,除非另有说明。
mutation {
change_title(new_title: "Something") {
title
}
}