我正在尝试在函数中调用简单的grpc请求,但我不知道如何等待响应并将其传递到grpc调用之外。
应该是这样的:
Client: (parent, args, context, info) =>{
client.Get({thing, objects, properties}, function(err, response) {
//how to return the response outside?
});
//await for the response function.
};
是否可以在client.Get函数之外以某种方式返回响应? 有人有建议吗? 谢谢您的指教!
答案 0 :(得分:1)
您有两种方法:
new Promise((resolve, reject) => client.Get({thing, objects, properties}, function(err, response) {
if(err) {
return reject(err)
}
resolve(response)
}))
const grpc_promise = require('grpc-promise');;
grpc_promise.promisifyAll(client);
然后,您可以将此承诺的gRPC客户端传递给GraphQL上下文。 (猜测您是基于函数签名使用的GraphQL。)
然后,您可以在GraphQL解析器中返回承诺。
Client: (parent, args, context, info) =>{
return context.client.Get({thing, objects, properties})
};