最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement? - Stack

programmeradmin0浏览0评论

The Hello World example provided on graphql/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:

What I'm trying to do:

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);
});

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.

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:

What I'm trying to do:

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);
});

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.

Share Improve this question asked May 12, 2017 at 5:29 TheEnvironmentalistTheEnvironmentalist 2,8522 gold badges23 silver badges48 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 19

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.

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);
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论