[英]How to make Update Mutation - GraphQL (plus mongoDB)
I would like to do an update mutation to albums, but I'm having issues getting the update and getting the return value.我想对专辑进行更新突变,但在获取更新和获取返回值时遇到问题。
Schema Mongoose:模式猫鼬:
var AlbumSchema = new Schema({
name: String,
dateCreated: { type: Date, default: Date.now },
dateUpdated: { type: Date, default: Date.now }
});
GraphQL Type: GraphQL 类型:
const AlbumType = new GraphQLObjectType({
name: 'Album',
fields: () => ({
id: {type:GraphQLID},
name: {type:GraphQLString},
dateCreated: {type: GraphQLString},
dateUpdated: {type: GraphQLString}
})
})
Update Mutation:更新突变:
updateAlbum: { //STILL TRYING TO GET TO WORK
type: AlbumType,
args: {
id: {type: new GraphQLNonNull(GraphQLString)},
name: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parentValue, args){
return new Promise((resolve, reject) => {
ALBUM.findOneAndUpdate(
{id: args.id},
{name: args.name},
//{dateUpdated: Date.now()}
).exec((err, res) => {
if(err) reject(err)
else resolve()
})
})
}
}
Test Mutation:测试突变:
mutation {
updateAlbum(id:"5a695c586c050802f182270c", name: "album 333"){
id
name }}
Test Mutation's Response:测试突变的响应:
{
"data": {
"updateAlbum": null
}
}
Questions:问题:
How should I write the updateAlbum
function?我应该如何编写updateAlbum
函数?
(less important) How do I add the Date.now()
to get a new dateUpdated:
value? (不太重要)如何添加Date.now()
以获取新的dateUpdated:
值?
How do I get the return value or what ever it is that I can get a response that isn't updateAlbum: null
?我如何获得返回值或者我可以获得不是updateAlbum: null
的响应? Thank you!谢谢!
updateAlbum: {
type: AlbumType,
args: {
id: {type: new GraphQLNonNull(GraphQLString)},
name: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(parentValue, args){
return new Promise((resolve, reject) => {
const date = Date().toString()
ALBUM.findOneAndUpdate(
{"_id": args.id},
{ "$set":{name: args.name, dateUpdated: date}},
{"new": true} //returns new document
).exec((err, res) => {
console.log('test', res)
if(err) reject(err)
else resolve(res)
})
})
}
}
res
into resolve()
见上,需要在resolve()
添加res
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.