【问题标题】:What am I doing wrong with this GraphQL query?这个 GraphQL 查询我做错了什么?
【发布时间】:2017-12-26 21:27:48
【问题描述】:

我是 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 错误请求错误。消息:“无法查询类型“突变”的字段“删除”。”

【问题讨论】:

  • 在提出这样的问题时,提供尽可能多的详细信息会很有帮助。例如:当您运行查询时,GraphQL 或节点是否抛出任何错误,以及错误的含义。

标签: node.js mongoose graphql graphql-js


【解决方案1】:

发出 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');
});

【讨论】:

  • 感谢您的评论和真正有用的上衣。但是我仍然收到 400 Bad Request 错误:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
相关资源
最近更新 更多