简体   繁体   English

如何进行更新变异 - GraphQL(加上 mongoDB)

[英]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:问题:

  1. How should I write the updateAlbum function?我应该如何编写updateAlbum函数?

  2. (less important) How do I add the Date.now() to get a new dateUpdated: value? (不太重要)如何添加Date.now()以获取新的dateUpdated:值?

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

  1. See Below.见下文。 You need to add the "$set" to put the set of parameters you will be changing.您需要添加“$set”来放置您将要更改的参数集。
    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.日期需要做成字符串,用 Date() 而不是 Date.now() 保持与 mongoose 格式相同。
  2. See above, you need to add res into resolve()见上,需要在resolve()添加res

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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