Apollo Server:如何基于回调发送响应?

时间:2019-08-14 14:59:13

标签: graphql 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? */ }); } ), }, }; ,它是基于回调的。

1 个答案:

答案 0 :(得分:1)

您可以使用Promise:

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