All files / src/hocs graphqlQuery.js

6.67% Statements 1/15
0% Branches 0/8
25% Functions 1/4
8.33% Lines 1/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32            2x                                                  
import * as React from 'react';
import gql from 'graphql-tag';
import {Query} from 'react-apollo';
import {List} from 'react-content-loader';
 
export default function withQuery(Com) {
  return class ComWithQuery extends React.Component {
    render() {
      const {graphql, variables, transformData, ...restProps} = this.props;
      return (
        <Query query={gql`${graphql}`} variables={variables}>
          {({loading, error, data, ...graphqlProps}) => {
            if (loading) return <List style={{maxWidth: '600px'}}/>;
            if (error) return `Error!: ${error}`;
            const key = Object.keys(data)[0];
            let value = data[key];
            if (Array.isArray(value)) {
              // delete symbol in every item to let vega works
              value = value.map(v => ({...v}));
            }
            if (transformData) {
              value = transformData(value);
            }
            return (
              <Com value={value} {...restProps} {...graphqlProps} />
            );
          }}
        </Query>
      );
    }
  }
}