graphql 解析器 return 不能 return null 对于 nodejs 中异步函数的非空字段

graphql resolver return Cannot return null for non-nullable field from asynchronous function in nodejs

不要误会我的意思,我被迫使用瀑布流是因为我需要一个接一个地执行函数。没有与架构或 return 类型相关的问题,只是卡在异步瀑布中。我所需要的只是 return 从最后一个函数开始。

const async = require('async')
module.exports = {
logout : ()=>{
        return async.waterfall([
            callback => {
                setTimeout(() => {
                    let data = 1;
                    return callback(null, data)
                }, 2000);
            },
            (data, callback) => {
                setTimeout(() => {
                    return callback(null, data+1)
                }, 2000);
            }
            
        ], (err, res)=>{
            console.log(res)
            return res
        })
    }
}

来自 graphiql 的回复,因为它 return 早。 console.log 正在工作

{
  "errors": [
    {
      "message": "Cannot return null for non-nullable field RootMutation.logout.",
      "locations": [
        {
          "line": 4,
          "column": 3
        }
      ],
      "path": [
        "logout"
      ]
    }
  ],
  "data": null
}

这里可以用async/await吗?沿着下面的路线

async function logout(){
       let data = await new Promise((resolve,reject) => {setTimeout(() => { resolve(1)},2000)});
       
         data = await new Promise((resolve,reject) => {setTimeout(() => { resolve(data + 1)},2000)});
return data;
}

async function foo() {
    let res = await logout();
  alert(res)
}

foo()

此外,我不熟悉异步库,但根据文档,您实际上是否应该像 returns undefined 那样返回示例中的 async.waterfall() 调用。

也许只是

const async = require('async')
module.exports = {
logout : ()=>{
        async.waterfall([
            callback => {
                setTimeout(() => {
                    let data = 1;
                    return callback(null, data)
                }, 2000);
            },
            (data, callback) => {
                setTimeout(() => {
                    return callback(null, data+1)
                }, 2000);
            }
            
        ], (err, res)=>{
            console.log(res)
            return res
        })
    }
}

如果没有,也许也可以分享您正在使用的 GraphQL 库