简体   繁体   中英

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!

  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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM