• Stars
    star
    339
  • Rank 124,632 (Top 3 %)
  • Language ReScript
  • Created over 5 years ago
  • Updated about 1 month ago

Reviews

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

Repository Details

Use Relay with ReScript.

rescript-relay

Use Relay with ReScript.

Join our Discord

Getting started

Are you using version >= 0.13.0 and ReScript syntax with VSCode? Make sure you install our dedicated VSCode extension. Note: It only works with ReScript syntax.

Check out the documentation.

Also, check out the changelog - things will continue to change 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().

/* Avatar.res */
module UserFragment = %relay(`
  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.res */
module UserFragment = %relay(`
  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() and include the fragments needed to render the entire tree of components.

/* Dashboard.res */
module Query = %relay(`
  query DashboardQuery {
    me {
      ...UserProfile_user
    }
  }
`)

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

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

Examples

More Repositories

1

graphql-query-test-mock

Easily mock GraphQL queries in your Relay Modern / Apollo / any-other-GraphQL-client tests.
JavaScript
60
star
2

resgraph

Build implementation-first GraphQL servers in ReScript
OCaml
59
star
3

rescript-bun

Use Bun with ReScript.
ReScript
55
star
4

res-x

A ReScript framework for building server-driven web sites and applications. Use familiar tech like JSX and the component model from React, combined with simple server driven client side technologies like HTMX. Built on Bun and Vite.
ReScript
48
star
5

rescript-edgedb

Use EdgeDB fully type safe in ReScript. Embed EdgeQL right in your ReScript source code.
ReScript
44
star
6

reasonml-q-a

ReasonML Q & A - Questions and answers for everything ReasonML.
HTML
44
star
7

relay-modern-flow-jest-example

Example of testing a Relay Modern app using Flow, Jest, react-testing-library and graphql-query-test-mock.
JavaScript
38
star
8

vscode-graphiql-explorer

Use GraphiQL + GraphiQL Explorer to build your GraphQL operations, right from inside of VSCode.
TypeScript
37
star
9

relay-utils

Utilities for working with Relay (modern) in general, and the Relay store in particular.
JavaScript
35
star
10

graphql-client-example-server

A simple GraphQL server for powering examples of various GraphQL clients in various languages.
TypeScript
34
star
11

relay-store-types-generator

Generate types for the Relay store from your GraphQL schema.
TypeScript
19
star
12

rescript-embed-lang

A general purpose PPX and library for embedding other languages into ReScript, via code generation.
ReScript
16
star
13

enzyme-async-helpers

Async helpers for testing React components with Enzyme.
JavaScript
16
star
14

rescript-relay-router

ReScript
16
star
15

bs-tailwind-ppx

JavaScript
16
star
16

vscode-rescript-relay

TypeScript
14
star
17

graphql-generate-flow-schema-assets

Generate Flow assets from GraphQL schema.
JavaScript
13
star
18

vscode-reasonml-graphql

Tighter integration between ReasonML and GraphQL in VSCode.
TypeScript
12
star
19

json-to-flowtype-generator

Generate Flow types from JSON or static JS objects/arrays.
JavaScript
10
star
20

relay-recipes

Relay Recipes - Recipes for doing common (and uncommon) things in Relay.
HTML
10
star
21

rescript-relay-starter

A starter template for RescriptRelay.
ReScript
10
star
22

rescript-bun-starter

A template project for building with ReScript and Bun.
ReScript
9
star
23

reason-relay-playground

Reason
7
star
24

vscode-reason-relay

VSCode extension for working with ReasonRelay.
TypeScript
7
star
25

relay-compiler-language-js-flow-uncommented

Language plugin for Relay that generates the same output as the default compiler, but with Flow types as actual source and not inside comments.
JavaScript
6
star
26

rescript-relay-recipes

Recipes for RescriptRelay.
TypeScript
5
star
27

rescript-relay-github-example

An example of RescriptRelay, built on the GitHub GraphQL API.
JavaScript
4
star
28

resgraph-template

Get started quickly with a new ResGraph project.
ReScript
3
star
29

rescript-relay-github-action

A GitHub action for RescriptRelay.
1
star
30

flow-enum-validator

Simple type safe validation of unknown strings to enums using Flow.
JavaScript
1
star