bigpotato
bigpotato

Reputation: 27507

Apollo Server: How can I send a response based on a callback?

I am currently trying to validate iOS receipts for in app purchases using this package: https://github.com/Wizcorp/node-iap

This is my incomplete resolver:

export default {
  Query: {
    isSubscribed: combineResolvers(
      isAuthenticated,
      async (parent, args, { models, currentUser }) => {
        const subscription = await models.Subscription.find({ user: currentUser.id });

        const payment = {
          ...
        };

        iap.verifyPayment(subscription.platform, payment, (error, response) => {
          /* How do I return a response here if it is async and I don't have the response object? */
        });
      }
    ),
  },
};

How do I return a response here if it is async and I don't have the response object? Normally, I'm just used to returning whatever the model returns. However, this time I'm using node-iap and it's callback based.

Upvotes: 0

Views: 255

Answers (1)

Borre Mosch
Borre Mosch

Reputation: 4564

You can use a Promise:

const response = await new Promise((resolve, reject) => {
  iap.verifyPayment(subscription.platform, payment, (error, response) => {
    if(error){
      reject(error);
    }else{
      resolve(response);
    }
  });
});

Upvotes: 2

Related Questions