Coder Social home page Coder Social logo

rxgx / with-graph-data Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 1.0 712 KB

An HoC, higher-order component, that handles the loading and error conditions for a GraphQL query HoC.

License: MIT License

TypeScript 96.18% JavaScript 3.82%
graphql reactjs hoc typescript

with-graph-data's Introduction

Note: recompose is deprecated, hard to test, and not good with Typescript

USE HOOKS INSTEAD! They are easier and essentially vanilla functions. For example:

function ProductsPreloader(props) {
  const { query, variables } = props

  const { loading, error, data } = useQuery(query, { variables })

  if (loading) return <LoadingComponent />
  if (error || !data) return <ErrorComponent error={error || 'Failed to load products'} />
  return <ProductsDisplay products={data.products} />
}

with-graph-data

An HoC, higher-order component, that handles the loading and error conditions for a GraphQL query HoC.

Installation

Install the module:

npm install @rxgx/with-graph-data

Usage

import { compose } from 'recompose';
import { graphql } from 'apollo-client';
import withGraphData from '@rxgx/with-graph-data';

function ErrorComponent() {
  return <p>Error happened</p>;
}

function LoadingComponent() {
  return <p>Now loading...</p>;
}

export default compose(
  graphql(query),
  withGraphData(LoadingComponent, ErrorComponent)
};

The function withGraphData takes two parameters:

  • LoadingComponent

    A React component that will display when props.data.loading is true.

  • ErrorComponent

    A React component that will display when props.data.error exists. The component will receive the props.data.error directly passed from Apollo client.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.