当尝试运行以下查询时,出现以下错误:“无法为非空字段User.first_name返回null。”。我不希望结果为空。
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存储在可以返回的数组中,但是我的用户类型中的任何其他内容都返回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,但是无济于事,我得到的最远的是使用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 },
})
答案 0 :(得分:2)
如果您使用的是MongoDB和mongoose,则需要使用populate
来加入您的收藏集。为此,在您的架构中,您要填充的字段不仅需要具有类型,还需要具有ref
属性,该属性可以告诉猫鼬使用哪个模型填充该字段,例如:>
// 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。