本文介绍了当从promise.all内部调用时,为什么我的apolloFetch调用返回一个空查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Node.js微服务的Promise.all
内使用apolloFetch
,但始终收到错误消息,查询为空.使用apolloFetch
的原因是调用另一个微服务并将其传递给查询数组.有人可以给我指示吗?我的代码如下:
I'm trying to use apolloFetch
inside a Promise.all
in my Node.js microservice but keep getting an error that the query is empty. The reason for using apolloFetch
is to call another micro service and pass it an array of queries. Can someone give me some direction? My code is as follows:
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
It looks like apolloFetch requires query
- you are passing 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)
});
这篇关于当从promise.all内部调用时,为什么我的apolloFetch调用返回一个空查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!