首页 文章

这个GraphQL查询我做错了什么?

提问于
浏览
1

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

1 回答

  • 2

    在发出GraphQL请求时,这里的情况是_2643349 . 你没有't specify whether you were seeing any errors, but I' d wager 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 {
    

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

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

相关问题