Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

apollo-react-relay-pagination

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-react-relay-pagination

This is a helper library to write less code when using relay style pagination with apollo and react.

  • 1.0.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
17
increased by54.55%
Maintainers
1
Weekly downloads
 
Created
Source

apollo-react-relay-pagination

This library provides an abstraction for relay style pagination for apollo and react.

Installation

yarn add apollo-react-relay-pagination

or

npm install --save apollo-react-relay-pagination

Usage

Wrapping the whole component

This should be your preferred method of integration. Just enhance your component using relayPagination and then use the loadMore method from the props as shown below.

import { relayPagination } from "apollo-react-relay-pagination";


class ItemsBase extends React.Component {
  render() {
    ...
  }

  // call this whenever you want to load more
  // elements (e.g. from componentDidUpdate)
  load (reload) {
    if (!this.state.loadingMore) {
      if (reload || (...check for hasNextPage...)) {
        this.setState(p => ({ ...p, loadingMore: true }));
        this.props.loadMore(reload, {
        after: reload ?
            null :
            this.props.data.viewer.comments.pageInfo.endCursor
        });
        this.setState(p => ({ ...p, loadingMore: false }));
      }
    }
  }
}

const ItemsWithPagination =
  relayPagination(ItemsQuery, ItemsBase);
const ItemsWithData =
  graphql(ItemsQuery)(ItemsWithPagination);

Using the merge function directly

The library additionally provides the function mergeResults that merges a new response containing a paginated list into a previous version of the same answer. You can use this to have more flexibility.

import { mergeResults } from "apollo-react-relay-pagination";


class ItemsBase extends React.Component {
  render() {
    ...
  }
}

export function getProps(props) {
  props.loadMore = (reload) => {
    let variables = ...;
    if (!reload) {
      variables.after = props.data.viewer.profile.groups.pageInfo.endCursor;
    }

    return props.data.fetchMore({
      query: ItemsQuery,
      variables,
      updateQuery: (previousResult, { fetchMoreResult }) => {
        return mergeResults(previousResult, fetchMoreResult, reload)
      },
      });
  };
  return props;
}

const ItemsWithData =
  graphql(ItemsQuery, {
    props: getProps,
})(ItemsWithPagination);

Exported functions

relayPagination(query, Component)

Returns a HOC that should be wrapped with graphql itself and will provide a loadMore method as property to the defined Component.

  • query: the query to use for the loadMore method
  • Component: the component to wrap

mergeResults(previous, current, reload)

Merges two objects by overwriting all the values from previous with the ones from current besides the children that are named edges, which will be merged. If reload is true, then all edges from previous are discarded.

Keywords

FAQs

Package last updated on 13 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc