reason-relay
Use Relay with ReasonML.
Last updated 3 months ago by _zth .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install reason-relay 
SYNC missed versions from official npm registry.

reason-relay

Use Relay with ReasonML.

Getting started

Check out the documentation (work in progress) here.

Also, check out the changelog - things will continue change some between versions (including breaking changes, although we'll try and keep them to a minimum) as we iterate and reach a stable version.

What it looks like

Your components define what data they need through [%relay.fragment].

/* Avatar.re */
module UserFragment = [%relay.fragment
  {|
  fragment Avatar_user on User {
    firstName
    lastName
    avatarUrl
  }
|}
];

[@react.component]
let make = (~user) => {
  let userData = UserFragment.use(user);

  <img
    className="avatar"
    src={userData.avatarUrl}
    alt={userData.firstName ++ " " userData.lastName}
  />;
};

Fragments can include other fragments. This allows you to break your UI into encapsulated components defining their own data demands.

Hooks to use your fragments are autogenerated for you. The hook needs a fragment reference from the GraphQL object where it was spread. Any object with one or more fragments spread on it will have a fragmentRefs prop on it, someObj.fragmentRefs. Pass that to the fragment hook.

Avatar_user is spread right on the fragment, so we pass userData.fragmentRefs to the <Avatar /> component since we know it'll contain the fragment ref for Avatar_user that <Avatar /> needs. The <Avatar /> component then uses that to get its data.

/* UserProfile.re */
module UserFragment = [%relay.fragment
  {|
  fragment UserProfile_user on User {
    firstName
    lastName
    friendCount
    ...Avatar_user
  }
|}
];

[@react.component]
let make = (~user) => {
  let userData = UserFragment.use(user);

  <div>
    <Avatar user={userData.fragmentRefs} />
    <h1> {React.string(userData.firstName ++ " " ++ userData.lastName)} </h1>
    <div>
      <p>
        {React.string(
           userData.firstName
           ++ " has "
           ++ userData.friendCount->string_of_int
           ++ " friends.",
         )}
      </p>
    </div>
  </div>;
};

Finally, you make a query using [%relay.query] and include the fragments needed to render the entire tree of components.

/* Dashboard.re */
module Query = [%relay.query
  {|
  query DashboardQuery {
    me {
      ...UserProfile_user
    }
  }
|}
];

[@react.component]
let make = () => {
  let queryData = Query.use(~variables=(), ());

  <div> <UserProfile user={queryData.me.fragmentRefs} /> </div>;
};

Examples

Current Tags

  • 0.1.0-alpha.1                                ...           alpha (a year ago)
  • 0.11.0                                ...           latest (3 months ago)

32 Versions

  • 0.11.0                                ...           3 months ago
  • 0.10.0                                ...           3 months ago
  • 0.9.2                                ...           5 months ago
  • 0.9.1                                ...           5 months ago
  • 0.9.0                                ...           5 months ago
  • 0.8.3                                ...           6 months ago
  • 0.8.2                                ...           6 months ago
  • 0.8.1                                ...           6 months ago
  • 0.8.0                                ...           6 months ago
  • 0.7.0                                ...           7 months ago
  • 0.6.0                                ...           7 months ago
  • 0.5.3                                ...           9 months ago
  • 0.5.2                                ...           9 months ago
  • 0.5.1                                ...           9 months ago
  • 0.5.0                                ...           9 months ago
  • 0.4.4                                ...           a year ago
  • 0.4.3                                ...           a year ago
  • 0.4.2                                ...           a year ago
  • 0.4.1                                ...           a year ago
  • 0.4.0                                ...           a year ago
  • 0.3.1-relay-experimental                                ...           a year ago
  • 0.3.0-relay-experimental                                ...           a year ago
  • 0.2.3-relay-experimental                                ...           a year ago
  • 0.2.2-relay-experimental                                ...           a year ago
  • 0.2.1-relay-experimental                                ...           a year ago
  • 0.2.0-relay-experimental                                ...           a year ago
  • 0.1.3                                ...           a year ago
  • 0.1.2                                ...           a year ago
  • 0.1.1                                ...           a year ago
  • 0.1.0-alpha.2                                ...           a year ago
  • 0.1.0-alpha.1                                ...           a year ago
  • 0.1.0-alpha.0                                ...           a year ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (17)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |