我正在尝试将新对象添加到嵌入式对象中,我看到请求已正确发送(POST 200 OK),但最终未创建对象
这是我拥有的mongodb结构
{
"data": {
"practices": [
{
"_id": "5d3477b38f0bc920ce6b6fe0",
"name": "some name",
"createdBy": "John",
"description": "desc",
"methodologies": [
"method 1"
],
"standards": [
{
"requirementId": "123",
"standardId": 333,
"assignedBy": "Mike",
"scores": null
}
],
"updated_date": "2019-08-22T05:59:29.975Z"
}
]
}
}
我想添加分数对象:
{
createdBy: {
type: GraphQLString
},
score: {
type: GraphQLBoolean
},
comment: {
type: GraphQLString
}
}
这是突变部分:
addScoreToPracticeInStandard: {
type: practiceType,
args: {
id: {
name: 'id',
type: new GraphQLNonNull(GraphQLString)
},
standards: {
type: GraphQLList(assignedToPracticeRequirementInputType)
},
scores: {
type: new GraphQLList(assignedToPracticeScoreInputType)
},
requirementId: {
type: GraphQLNonNull(GraphQLString)
},
createdBy: {
type: GraphQLNonNull(GraphQLString)
},
score: {
type: GraphQLNonNull(GraphQLBoolean)
},
comment: {
type: GraphQLNonNull(GraphQLString)
}
},
resolve(root, params) {
return PracticeModel.update({
"id": params.id,
"standards.requirementId": params.requirementId
},
{
$push: {
"standards.$.scores": {
"createdBy": params.createdBy,
"score": params.score,
"comment": params.comment
}
}
},
function (err) {
if (err) return next(err);
}
);
发送后
mutation {
addScoreToPracticeInStandard(id: "5d3477b38f0bc920ce6b6fe0",requirementId: "123", standards: [], scores: [], createdBy: "Janko", score: true, comment: "komentarz sierpiowy")
{
_id
name
createdBy
description
methodologies
standards
{
requirementId
standardId
scores{
createdBy
score
comment
}
}
updated_date
}
}
我看到的回应
{
"data": {
"addScoreToPracticeInStandard": {
"_id": null,
"name": null,
"createdBy": null,
"description": null,
"methodologies": null,
"standards": null,
"updated_date": null
}
}
}
任何想法,我在做什么错了?
最后,我如下解决了该问题:
resolve(root, params) {
return PracticeModel.findById(params.id).exec()
.then(PracticeModel.update({standards: {$elemMatch: {"requirementId": params.requirementId}}},
{
$addToSet: {
"standards.$.scores":
{
createdBy: params.createdBy,
score: params.score,
comment: params.comment
}
},
$set: {updated_date: new Date()},
}
,
function (err) {
if (err) return next(err);
})
);
}
},