首页 > 解决方案 > 我在来自数据库的graphql中从resolve返回的每个字段都为null

问题描述

我有这个问题,当我运行任何从数据库(mongodb)获取数据的根查询时,响应的每个字段都是空的

我已经尝试使用预定义的硬编码 json 并且它适用于它但是当我将 mongoose 承诺放入解析中时,它在响应的每个字段中返回 null 当通过突变将查询添加到数据库并返回数据时,我可以看到添加的信息我当我运行一个promise并且控制台在resolve中记录Book.find({})的输出时也可以看到它

定义查询的根查询的一部分

    books: {
            type: BookType,
            resolve(parent, args) {
                return Book.find({})
            }
        }

GraphQL 类型

const BookType= new GraphQLObjectType({
    name: 'BookType',
    fields: () => ({
        _id: {type: GraphQLID},
        name: {type: GraphQLString},
        genre: {type: GraphQLString},
        authorid: {type: GraphQLString}
    })
});

const bookSchema = new mongoose.Schema({
    name: String,
    genre: String,
    authorid: String
})

我的查询

{
    templates {
      name
      genre
      authorid
    }
}

这就是我得到的回应

{
  "data": {
    "books": {
      "name": null,
      "genre": null,
      "authorid": null
    }
  }
}

我应该看到数据库中的数据,因为我有一些条目

标签: javascriptexpressmongoosegraphql

解决方案


推荐阅读