为什么我的 apolloFetch 调用在 promise.all 中调用时返回一个空查询?
Why is my apolloFetch call returning an empty query when called from within a promise.all?
我正在尝试在 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})) )
你也调用了两次resolve
解决所有错误或成功
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)
});
一旦 pool.query()
回调开始,您立即解决或拒绝:
if(err){ reject(err);}resolve(results);
因此,除非查询失败,否则您永远不会使用 apolloFetch 调用的结果进行解析,因为承诺已经使用 pool.query() 结果进行了解析。我猜你错过了一个 else
块:
if( err ) {
reject();
}
else {
const promises = ...
}
PS:你可以尝试使用 node.js' util.promisify() 将 pool.query()
变成一个承诺,这样你就可以写类似的东西:query(...).then(results=>results.map(apolloFetch)
而不是混合回调和承诺。
我正在尝试在 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})) )
你也调用了两次resolve
解决所有错误或成功
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)
});
一旦 pool.query()
回调开始,您立即解决或拒绝:
if(err){ reject(err);}resolve(results);
因此,除非查询失败,否则您永远不会使用 apolloFetch 调用的结果进行解析,因为承诺已经使用 pool.query() 结果进行了解析。我猜你错过了一个 else
块:
if( err ) {
reject();
}
else {
const promises = ...
}
PS:你可以尝试使用 node.js' util.promisify() 将 pool.query()
变成一个承诺,这样你就可以写类似的东西:query(...).then(results=>results.map(apolloFetch)
而不是混合回调和承诺。