I have a pretty complicated node.js microservice that utilizes graphql as well, to first query a database, then populate an array with the returning values, and then run a graphql mutation inside an await and Promise.all call. With the following code I keep getting an error: 'await has to be used inside an async function' - but, I already have everything wrapped inside an asyn function! Can someone point out what I'm doing wrong? (PS: there might be other errors as well that I'm not seeing because I can't get past the first error)
Note: this question is not a duplicate - the other question had a problem with the function name - in my function the name is resolve() - there is no additional function name.
const QryAllBooks = {
type: new GraphQLList(BookType),
args: {},
async 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 );
}
//I need an await function so that apolloFetch
//goes in sequence of bookid, one after the other
await Promise.all(Object.values(input).map(async (promises, i) => {
apolloFetch({ promises[i]});
})
.catch(( e ) => {
errorLogger( 29, 'Error', e );
)};
});
});
}
};
module.exports = {
QryAllBooks,
BookType
};