【发布时间】:2020-09-21 08:58:45
【问题描述】:
GraphQL 是 MongoDB Stitch 的一项新功能,我知道它处于测试阶段,因此提前感谢您的帮助。我对直接在 Stitch 中使用 GraphQL 感到很兴奋,所以我希望我可能只是忽略了一些东西。
返回 Payload 的 documentation 显示使用 bsonType,但是当实际输入有效负载类型的 JSON Schema 时,它会要求您使用“type”而不是“bsonType”。它仍然使用“bsonType”对我来说很奇怪,只要至少有一个属性使用“type”。
下面是函数:
const mongodb = context.services.get("mongodb-atlas");
const collection = mongodb.db("<database>").collection("<collection>");
const query = { _id: BSON.ObjectId(input.id) }
const update = {
"$push": {
"notes": {
"createdBy": context.user.id,
"createdAt": new Date,
"text": input.text
}
}
};
const options = { returnNewDocument: true }
collection.findOneAndUpdate(query, update, options).then(updatedDocument => {
if(updatedDocument) {
console.log(`Successfully updated document: ${updatedDocument}.`)
} else {
console.log("No document matches the provided query.")
}
return {
_id: updatedDocument._id,
notes: updatedDocument.notes
}
})
.catch(err => console.error(`Failed to find and update document: ${err}`))
}
这是客户解析器中的输入类型:
"type": "object",
"title": "AddNoteToLeadInput",
"required": [
"id",
"text"
],
"properties": {
"id": {
"type": "string"
},
"text": {
"type": "string"
}
}
}
下面是有效载荷类型:
{
"type": "object",
"title": "AddNoteToLeadPayload",
"properties": {
"_id": {
"type": "objectId"
},
"notes": {
"type": "array",
"items": {
"type": "object",
"properties": {
"createdAt": {
"type": "string"
},
"createdBy": {
"type": "string"
},
"text": {
"type": "string"
}
}
}
}
}
}
当输入错误的“类型”时,错误状态:
预期的有效值为:[array boolean integer number null object string]
当输入错误的“bsonType”时,错误状态:
预期有效值为:[string object array objectId boolean bool null regex date timestamp int long decimal double number binData]
我已经尝试了所有我能想到的组合,包括将所有“bsonType”更改为“type”。我还尝试在使用“type”时将 _id 更改为字符串,或者在使用“bsonType”时将 objectId 更改为字符串。无论我在使用突变时尝试哪种组合,它都会执行应有的操作并将注释添加到前导中,但返回的有效负载始终显示为空。我需要它返回 _id 并注意,以便它在前端更新 Apollo 中的 InMemoryCache。
【问题讨论】:
标签: mongodb graphql apollo-client mongodb-stitch