如何从需要 return 语句的 GraphQL 解析器中调用异步 node.js 函数? [英] How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement?
问题描述
graphql.org/graphql-js 上提供的用于创建简单 GraphQL 实现的 Hello World 示例是以下内容:
The Hello World example provided on graphql.org/graphql-js for creating a simple GraphQL implementation is the following:
var { graphql, buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
hello: () => {
return 'Hello World!';
}
};
// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
console.log(response);
});
我正在尝试在解析器中运行异步函数,例如数据库调用,但我似乎不知道如何使其工作:
I'm trying to run an asynchronous function within the resolver, for example a database call, and I can't seem to figure out how to make that work:
我想做什么:
var { graphql, buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
data: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
data: () => {
getData((data) => {
return data; // Returns from callback, instead of from resolver
}
}
};
// Run the GraphQL query '{ data }' and print out the response
graphql(schema, '{ data }', root).then((response) => {
console.log(response);
});
我尝试过使用承诺,但似乎没有办法在不输入回调的情况下逃避承诺.我还尝试了各种包装异步 getData
函数以强制它同步的方法,但最终不得不从异步函数返回一个值,同样的问题.我觉得这不可能这么复杂,我的意思是 GraphQL-JS 是 Facebook 写的.
I've tried to use promises, but there doesn't seem to be a way of escaping the promises without entering a callback. I've also tried various ways of wrapping the asynchronous getData
function to force it to be synchronous, but end up having to return a value from an asynchronous function, same problem. I feel like this can't be this complicated, I mean GraphQL-JS was written by Facebook.
推荐答案
原来这个问题是你弄明白之后觉得很愚蠢的问题之一,但是因为我花了很长时间来解决它,我会回答我自己的问题,希望其他人可以从中受益.
So this one turned out to be one of those problems you feel really stupid about after you figure it out, but because of how long I spent struggling with it, I'll answer my own question so someone else can hopefully benefit.
事实证明,GraphQL 解析器函数必须返回一个值或解析为该值的承诺.所以我试图用这样的东西做:
It turns out the GraphQL resolver function has to return either a value or a promise that resolves to that value. So what I was trying to do with something like this:
var { graphql, buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
data: String
}
`);
// The root provides a resolver function for each API endpoint
var root = {
data: () => {
getData((data) => {
return data; // Returns from callback, instead of from resolver
}
}
};
// Run the GraphQL query '{ data }' and print out the response
graphql(schema, '{ data }', root).then((response) => {
console.log(response);
});
可以用这样的方法来完成:
Can be done with something like this:
var { graphql, buildSchema } = require('graphql');
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
data: String
}
`);
let promiseData = () => {
return new Promise((resolve, reject) => {
getData((data) => {
resolve(data);
});
});
};
// The root provides a resolver function for each API endpoint
var root = {
data: () => {
return promiseData();
}
};
// Run the GraphQL query '{ data }' and print out the response
graphql(schema, '{ data }', root).then((response) => {
console.log(response);
});
这篇关于如何从需要 return 语句的 GraphQL 解析器中调用异步 node.js 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!