使用 GraphQL 更新数据失败

Posted

技术标签:

【中文标题】使用 GraphQL 更新数据失败【英文标题】:Failed Update data with GraphQL 【发布时间】:2019-04-17 07:40:14 【问题描述】:

现在我正在尝试更新现有数据宽度

const Mutation = new GraphQLObjectType(
    name: 'Mutation',
    fields: 
        updateUser: 
            type: UserType,
            args: 
                id:  type: GraphQLID ,
                email:  type: GraphQLString ,
            ,
            resolve(parent,  id, email ) 
                console.log('email',email)
                const user = User.findById(id)
                user.email = email
                return user
            
        ,
    
)

我的突变是这样的

 mutation
   updateUser(id:"5beac69c421c6e2820a0e809", email:"8")
       id       
        email
   

 

我得到的是用户数据, 但是数据库没有变化,请帮忙

【问题讨论】:

你忘记在某处调用 use.save() 了吗? 【参考方案1】:

@Matan,你在用Mongoose吗?如果是,则必须实际更新此数据,您所做的只是改变对象并返回它。

使用MongoDB(如果您正在使用它),您可以做到:User.findOneAndUpdate( id , email ) 或接近。

如果它是另一个数据库,您必须在某个时候调用.save().update(),否则,您不会更改任何数据。

【讨论】:

以上是关于使用 GraphQL 更新数据失败的主要内容,如果未能解决你的问题,请参考以下文章