【问题标题】:how to pass two queries in resolve function GraphQL?如何在解析函数 GraphQL 中传递两个查询?
【发布时间】:2019-04-18 16:57:24
【问题描述】:

今天我在解析函数 (GraphQL) 中的 db(drop) 操作中遇到问题。代码如下。

dropAuthor: {
type: authorType,
            args: {id: idType},
            async resolve(parent, args){
                Book.deleteMany({authorId: args.id}).find();
                return Author.deleteOne({_id: args.id});
            }
}

GraphiQL 查询如下

mutation{  
  dropAuthor(id:"5bed7c23bff55c1086a7b4d4"){
    id
  }
}

每当我尝试删除作者表单集合时,它只删除作者,并且记录的书籍保持不变。这段代码有什么问题?

【问题讨论】:

    标签: mongodb express graphql mongoose-schema


    【解决方案1】:
    dropAuthor: {
      type: authorType,
      args: { id: idType },
      async resolve(parent, args){
        let deleteBooks = Book.deleteMany({ authorId: args.id }).find();
        let deleteAuthor = Author.deleteOne({ _id: args.id });
        return Promise.all([deleteBooks, deleteAuthor]);
      }
    }
    

    试试这个代码。

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 2021-06-07
      • 1970-01-01
      • 2016-12-12
      • 2017-11-13
      • 2022-07-20
      • 1970-01-01
      • 2019-06-22
      • 1970-01-01
      相关资源
      最近更新 更多