【问题标题】:Issue with graphQL in nodejs , null returned for customised type in graphQL节点 js 中的 graphQL 问题,为 graphQL 中的自定义类型返回 null
【发布时间】:2017-08-24 16:46:42
【问题描述】:

我目前正在使用 nodejs 探索 graphQL,我正在尝试构建一个简单的示例,如下所示:

var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema } = require('graphql');

var schema = buildSchema(`
type Query {
getUser(jid: String ): [User],
albums:[Album],
}

type User{
name:String,
image:String,
albums:[Album]
}

type Album{
name:String,
image:String
}

`);

class Album {
    constructor() { }
    name() { return 'ablum name' }
    image() { return 'image url' }
}

class User {
    constructor(jid) {
        this.jid = jid;
        console.log(this.jid);

    }
    ablums() {

        return [new Album, new Album];

    };
    name() {
        console.log(this.jid);
        return "name";
    }
    image() { return "url"; }
}

var root = {

    getUser: function ({ jid }) {
        return [new User(jid), new User(jid)];
    },

};

var app = express();
app.use('/graphql', graphqlHTTP({
    schema: schema,
    rootValue: root,
    graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');

我期待一个带有两个用户数组的 JSON 结果,每个数组都包含另外两个专辑数组,其中包含它的名称和图像。但是我在专辑字段中得到了 null,它应该显示两个专辑的数组,其中包含名称和图像字段。

{
"data": {
"getUser": [
  {
    "name": "name of abc is michael",
    "image": "url",
    "albums": null
  },
  {
    "name": "name of abc is michael",
    "image": "url",
    "albums": null
  }
 ]
}
}

请帮忙!我对 graphQL 完全陌生,我认为这是一项非常有用的技术!

【问题讨论】:

  • 请编辑您的问题以正确缩进您的代码。它很难阅读,因此很难为您提供帮助。

标签: node.js graphql


【解决方案1】:

这是您的用户类的拼写错误,您的方法称为 ablums 但您的查询正在寻找一个名为专辑的方法/属性,该方法/属性不存在,因此返回 null。

【讨论】:

    【解决方案2】:

    这是一个使用 Node 的示例,用于使用 Hasura 的现有服务器(顺便说一句,这很棒)。以下代码只是为了从 Hasura/Postgres 模式(或任何 GraphQL 服务器)获取 JSON 响应。一旦你将数据放入一个变量中,你就可以使用 express 来提供它。我正在努力,但可能有几个例子。我可以稍后分享。还可以查看 Hasura 订阅,它使用开箱即用的实时通信,因此数据库上的每个更改都会发送给客户端。这是一个链接:https://github.com/hasura/nodejs-graphql-subscriptions-boilerplate

    在此示例中,只需更改 gql 查询以匹配您的架构和查询。

    希望这会有所帮助.. 问候!

    var { ApolloClient } = require('apollo-client');
    var { InMemoryCache } = require('apollo-cache-inmemory');
    var { HttpLink } = require('apollo-link-http');
    var fetch = require('node-fetch');
    
    const cache = new InMemoryCache();
    const link = new HttpLink({
      uri: 'http://localhost:8081/v1/graphql/', fetch: fetch
    });
    
    const client = new ApolloClient({
      cache,
      link
    });
    const gql = require('graphql-tag');
    
    var query = gql`
    query planeacion {
        lineas (where: { linea : { _eq: "123" }}){
        descripcion
        linea
      }
    }
    
    `
    client
      .query({
        query: query
      })
      .then(result => console.log(JSON.stringify(result.data)));
    
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-01
      • 2020-12-19
      • 2019-03-18
      • 2017-09-18
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多