Kevin Danikowski
Kevin Danikowski

Reputation: 5206

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:

    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:

  1. How should I write the updateAlbum function?

  2. (less important) How do I add the Date.now() to get a new dateUpdated: value?

  3. 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

Answers (1)

Kevin Danikowski
Kevin Danikowski

Reputation: 5206

  1. See Below. You need to add the "$set" to put the set of parameters you will be changing.
    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)
                        })
                    })
                }
            }
  1. The date needs to be made into a string, keep with Date() instead of Date.now() to leave same as mongoose format.
  2. See above, you need to add res into resolve()

Upvotes: 3

Related Questions