【问题标题】:How to make Update Mutation - GraphQL (plus mongoDB)如何进行更新突变 - GraphQL(加上 mongoDB)
【发布时间】:2018-07-04 07:37:54
【问题描述】:

我想对专辑进行更新突变,但在获取更新和获取返回值时遇到问题。

模式猫鼬:

var AlbumSchema = new Schema({
    name: String,
    dateCreated: { type: Date, default: Date.now },
    dateUpdated: { type: Date, default: Date.now }
});

GraphQL 类型:

    const AlbumType = new GraphQLObjectType({
        name: 'Album',
        fields: () => ({
            id: {type:GraphQLID},
            name: {type:GraphQLString},
            dateCreated: {type: GraphQLString},
            dateUpdated: {type: GraphQLString}
        })
    })

更新突变:

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

测试突变:

mutation {
  updateAlbum(id:"5a695c586c050802f182270c", name: "album 333"){
    id
   name }}

测试突变的反应:

{
  "data": {
    "updateAlbum": null
  }
}

问题:

  1. updateAlbum函数应该怎么写?

  2. (不太重要)如何添加Date.now() 以获得新的dateUpdated: 值?

  3. 我如何获得返回值,或者我可以获得不是updateAlbum: null 的响应是什么?谢谢!

【问题讨论】:

    标签: node.js mongodb express mongoose graphql


    【解决方案1】:
    1. 见下文。您需要添加“$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. 日期需要做成一个字符串,用 Date() 代替 Date.now() 保持与猫鼬格式相同。
    2. 见上图,需要将res加入resolve()

    【讨论】:

      猜你喜欢
      • 2021-12-02
      • 2019-05-24
      • 2022-01-10
      • 2020-08-21
      • 2016-05-30
      • 2020-05-30
      • 2018-08-03
      • 2021-02-17
      • 2018-09-03
      相关资源
      最近更新 更多