Resolve asynchronous function of Graphql not able

2019-06-02 01:07发布

I am trying to query a stardog server using graphql below is my code.

import {
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLInt,
  GraphQLString,
  GraphQLList,
  GraphQLNonNull,
  GraphQLID,
  GraphQLFloat
} from 'graphql';

import axios from 'axios';

var stardog = require("stardog");

let Noun = new GraphQLObjectType({
      name: "Noun",
      description: "Basic information on a GitHub user",
      fields: () => ({
          "c": {
       type: GraphQLString,
       resolve: (obj) => {
        console.log(obj);
          }
        }
     })
});

const query = new GraphQLObjectType({
      name: "Query",
      description: "First GraphQL for Sparql Endpoint Adaptive!",
      fields: () => ({
        noun: {
          type: Noun,
          description: "Noun data from fibosearch",
          args: {
            noun_value: {
              type: new GraphQLNonNull(GraphQLString),
              description: "The GitHub user login you want information on",
            },
          },
          resolve: (_,{noun_value}) => {
              var conn = new stardog.Connection();

              conn.setEndpoint("http://stardog.edmcouncil.org");
              conn.setCredentials("xxxx", "xxxx");
                conn.query({
                    database: "jenkins-stardog-load-fibo-30",
                    query: `select ?c  where {?s rdfs:label '${noun_value}'. ?c rdfs:subClassOf ?s}`,  
                    limit: 10,
                    offset: 0
                },
                function (data) {
                       console.log(data.results.bindings);
                       return data.results.bindings;
                });  
              }
            },
          })
      });

const schema = new GraphQLSchema({
  query
});

export default schema;

Query is executed successfully and I am able to see the result on console but return data.results.bindings; inside function(data) is not returning this result to the Noun type system under the resolve: (obj) => { console.log(obj); } and the obj returned is showing null instead of the result bindings returned from the query of GraphQL. It would be great if someone could help me to figure out what I am missing here.

Thanks in advance, Yashpal

1条回答
Juvenile、少年°
2楼-- · 2019-06-02 01:38

In your query, the resolve function of noun field is an asynchronous operation (the query part). But your code is synchronous. So, nothing actually gets returned immediately from the resolve function. This results in nothing passed to the resolve function of Noun GraphQL object type. That's why you're getting null when you print obj.

In case of asynchronous operations in resolve functions, you have to return a promise object that resolves with your intended result. You can also use ES7 async/await feature; in that case, you have to declare resolve: async (_, {noun_value}) => { // awaited code}.

With Promise, the code will look like below:

resolve: (_,{noun_value}) => {
  var conn = new stardog.Connection();

  conn.setEndpoint("http://stardog.edmcouncil.org");
  conn.setCredentials("xxxx", "xxxx");
  return new Promise(function(resolve, reject) {
    conn.query({
      database: "jenkins-stardog-load-fibo-30",
      query: `select ?c  where {?s rdfs:label '${noun_value}'. ?c rdfs:subClassOf ?s}`,  
      limit: 10,
      offset: 0
    }, function (data) {
      console.log(data.results.bindings);
      if (data.results.bindings) {
        return resolve(data.results.bindings);
      } else {
        return reject('Null found for data.results.bindings');
      }
    });
  });
}
查看更多
登录 后发表回答