GithubHelp home page GithubHelp logo

joinswoop / relay-compose Goto Github PK

View Code? Open in Web Editor NEW

This project forked from errorpro/relay-compose

0.0 3.0 0.0 32 KB

This is HOC for relay modern to work with сomposable components.

JavaScript 100.00%

relay-compose's Introduction

Relay-compose

This is a HOC for relay modern to work with сomposable react components.

You probably want to use this when you work with smart and dumb components and you need to compose relay data fetching in your smart component and pass it down to dumb component.

Setup

npm install --save relay-compose

Set relay environment using setEnvironment in your entry point. For example in client.js:

import { setEnvironment } from 'relay-compose';
import relayEnv from './createRelayEnvironment'

setEnvironment(relayEnv);

And now you are ready to use it.

Examples

FragmentContainer

import {
  graphql,
} from 'react-relay';
import { fragment } from 'relay-compose';

import Persons from './Persons';

export default compose(
  fragment(graphql`
    fragment PersonsContainerDesc on Person @relay(plural: true) {
      id
      title
    }
  `),
  connect(mapProps, mapDispatch, mergeProps),
)(Persons);

Query renderer(root)

import {
  graphql,
} from 'react-relay';
import { queryRenderer } from 'relay-compose';

import PersonsInfoPage from './PersonsInfoPage';
import { PersonsContainer } from '../Persons';

export default compose(
  queryRenderer(graphql`
    query PersonsInfoPageContainerQuery {
      Person {
        ...PersonsContainerDesc
      }
    }
  `),
  mapProps(props => ({
    persons: <PersonsContainer data={props.Person} />,
  })),
)(PersonsInfoPage);

Mutations

import { createMutation } from 'relay-compose';

export default compose(
  mapProps(props => ({
    onSubmit: (data) => {
      createMutation(graphql`
        mutation MyComponentContainerMutation($input: MyInput!) {
          createUser(input: $input) {
            clientMutationId
          }
        }
      `, {
        variables: {
          input: data,
        },
        configs: [{
          type: 'RANGE_ADD',
          ...myConfig,
        }],
      }).then(res => console.log(res);
    },
  })),
  reduxForm({
    form: 'MyForm',
  }),
)(MyForm);

RefetchContainer

import { queryRenderer, refetchContainer } from 'relay-compose';

export default compose(
  queryRenderer(graphql`
    query appQuery {
      viewer {
        ...Test_viewer
      }
    }
  `),
  refetchContainer(
    {
      viewer: graphql.experimental`
        fragment Test_viewer on User
        @argumentDefinitions(
          name: { type: String }
        ) {
          id
          firstName
          lastName
        }
      `,
    },
    graphql.experimental`
      query TestQuery($name: String!) {
        viewer {
          ...Test_viewer @arguments(name: $name)
        }
      }
    `,
  ),
)(Test);

PaginationContainer

import { queryRenderer, paginationContainer } from 'relay-compose';

export default compose(
  queryRenderer(
    query songsContainerQuery(
      $count: Int!
      $cursor: String
    ) {
      ...songsContainer
    }
  `),
  paginationContainer(
    fragment songsContainer on Query {
      songs(
        first: $count,
        after: $cursor,
      ) @connection(key: "songsContainer_songs") {
        edges {
          node {
            audioId,
            name,
            coverImageUrl,
            artist,
            likes,
            dislikes,
          }
        }
      }
    }
  `),
  {
    direction: 'forward',
    query: graphql`
      query songsContainerForwardQuery(
        $count: Int!
        $cursor: String
      ) {
        ...songsContainer,
      }
    `,
    getVariables: (_, { count, cursor }) => ({
      count,
      cursor,
    }),
  }),
)(Test);

Information

This project is still in WIP. You are welcome to participate to it.

relay-compose's People

Contributors

errorpro avatar gabrielbull avatar quinnbaetz avatar

Watchers

 avatar  avatar  avatar

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.