【问题标题】:The graphiql mutation request succeeds but returns nullgraphiql 突变请求成功但返回 null
【发布时间】:2019-05-22 05:37:34
【问题描述】:

当我对使用我的 GraphQL 服务器代码的 graphiql 进行突变时,它会为对象中的所有条目返回 null。

我正在使用 Node 和 Express 后端,它使用的是 MongoDB 数据库,该数据库使用 mongoose 来访问它。

updateTodo: {
  type: TodoType,
  args: {
    id: {
      type: new GraphQLNonNull(GraphQLID)
    },
    action: {
      type: new GraphQLNonNull(GraphQLString)
    }
  },
  resolve(parent, args) {
    return Todo.updateOne({ // updateOne is a MongoDB/mongoose function
      _id: args.id
    }, {
      $set: {
        action: args.action
      }
    });
  }
}

我得到了什么

{
  "data": {
    "updateTodo": {
      "id": null,
      "action": null
    }
  }
}

来自以下

mutation {
  updateTodo(id: "5c18590fa6cd6b3353e66b06", action: "A new Todo") {
    id
    action
}

我之后会这样做

{
  todos{
    id
    action
  }
}

我明白了

{
  "data": {
    "todos": [
      {
        "id": "5c18590fa6cd6b3353e66b06",
        "action": "A new Todo"
      }
    ]
  }
}

所以我知道它正在工作,但更愿意获得新的数据返回。

更多信息

const TodoType = new GraphQLObjectType({
  name: 'Todo',
  fields: () => ({
    id: {
      type: GraphQLID
    },
    action: {
      type: GraphQLString
    },
    isCompleted: {
      type: GraphQLBoolean
    },
    user: {
      type: UserType,
      resolve(parent, args) {
        return User.findById(parent.userId);
      }
    }
  })
});

导入到文件中。

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const todoSchema = new Schema({
  action: String,
  isCompleted: Boolean,
  userId: String
})

module.exports = mongoose.model('Todo', todoSchema);

这里是 github 存储库,您可以查看代码 https://github.com/petersrule/graphql-node-express-boilerplate

【问题讨论】:

  • 可以添加Todo.updateOne的代码吗?
  • 感谢您的评论。我添加了一条注释来阐明该功能,并在底部添加了一些代码来阐明“Todo”是什么。如果您需要更多,请告诉我。再次感谢!

标签: mongodb mongoose graphql graphql-js express-graphql


【解决方案1】:

请尝试在您的解析器函数中使用以下更新的代码进行更新,“{new: true}”有助于从 mongoDB 返回更新的对象。我希望这会有所帮助。

updateTodo: {
  type: TodoType,
   args: {
     id: {
      type: new GraphQLNonNull(GraphQLID)
     },
     action: {
      type: new GraphQLNonNull(GraphQLString)
     }
   },
 resolve(parent, args) {
    return new Promise((resolve, reject) => {
        Todo.findOneAndUpdate({ // updateOne is a MongoDB/mongoose function
            "_id": args.id
          }, {
            $set: {
              "action": args.action
            }
          }, {
            new: true // This makes sure the return result is the updated information
          })
          .then((result) => {
            return resolve(result);
          })
          .catch((err) => {
            return reject(err);
          })
      })
      .then((finalResult) => {
        return finalResult;
      })
      .catch((err) => {
        return err;
      })
  }
}

请告诉我结果。

【讨论】:

  • 我真的认为这会奏效。但它甚至没有改变数据库中的条目。尽管我们还没有成功,但感谢您的帮助。我也考虑过做一个承诺,但从未尝试过。我将再试一次并稍微更改您的代码。我认为您的代码没有指定更新。
  • Gautam Mailik,我有一个问题希望你能回答。即使您使用了 findOneAndUpdate 函数,当我在 Graphiql 中执行 updateTodo 调用时,它也会在终端中给我这个警告,“(node:13964) DeprecationWarning: collection.findAndModify 已弃用。请改用 findOneAndUpdate、findOneAndReplace 或 findOneAndDelete。”如何解决问题,以免收到警告?
  • @petersrule 修复警告传递另外 {new: true, useFindAndModify: false}。同时,我还将尝试复制该问题以进行更新。我希望这会对您有所帮助。
猜你喜欢
  • 2018-05-12
  • 1970-01-01
  • 2018-08-04
  • 2017-08-17
  • 2017-12-11
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多