node.js - 我这个 GraphQL 查询做错了什么?

标签 node.js mongoose graphql graphql-js

我是 GraphQL 新手,我正在尝试进行突变以从数据库中删除一篇文章,但我不知道如何操作。我正在使用 Node.js、Mongoose 和 GraphQL。

这是我的架构的突变。

const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  description: 'Articles Mutations',
  fields: () => ({
    remove: {
      type: articleType,
      description: 'Deletes an article by id',
      args: {
        id: {
          type: new GraphQLNonNull(GraphQLString)
        }
      },
      resolve: (value, {id}) => {
        return db.Article.findOneAndRemove({_id: new ObjectId(id)});
      }
    }
  })
});

这是我在调用 API 删除文章时使用的查询。

export const DELETE_ARTICLE_QUERY = (id) => (`{
  remove(id: "${id}") {
    id
    author
    content
    published
    tags
    title
    excerpt
  }
}`);

我做错了什么?

我收到 400 错误请求错误。消息:“无法查询类型“Mutation”上的“删除”字段。”

最佳答案

发出 GraphQL 请求时,最好始终指定您正在执行的操作类型(查询或突变)。如果您未能这样做,GraphQL 会假设您正在进行查询,但此处的情况并非如此。您没有指定是否看到任何错误,但我敢打赌 GraphQL 会返回类似 cannot query field remove on Query 的内容。

修改 DELETE_ARTICLE_QUERY 以包含以下操作:

export const DELETE_ARTICLE_QUERY = (id) => (`mutation {

出于调试目的,最好包含操作名称,因此您也可以说:

export const DELETE_ARTICLE_QUERY = (id) => (`mutation DeleteArticle {

编辑:根据您提供的错误,听起来架构对象设置不正确。它应该看起来像这样:

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: "WhateverNameYouWantForQuery",
    fields: {
      // each query is a property here
    }
  }),
  mutation: new GraphQLObjectType({
    name: "WhateverNameYouWantForMutation",
    fields: {
      // each mutation is a property here
    }
  }),
});

如果您将突变定义为单独的变量 (Mutation),则可以将突变属性的值作为该变量:

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    // query props
  }),
  mutation: Mutation
  }),
});

这是一个可以立即使用的工作示例。启动服务器后,您可以在浏览器中访问 http://localhost:3000/graphql 来访问 GraphiQL 界面并在那里尝试潜在的查询/突变。

const graphqlHTTP = require('express-graphql');
const app = require('express')();
const {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLNonNull,
  GraphQLString,
  GraphQLList
} = require('graphql');

const articleType = new GraphQLObjectType({
  name: 'Article',
  fields: {
    title: {
      type: GraphQLString,
    },
  },
});

const Mutation = new GraphQLObjectType({
  name: "RootMutationnnnn",
  fields: () => ({
    remove: {
      type: articleType,
      args: {
        id: {
          type: new GraphQLNonNull(GraphQLString)
        }
      },
      resolve: (value, {id}) => {
        return {title: 'Testing a delete'};
      }
    }
  })
});

const schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: "RootQueryyyyy",
    fields: {
      articles: {
        type: new GraphQLList(articleType),
        resolve: () => {
          return [{title: 'Test title'}, {title: 'Test title'}];
        }
      }
    }
  }),
  mutation: Mutation
});

const root = {};

app.post('/graphql', graphqlHTTP({
  schema,
  rootValue: root,
  graphiql: false,
}));

app.get('/graphql', graphqlHTTP({
  schema,
  rootValue: root,
  graphiql: true,
}));

app.listen(3000, function(){
  console.log('listening on port 3000');
});

关于node.js - 我这个 GraphQL 查询做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227306/

相关文章:

node.js - MongoDB 安装 macOS Catalina

node.js - 如何解决 MongoError : pool destroyed while connecting to CosmosDB

Node.JS Express 4 - Mongoose 不保存数据

node.js - 如何使用 Mongoose 在 MongoDB 中插入 JSON 数组

node.js - 编辑第三层 mongo 上的数据

node.js - GraphQL Apollo Sequelize,id 字段未定义但仍调用 Sequelize?

html - 使用图像预览 Bootstrap 文件上传

node.js - Telegram bot 可在组中单击命令,但单击它们会提到 bot

reactjs - Gatsby "Multiple "root“queries found”中的多个查询错误

python - 在 Django 应用程序中初始化 GraphQL 客户端的位置