graphiql 突变请求成功但 returns 为空

The graphiql mutation request succeeds but returns null

当我对使用我的 GraphQL 服务器代码的 graphiql 进行突变时,它 return 对于对象中的所有条目都是空的。

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

所以我知道它正在工作,但更愿意获取新数据 return。

更多信息

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

请尝试在您的解析器函数中使用以下更新代码进行更新,“{new: true}”有助于 return 来自 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;
      })
  }
}

请告诉我结果。