本文介绍了如何将数据从 GraphQL 服务器显示到 npm react-table?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开始使用 GraphQL 服务器Apollo 客户端.代码是用 Reactjs 编写的.现在我想使用任何查询从 GraphQL 服务器获取数据,并将数据显示到 UI 中的表中.我使用 npm react-table 作为表格.

表格可能如下所示:

如果查询响应没有数组,我可以轻松获取和显示数据.例如,我有下面的查询输入字符串:

{帐户 {名姓ID}}

无数组的查询结果

{数据": {帐户": {"firstName": "马伦","lastName": "马夸特",身份证":2}}}

在 ReactTable 组件中,我只是使用 data.account.firstName

获取数据

但是,如果查询结果有数组,我不知道如何获取数据.请看一下图片使用数组查询结果

那么如何将所有 5 个玩具的 title 显示到表格中?这是我的旧代码:

从'react'导入React;从 './Test.css' 导入 s;从'apollo-client'导入{ ApolloClient};从'apollo-link-http'导入{ HttpLink };从'apollo-cache-inmemory'导入{InMemoryCache};从节点获取"中导入获取;从'react-apollo'导入{graphql};从'graphql-tag'导入gql;从反应表"导入反应表导入 'react-table/react-table.css';const localURL = 'http://localhost:3000/graphql';const 客户端 = 新 ApolloClient({链接:新的 HttpLink({ uri: localURL, fetch }),缓存:新的 InMemoryCache()});常量列 = [{标题:身份证",访问者:id"},{标题:名字",访问者:名字"},{标题:姓氏",访问者:姓氏"}]类表扩展了 React.Component {使成为() {让{数据} = this.props;返回 (<div><h2>管理员</h2><反应表数据={[{名字:data.account.firstName,姓氏:data.account.lastName,id:data.account.id,}]}列={列}默认页面大小={10}/>

);}}const queryAccountList = gql`询问 {帐户{名姓ID}}}`const AccountListWithData = graphql(queryAccountList)(Table);导出默认 AccountListWithData;

解决方案

如您所见,您的 ReactTable 的数据道具需要一个数组(或者至少,在您的示例中,您传递的是一个包含 1 个对象的数组).

另请注意,GraphQL 返回的数据对象的形式为

 {帐户{名,姓,ID}}

所以你可以用 data={[ data.account ]} 定义你的 ReactTable 数据并得到相同的结果.

现在假设您执行 Toys 查询,那么返回的数据将采用以下形式

{所有玩具 [{ID,标题},{ID,标题},...]}

所以如果你对数据的定义保持不变(const { data } = this.props;),那么data.allToy将是一个的数组{ id, title } 对象.

因此您可以将您的玩具 ReactTable 定义为:

I've been starting working with GraphQL server and Apollo client. The code is written in Reactjs. Now I want to get data from GraphQL server using any queries, and show data to the table in the UI. I use npm react-table for table.

The table could look like this:

I can easily get and show data if the query response has no array. For example, I have the query input string below:

{
  account {
    firstName
    lastName
    id
  }
}

And query result with no array

{
  "data": {
    "account": {
      "firstName": "Marlen",
      "lastName": "Marquardt",
      "id": 2
    }
  }
}

In ReactTable component, I just fetch the data using data.account.firstName

<ReactTable
  data={[
    {
      firstName: data.account.firstName,
      lastName: data.account.lastName,
      id: data.account.id,
    }
  ]}
  columns={columns}
/>

However, if query result has array I don't know how to fetch the data. Please take a look the picturequery result with array

So how can I show all 5 toy's title to table ?This is my old code:

import React from 'react';
import s from './Test.css';
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
import fetch from 'node-fetch';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import ReactTable from 'react-table'
import 'react-table/react-table.css';

const localURL = 'http://localhost:3000/graphql';
const client = new ApolloClient({
  link: new HttpLink({ uri: localURL, fetch }),
  cache: new InMemoryCache()
});
const columns = [
  {
    Header: "ID",
    accessor: "id"
  },
  {
    Header: "First Name",
    accessor: "firstName"
  },
  {
    Header: "Last Name",
    accessor: "lastName"
  }
]

class Table extends React.Component {
  render() {
    let { data } = this.props;
    return (
      <div>
        <h2> ADMIN </h2>
        <ReactTable
          data={[
            {
              firstName: data.account.firstName,
              lastName: data.account.lastName,
              id: data.account.id,
            }
          ]}
          columns={columns}
          defaultPageSize={10}
        />
      </div>
    );
  }
}

const queryAccountList = gql`
  query {
    account{
      firstName
      lastName
      id
      }
    }
  }
`

const AccountListWithData = graphql(queryAccountList)(Table);

export default AccountListWithData;
解决方案

As you can see, your ReactTable's data prop expects an array (or at least, in your example you're passing it an array of 1 object).

Note also that the data object returned by your GraphQL is of the form

  {
    account
    {
      firstName,
      lastName,
      id
    }
  }

So you could have defined your ReactTable data with data={[ data.account ]} with the same result.

Now let's say you perform your Toys query, then the data returned will be of the form

{
  allToy [
    {
      id,
      title
    },
    {
      id,
      title
    },
    ...
  ]
}

so if your definition of data remains the same (const { data } = this.props;), then data.allToy will be an array of { id, title } objects.

So you can define your Toy ReactTable as:

<ReactTable
  data={ data.allToy }
  columns={columns}
  defaultPageSize={10}
/>

这篇关于如何将数据从 GraphQL 服务器显示到 npm react-table?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 11:03