Last active
November 14, 2017 18:36
-
-
Save lpalmes/abe42b2cab2db9fa45575998588a35f3 to your computer and use it in GitHub Desktop.
React Relay pseudo bindings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { graphql } from 'react-relay' | |
const query = graphql` | |
query AppQuery(id: ID!) { | |
user(id: $id) { | |
id | |
name | |
} | |
}` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type error = string; | |
type user = Js.t {. id: string, name: string}; | |
type props = Js.t {. user: user}; | |
type renderProps = Js.t {. error : error, props : Js.null_undefined props}; | |
<ReactRelay.QueryRenderer | |
environment | |
variables={"id": "1"} | |
query=[%bs.raw {| require("./__generated__/AppQuery.graphql.js") |}] //You have to precompile your query and use it here, there is a problem with babel and reason bindings, i haven't figured it out yet | |
render=( | |
fun (renderProps: renderProps) => { | |
let queryProps = Js.Null_undefined.to_opt renderProps##props; | |
switch queryProps { | |
| None => <h1>(ReasonReact.stringToElement "Loading")</h1> | |
| Some props => | |
<h1>(ReasonReact.stringToElement props##user##name)</h1> | |
} | |
} | |
) | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type environment; | |
type graphqlTag; | |
module QueryRenderer = { | |
type renderProps 'error 'props = Js.t {. error : 'error, props : 'props}; | |
external queryRenderer : ReasonReact.reactClass = | |
"QueryRenderer" [@@bs.module "react-relay"]; | |
let make | |
(type error props) | |
environment::(environment: environment) | |
query::(query: graphqlTag) | |
render::(render: renderProps error props => ReasonReact.reactElement) | |
variables::(variables: 'variables) | |
children => | |
ReasonReact.wrapJsForReason | |
reactClass::queryRenderer | |
props::{ | |
"environment": environment, | |
"query": query, | |
"render": render, | |
"variables": variables | |
} | |
children; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First you have to run the relay-compiler so it can compile the query inside the App.js file, then you can import it from example.re file, is really ugly, i know, we have to work around the relay compiler and babel plugins