title: How do I call an asynchronous node.js function from within a GraphQL resolver requiring a return statement?
body: <p>The Hello World example provided on <a href="http://graphql.org/graphql-js" rel="noreferrer">graphql.org/graphql-js</a> for creating a simple GraphQL implementation is the following:</p>
  
  <pre><code>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: () =&gt; {
          return 'Hello World!';
      }
  };
  
  // Run the GraphQL query '{ hello }' and print out the response
  graphql(schema, '{ hello }', root).then((response) =&gt; {
      console.log(response);
  });
  </code></pre>
  
  <p>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:</p>
  
  <p>What I'm trying to do:</p>
  
  <pre><code>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: () =&gt; {
          getData((data) =&gt; {
              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) =&gt; {
      console.log(response);
  });
  </code></pre>
  
  <p>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 <code>getData</code> 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.</p>