首页 > 解决方案 > GraphQL:查询:无法从 MongoDb 获取数据

问题描述

我收到此错误:

"errors": [
    {
      "message": "Type Query must define one or more fields."
    }
  ]

当我运行查询时:

{
  user{

   _id 
    name
    username
    password
    email
    date_inscription
    profession

  }
}

然后我有同样的例子 https://blog.cloudboost.io/a-crud-app-with-apollo-graphql-nodejs-express-mongodb-angular5-2874111cd6a5

标签: javascriptmongodbexpress-graphql

解决方案


此错误表明您的查询未返回任何字段。确保在 graphql/queries/user/user.js 下至少定义了一个字段,如下所示:

const UserListType = new GraphQLObjectType({ name: 'UserList', fields() { return { id: { type: GraphQLInt, description: "Unique identifier of the User" } } } })

要检查的另一部分是您已正确导出 graphql/queries/index.js 中的查询,如下所示:

// Users
const listUsers = require('./user/listUsers')

module.exports = {

  // Users
  listUsers
} 

推荐阅读