解决Graphql的异步函数无法返回在具有rdf数据的Stardog服务器上查询的数据 [英] Resolve asynchronous function of Graphql not able to return data queried over Stardog server having rdf data

查看:117
本文介绍了解决Graphql的异步函数无法返回在具有rdf数据的Stardog服务器上查询的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用下面的graphql查询stardog服务器是我的代码。

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;

查询成功执行,我可以在控制台上查看结果,但返回data.results.bindings; 内部函数(数据)未将此结果返回到名词下的类型系统解析:(obj)=> {console.log(obj);返回的
obj 显示为null而不是结果绑定从GraphQL的查询返回。如果有人能帮助我弄清楚我在这里缺少什么,那就太棒了。

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.

提前致谢,
Yashpal

Thanks in advance, Yashpal

推荐答案

在您的查询中,解析函数名词 field是一个异步操作(查询部分)。但是你的代码是同步的。因此,实际上不会立即从resolve函数返回任何内容。这导致没有任何内容传递给 Noun GraphQL对象类型的resolve函数。这就是你在打印 obj 时得到null的原因。

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.

如果在<$ c $中进行异步操作c>解析函数,您必须返回一个与您的预期结果一起解析的promise对象。您还可以使用ES7 async / await功能;在这种情况下,你必须声明 resolve:async(_,{noun_value})=> {//等待代码}

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}.

使用 Promise ,代码如下所示:

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

这篇关于解决Graphql的异步函数无法返回在具有rdf数据的Stardog服务器上查询的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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