【发布时间】:2021-06-01 02:57:37
【问题描述】:
关于在 apollo 插件中使用 async/await 有什么建议吗?我正在尝试等待 twilio 服务承诺并遇到Can not use keyword 'await' outside an async function babel 解析器错误,并且不确定如何将父函数转换为异步。这是基本布局:
export const twilioVerification = async () => {
return {
requestDidStart () {
return {
willSendResponse ({ operationName, response, context }) {
if (['UpdateUser', 'CreateUser', 'SignInByPhone'].includes(operationName)) {
const user = response.data[operationName];
if (user != null) {
await sendVerificationText(user.phoneNumber);
}
}
}
}
},
}
};
上面的代码抛出了BABEL_PARSE_ERROR。我尝试了多种方法将异步添加到willSendResponse 和/或requestDidStart,结果混合不成功。作为参考,这也是我实例化 ApolloServer 的方式:
const server = new ApolloServer({
context: { driver, neo4jDatabase: process.env.NEO4J_DATABASE },
schema: schema,
introspection: process.env.APOLLO_SERVER_INTROSPECTION,
playground: process.env.APOLLO_SERVER_PLAYGROUND,
plugins: [
pushNotifications(firebase),
twilioVerification(),
]
})
【问题讨论】:
标签: node.js express graphql apollo apollo-server