I'm developing an app using GraphQL in the backend and I want to implement a face comparison method. I am using the facepp npm package, which gives me an API to perform the comparison. The problem I am having is that when my resolver runs, it finishes before it gets a response from facepp. How can I make it return the response from facepp?
module.exports = {
Mutation: {
checkFace: async () => {
console.log("Checking.....");
let confidence;
var parameters = {
image_url1: "link to image 1",
image_url2: "link to image 2",
};
facepp.post("/compare", parameters, function (err, res) {
if (!err) {
confidence = res.confidence;
//return res.confidence doesn't work
} else {
confidence = "There was an error"
}
});
return confidence
},
},
};
After a couple of trials and researches, I came up with a working solution. The problem in my original code was that I didn't correctly handle the asynchronous handler of the facepp.post method. In case if someone runs into the same issue, below is a working implementation of my code.
module.exports = {
Mutation: {
checkFace: async () => {
let confidence;
const callCheck = () => {
return new Promise((resolve, reject) => {
var parameters = {
image_url1: "link to image 1",
image_url2: "link to image 2",
};
facepp.post("/compare", parameters, function (err,
res) {
if (!err) {
confidence = res.confidence;
resolve();
} else {
confidence = "error";
reject();
}
});
});
};
await callCheck();
return confidence;
},
},
};