• Stars
    star
    276
  • Rank 149,319 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Relay integration for Found

Found Relay Travis npm

Relay integration for Found.

Table of Contents generated with DocToc

Usage

import { BrowserProtocol, queryMiddleware } from 'farce';
import {
  createFarceRouter,
  createRender,
  makeRouteConfig,
  Route,
} from 'found';
import { Resolver } from 'found-relay';

/* ... */

const Router = createFarceRouter({
  historyProtocol: new BrowserProtocol(),
  historyMiddlewares: [queryMiddleware],
  routeConfig: makeRouteConfig(
    <Route
      path="/"
      Component={Application}
      query={graphql`
        query app_Application_Query {
          viewer {
            ...Application_viewer
          }
        }
      `}
    >
      <Route path="widgets">
        <Route
          Component={WidgetList}
          query={graphql`
            query app_WidgetList_Query {
              widgets {
                ...WidgetList_widgets
              }
            }
          `}
          prepareVariables={prepareWidgetListVariables}
        />
        <Route
          path=":name"
          Component={Widget}
          query={graphql`
            query app_Widget_Query($name: String!) {
              widget(name: $name) {
                ...Widget_widget
              }
            }
          `}
          render={({ props }) => (props ? <Widget {...props} /> : <Loading />)}
        />
      </Route>
    </Route>,
  ),

  render: createRender({}),
});

ReactDOM.render(
  <Router resolver={new Resolver(environment)} />,
  document.getElementById('root'),
);

Examples

Guide

Installation

$ npm i -S farce found react react-relay
$ npm i -S found-relay

Router configuration

Create a router component class using createFarceRouter or a lower-level API. Create a Resolver with your Relay environment, then use that as the resolver instead of the default Found resolver.

import { BrowserProtocol, queryMiddleware } from 'farce';
import { createFarceRouter, createRender } from 'found';
import { Resolver } from 'found-relay';

/* ... */

const Router = createFarceRouter({
  historyProtocol: new BrowserProtocol(),
  historyMiddlewares: [queryMiddleware],
  routeConfig,

  render: createRender({}),
});

ReactDOM.render(
  <Router resolver={new Resolver(environment)} />,
  document.getElementById('root'),
);

Route configuration

Route configuration works similarly to that in Found, but instead of data or getData, routes accept properties that control Relay data fetching. Each route behaves as if it were its own <QueryRenderer>, except that all data fetching happens in parallel, even for nested routes. Found Relay routes accept the following properties:

  • query or getQuery: the Relay query for the route, or a method that returns the Relay query for the route
  • cacheConfig or getCacheConfig: the cache configuration for the route, or a method that returns the cache configuration for the route
  • fetchPolicy or getFetchPolicy: the fetch policy for the Relay data for the route, or a method that returns the fetch policy for the Relay data for the route; network-only (the default), store-and-network, or store-or-network
  • prepareVariables: a method to apply additional transformations to the route variables
  • render: as on Found, a method that returns the element for the route, but with additional properties

Note that Found Relay routes ignore data, getData, and defer.

query or getQuery

To inject Relay data into a route, specify query or getQuery on the route. The value should be a Relay query. In general, Component for this route will likely be a fragment container, and the query should compose the fragment or fragments from Component.

By default, the available variables for the query will be the accumulated path parameters for this route and its parents. To customize these variables or inject additional ones from the routing state, use prepareVariables as described below.

As with <QueryRenderer>, upon routing, the route will not refetch its data if its query and variables are the same. To force refetching upon navigation even when the query and variables stay the same, use prepareVariables below to add a nonce variable.

cacheConfig or getCacheConfig

As on <QueryRenderer>, this value will be forwarded directly to the network layer.

fetchPolicy

As on <QueryRenderer>, this controls the fetch policy for data for the route. In addition to network-only and store-and-network as on <QueryRenderer>, this can also take the value store-or-network, which bypasses the network fetch entirely when the data are available in the store.

prepareVariables

By default, the available variables for the route query will be the accumulated path parameters for this route and its parents. If specified, the prepareVariables callback receives the accumulated variables used from all parent routes and the current route match. It should return the updated variables for this route, which will also be accumulated into the variables used for all child routes.

const widgetListRoute = (
  <Route
    path="widgets"
    Component={WidgetList}
    query={graphql`
      query app_WidgetList_Query($color: String, $size: String, $limit: Int) {
        widgets(color: $color, size: $size, limit: $limit) {
          ...WidgetList_widgets
        }
      }
    `}
    prepareVariables={(params, { location }) => {
      const { color, size } = location.query;
      const limit = location.state && location.state.limit;

      return {
        ...params,
        color,
        size: size && parseInt(size, 10),
        limit: limit || 10,
      };
    }}
  />
);

render

This behaves identically to render in Found, except its render arguments object receives the following additional properties:

  • error: the Relay error, if any, as on render on <QueryRenderer>
  • retry: when available, a callback that will refetch the data for the route, as on <QueryRenderer>
  • environment: the current Relay environment
  • variables: an object containing the Relay variables used for the route
  • resolving: a boolean indicating whether the route is rendering as part of router navigation resolution rather than due to a subsequent store update; in general, it is only safe to throw HttpError or RedirectException instances to trigger navigation when resolving is true

If render returns a truthy value, then the rendered element will also subscribe to Relay store updates.

More Repositories

1

react-router-relay

[Deprecated] Relay Classic integration for React Router
JavaScript
559
star
2

relay-hooks

Use Relay as React hooks
TypeScript
541
star
3

react-relay-network-modern

Relay Modern Network Layer with middlewares โ€” cache, auth, retry, batch, logger, SSR
JavaScript
326
star
4

react-relay-network-layer

ReactRelayNetworkLayer with middlewares and query batching for Relay Classic.
JavaScript
276
star
5

relay-local-schema

Use Relay without a GraphQL server
JavaScript
253
star
6

relay-compiler-language-typescript

โ›”๏ธ Obsolete - A language plugin for Relay that adds TypeScript support, including emitting type definitions.
TypeScript
241
star
7

relay-subscriptions

[Deprecated] Subscription support for Relay Classic
JavaScript
176
star
8

react-relay-network-modern-ssr

SSR middleware for react-relay-network-modern
JavaScript
67
star
9

Relay.swift

Relay for GraphQL, ported to Swift and SwiftUI
Swift
65
star
10

relay-compiler-webpack-plugin

Automatically run the Relay Compiler from Webpack
JavaScript
65
star
11

relay-query-lookup-renderer

Same as Relay Modern's QueryRenderer, but will check the store for data before fetching
JavaScript
52
star
12

fetch-multipart-graphql

Cross browser function to fetch and parse streaming multipart graphql responses
JavaScript
51
star
13

vscode-apollo-relay

Simple configuration of vscode-apollo for Relay projects.
TypeScript
49
star
14

react-relay-mutation

Higher-level React mutation API for Relay
TypeScript
40
star
15

relay-commit-mutation-promise

Promise wrapper for Relay Modern's commit mutation
JavaScript
27
star
16

typescript-relayjs-examples

RelayJS typescript examples
TypeScript
24
star
17

relay-connection-handler-plus

Relay connection handler with additional functionality
TypeScript
21
star
18

relay-context-provider

JavaScript
16
star
19

relay-graphql-js

Relay tooling based on graphql-js
TypeScript
15
star
20

tslint-plugin-relay

tslint plugin for relay-compiler-language-typescript tool
TypeScript
6
star