Skip to content
This repository has been archived by the owner on Nov 26, 2019. It is now read-only.

4Catalyzer/relay-decorators

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

relay-decorators npm

Utility decorators for Relay components.

Discord

Guide

Installation

$ npm install relay-decorators

The @poll decorator sets up a wrapper component that uses setInterval to repeatedly call props.relay.forceFetch at a fixed interval (specified in milliseconds). This is not an ideal way to get updating data from a GraphQL server, but is convenient if you don't have proper subscriptions.

import React from 'react';
import Relay from 'react-relay';
import poll from 'relay-decorators/lib/poll';

// type Widget {
//   size: Int
// }

@poll(1000) // Poll every second.
class WidgetSize extends React.Component {
  static propTypes = {
    widget: React.PropTypes.object.isRequired,
  };

  render() {
    return (
      <div>The current widget size is: {this.props.widget.size}.</div>
    );
  }
}

export default Relay.createContainer(WidgetSize, {
  fragments: {
    widget: () => Relay.QL`
      fragment on Widget {
        size
      }
    `,
  },
});

If a polling component has a parent with the same poll interval, the @poll decorator will coalesce the poll timeouts to enable the queries to be batched.