Reputation: 5206
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:
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?
(less important) How do I add the Date.now()
to get a new dateUpdated:
value?
How do I get the return value or what ever it is that I can get a response that isn't updateAlbum: null
? Thank you!
Upvotes: 1
Views: 5229
Reputation: 5206
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()
Upvotes: 3