Why is my apolloFetch call returning an empty query when called from within a promise.all?

风流意气都作罢 提交于 2019-12-25 03:12:46

问题


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
};

回答1:


It looks like apolloFetch requires query - you are passing p

change

Promise.all( promises.map(p=>apolloFetch({p})) )

to

Promise.all( promises.map(query=>apolloFetch({query})) )

You also call resolve twice

To resolve all errors or success

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)
});



回答2:


You immediately resolve or rejects once the pool.query() callback starts:

if(err){ reject(err);}resolve(results);

So unless the query fails, you never resolve with the results from the apolloFetch calls, since the promise is already resolved with the pool.query() results. I guess you're missing an else block:

if( err ) {
  reject();
}
else {
  const promises = ...
}

PS: you can try using node.js' util.promisify() to turn pool.query() into a promise as well so you can just write something resembling: query(...).then(results=>results.map(apolloFetch) instead of ahving to mix callbacks and promises.



来源:https://stackoverflow.com/questions/54908549/why-is-my-apollofetch-call-returning-an-empty-query-when-called-from-within-a-pr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!