【问题标题】:How to run a mutation in ApolloServer using the GraphQL Playground?如何使用 GraphQL Playground 在 ApolloServer 中运行突变?
【发布时间】:2021-05-14 20:56:32
【问题描述】:

我正在使用 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
  }
}

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

我的目标是能够运行突变。如果我应该以其他方式进行操作或有错误,请告诉我。谢谢!

【问题讨论】:

    标签: graphql apollo-server graphql-playground


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-11-19
      • 2021-04-14
      • 2019-02-21
      • 2017-06-07
      • 2021-02-17
      • 2022-06-23
      • 1970-01-01
      • 2018-09-03
      • 2021-03-16
      相关资源
      最近更新 更多