返回 null 的 GraphQL 嵌套查询

GraphQL nested query returning null

尝试运行以下查询时出现此错误:"Cannot return null for non-nullable field User.first_name."。我不希望得到空结果。

Query:
{
  site(site_reference: 123456789) {
    site_name
    site_managers {
      _id
      first_name
    }
  }
}

我希望看到的是:

{
  "data": {
    "site": {
      "site_name": "Test Site",
      "site_managers": [
        {
          "_id": "5bbcd55ff7bd643be4d490fa",
          "first_name": "Claire"
        },
        {
          "_id": "5b9fa2e1f66fb32164f4d547",
          "first_name": "John"
        }
      ]
    }
  }
}

我的 MongoDB 将 _id 存储在它能够 return 的数组中,但是我的用户类型中的任何其他内容 returns null.

用户架构:

type User {
    _id: String!,
    first_name: String!,
    last_name: String!,
    role_id: Int!
    email: String!,
    password: String,
    created_date: String!,
    updated_date: String!,
    last_login_date: String,
    reset_password_token: String
  }
  type Query {
    user(_id: String!): User!
    users(role_id: Int): [User!]!
  }

用户解析器:

  Query: {
    user: (parent, args) => User.findById(args._id),
    users: (parent, args) => {
      if (args.role_id) {
        return User.find({ role_id: args.role_id })
      }
      return User.find({})
    },
  },

站点架构:

  type Site {
    site_name: String!,
    use_policy: String!,
    redirect_url: String!,
    intro_text: String!,
    logo: String!,
    address_line_1: String!,
    address_line_2: String,
    address_city: String!,
    address_county: String!,
    address_postcode: String!,
    phone: String!,
    site_reference: Int!,
    site_managers: [User],
    business_id: String!,
    connect_duration: Int!,
    created_date: String!,
    updated_date: String!,
  }

  type Query {
    site(site_reference: Int!): Site!
    sites(business_id: Int!): [Site!]!
  }

站点解析器:

  Query: {
    site: (parent, args) =>
      Site.findOne({ site_reference: args.site_reference }),
    sites: (parent, args) => Site.find({ business_id: args.business_id }),
  },

我想我需要在我的解析器中为网站查询做更多的事情,但我不确定具体是什么。我已经尝试使用 mongoose.populate 但无济于事,我得到的最远的是 return 使用 populate 方法创建一个空数组。

提前感谢您的宝贵时间。

编辑: 这是我的猫鼬模式

用户架构

const UserSchema = new mongoose.Schema({
  first_name: { type: String, required: true },
  last_name: { type: String, required: true },
  role_id: { type: Number, required: true },
  email: { type: String, required: true },
  password: { type: String },
  created_date: { type: Date, default: Date.now, required: true },
  updated_date: { type: Date, default: Date.now, required: true },
  last_login_date: { type: Date },
  reset_password_token: { type: String },
})

站点架构:

const SiteSchema = new mongoose.Schema({
  site_name: { type: String, required: true },
  use_policy: {
    type: String,
    required: true,
    default: config.defaultUsePolicy,
  },
  redirect_url: {
    type: String,
    required: true,
    default: config.defaultRedirect,
  },
  intro_text: {
    type: String,
    required: true,
    default: config.defaultIntroText,
  },
  logo: { type: String, required: true, default: config.defaultLogo },
  address_line_1: { type: String, required: true },
  address_line_2: String,
  address_city: { type: String, required: true },
  address_county: { type: String, required: true },
  address_postcode: { type: String, required: true },
  phone: { type: String, required: true },
  site_reference: { type: String, required: true },
  site_managers: {type:[mongoose.Schema.Types.ObjectId], required: true}
  business_id: { type: mongoose.Schema.Types.ObjectId, required: true },
  connect_duration: { type: Number, required: true, default: 0 },
  created_date: { type: Date, default: Date.now, required: true },
  updated_date: { type: Date, default: Date.now, required: true },
})

如果您使用 MongoDB 和 mongoose,则需要使用 populate 加入您的 collections。为此,在您的架构中,您要填充的字段不仅需要有一个类型,还需要一个 ref 属性 来告诉 mongoose 使用哪个模型来填充该字段,因为示例:

// Note the actual ref should be whatever name you passed to mongoose.model
site_managers: [{type:mongoose.Schema.Types.ObjectId, ref: 'User'}]

现在,在解析器中使用 populate

site: (parent, { site_reference }) => {
  return Site.findOne({ site_reference }).populate('site_managers').exec()
}

有关更详细的示例,请参阅 docs