如何让graphql输出客户端的这个对象列表?

时间:2017-06-22 22:40:39

标签: graphql

我有一个graphql服务器,我正在尝试实现一个查询,它将使用节点列表更新客户端。

在nodes.js中我有:

export const nodes = {
  "nodes": [
    {id: 1, "name": 'building_1', "hasStrobe": true},
    {id: 2, "name": 'building_2', "hasStrobe": false},
    {id: 3, "name": 'building_3', "hasStrobe": true}
  ]
}

我在schema.js中的查询如下:

const Query = new GraphQLObjectType({
  name: 'Query',
  description: 'Root query object',
  fields() {
    return {
      message: {
        type: Message,
        resolve() {
          return getMessage();
        },
      },
      system: {
        type: System,
        resolve() {
          return getSystemInfo();
        },
      },
      nodes: {
        type: Nodes,
        resolve() {
          //console.log(nodes.nodes)
          return nodes.nodes;
        }
      },

      // alert: {
      //   type: Alert,
      //   resolve() {
      //     return 'poke';
      //   }
      // }
    };
  },
});

我的节点类型如下:

const Nodes = new GraphQLObjectType({
  name: 'Nodes',
  description: 'List of zone nodes and their capabilities.',
  fields() {
    return {
      nodes: {
        type: GraphQLString,
        resolve(msg) {
          console.log(msg)
          return msg;
        }
      },
    }
  }
});

运行查询时,它会将nodes.nodes正确记录到控制台:

[ { id: 1, name: 'building_1', hasStrobe: true },
  { id: 2, name: 'building_2', hasStrobe: false },
  { id: 3, name: 'building_3', hasStrobe: true } ]

查询输出为:

{
  "data": {
    "system": {
      "uname": "4.11.5-2-ck-haswell\n",
      "time": "Thu Jun 22 2017 17:39:29 GMT-0500 (CDT)"
    },
    "nodes": {
      "nodes": "[object Object],[object Object],[object Object]"
    }
  }
}

我不确定如何以一种可以输出节点的方式处理数据数组。

0 个答案:

没有答案