I'm setting up a GraphQL resolver to call a Braintree endpoint. The Braintree npm package wants to call their endpoint with code that looks like this:
braintreeGateway.customer.create({
firstName: "Jen",
lastName: "Smith",
company: "Braintree",
email: "jen@example.com",
phone: "312.555.1234",
fax: "614.555.5678",
website: "www.example.com"
}, function (err, result) {
result.success;
result.customer.id;
});
GraphQL resolvers return promises. I'm trying to figure out how to promisify this callback, and include it within a promise resolver.
I've read a lot of SO posts about promisifying a callback, but the ones I've found so far don't seem to quite match up to this use case.
I've tried a lot of things, this being the latest:
getBrainTreeCustomerId: (parent, args, context) => {
const userid = context.userId;
const braintreeCustomerCreate = util.promisify(braintreeGateway.customer.create);
async function run_braintreeCustomerCreate() {
try {
let braintreeCustomerId = await braintreeCustomerCreate({
firstName: "Jen",
lastName: "Smith",
company: "Braintree",
email: "jen@example.com",
phone: "312.555.1234",
fax: "614.555.5678",
website: "www.example.com"
});
return braintreeCustomerId
}
catch (err) {
console.log('ERROR:', err);
}
}
return Promise.resolve()
.then(() => {
let braintreeCustomerId = (async () => {
let braintreeCustomerId = await run_braintreeCustomerCreate()
return braintreeCustomerId;
})();
return braintreeCustomerId;
})
.then((braintreeCustomerId) => {
return braintreeCustomerId;
})
.catch((err) => {
console.log(err);
});
}
}
But the catch handler gets an error saying "Cannot read property '_createSignature' of undefined".
What's the correct syntax to use here?