如何在需要return语句的GraphQL解析器中调用异步node.js函数? [英] How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement?

查看:107
本文介绍了如何在需要return语句的GraphQL解析器中调用异步node.js函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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解析器函数必须返回值或解析为该值的promise值即可。所以我试图做这样的事情:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆