首页 > 解决方案 > Apollo Server:如何根据回调发送响应?

问题描述

我目前正在尝试使用此包验证应用内购买的 iOS 收据:https ://github.com/Wizcorp/node-iap

这是我不完整的解析器:

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? */
        });
      }
    ),
  },
};

如果它是异步的并且我没有响应对象,如何在此处返回响应?通常,我只是习惯于返回模型返回的任何内容。但是,这次我使用node-iap的是基于回调的。

标签: graphqlapollo-server

解决方案


你可以使用一个承诺:

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

推荐阅读