我尝试在 Node.js 微服务的 Promise.all
内使用 apolloFetch
,但不断收到查询为空的错误。使用 apolloFetch
的原因是调用另一个微服务并向其传递一组查询。有人可以给我一些指导吗?我的代码如下:
const uri = "dsc.xxx.yyyy.com/abc/def/graphql";
const apolloFetch = CreateApolloFetch({uri});
const QryAllBooks = {
type: new GraphQLList(BookType),
args: {},
resolve() {
return new Promise((resolve, reject) => {
let sql = singleLineString`
select distinct t.bookid,t.bookname,t.country
from books_tbl t
where t.ship_status = 'Not Shipped'
`;
pool.query(sql, (err, results) => {
if (err) {
reject(err);
}
resolve(results);
const str = JSON.stringify(results);
const json = JSON.parse(str);
const promises = [];
for (let p = 0; p < results.length; p++) {
const book_id = json[p].bookid;
const query = `mutation updateShipping
{updateShipping
(id: ${book_id}, input:{
status: "Shipped"
})
{ bookid
bookname }}`;
promises.push(query);
}
//Below is the Promise.all function with the
//apolloFetch that calls another graphql endpoint
//an array of queries
Promise.all(promises.map(p => apolloFetch({p}))).then((result) => {
//this is the problem code^^^^^^^^^^^^^^^^^^^^^
resolve();
console.log("success!");
}).catch((e) => {
FunctionLogError(29, "Error", e);
});
});
});
}
};
module.exports = {
QryAllBooks,
BookType
};
最佳答案
看起来 apolloFetch 需要 query
- 您正在传递 p
改变
Promise.all( promises.map(p=>apolloFetch({p})) )
至
Promise.all( promises.map(query=>apolloFetch({query})) )
您还调用了两次解析
解决所有错误或成功
const final_results = []
Promise.all(promises.map(query => apolloFetch({
query,
}))).then((result) => {
final_results.push(result)
}).catch((e) => {
final_results.push(e)
}).then(() => {
resolve(final_results)
});
关于javascript - 为什么我的 apolloFetch 调用在从 Promise.all 中调用时返回空查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54908549/