Skip to content

relay-tools/relay-connection-handler-plus

Repository files navigation

Relay Connection Handler Plus! npm

Relay connection handler with additional functionality.

Usage

import ConnectionHandler from 'relay-connection-handler-plus';
import { Environment } from 'relay-runtime';
import RelayDefaultHandlerProvider from "relay-runtime/lib/handlers/RelayDefaultHandlerProvider";

function handlerProvider(handle) {
  switch (handle) {
    case 'connection':
      return ConnectionHandler;
    default:
      return RelayDefaultHandlerProvider(handle);
  }
}

const environment = new Environment({
  handlerProvider,
  /* ... */
});

Relay Connection Handler Plus! provides an enhanced connection handler that can be used in place of the default connection handler.

This connection handler exposes an additional getConnections method. This method allows getting all connections for a connection key, per facebook/relay#1861:

const connections = ConnectionHandler.getConnections(record, connectionKey);

The getConnections method also allows getting a subset of connections for a given connection key that were fetched with args matching a filter function. For example, it can be used to get all connections loaded with color: "red", regardless of the other arguments on the connection:

const connections = ConnectionHandler.getConnections(
  record,
  connectionKey,
  ({ color }) => color === 'red',
);