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

查看:18
本文介绍了解决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;

查询已成功执行,我能够在控制台上看到结果,但 return data.results.bindings; 内部 function(data) 没有将此结果返回给下的名词类型系统解析:(obj) =>{ console.log(obj);}并且返回的 obj 显示 null 而不是从 GraphQL 查询返回的结果 bindings.如果有人能帮我弄清楚我在这里遗漏了什么,那就太好了.

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.

提前致谢,亚什帕尔

推荐答案

在你的查询中,名词字段的resolve函数是一个异步操作(查询部分).但是你的代码是同步的.因此,解析函数实际上没有立即返回任何内容.这导致没有任何内容传递给 Noun GraphQL 对象类型的解析函数.这就是为什么在打印 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.

resolve 函数中的异步操作的情况下,您必须返回一个使用预期结果解析的 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天全站免登陆