[英]graphql resolver return Cannot return null for non-nullable field from asynchronous function in nodejs
不要误会我的意思,我被迫使用瀑布流,因为我需要一个接一个地执行 function。 没有与架构或返回类型相关的问题,只是卡在异步瀑布中。 我只需要从最后的 function 之前返回。
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 的响应,因为它提前返回。 并且 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()
此外,我不熟悉异步库,但您实际上应该在您的示例中返回async.waterfall()
调用,因为根据文档返回undefined
。
也许只是
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 库
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.