How to show data from GraphQL server to npm react-

2019-08-20 06:19发布

问题:

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 picture query 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;

回答1:

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