Created
August 9, 2019 17:27
-
-
Save johnwesonga/ac3e2f2dc90d7ca52fcd3533e4fcb6f4 to your computer and use it in GitHub Desktop.
reasonml+graphql
This file contains hidden or 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
module GetDrivers = [%graphql | |
{| | |
query getDrivers{ | |
all_drivers { | |
id | |
name | |
} | |
} | |
|} | |
]; | |
module Driver = { | |
[@react.component]; | |
let make = (~driver) => | |
switch (driver##name) { | |
| Some(name) => <div> {ReasonReact.string(name)} </div> | |
| None => React.null | |
}; | |
}; | |
module Drivers = { | |
[@react.component]; | |
let make = (~drivers) => | |
<div> | |
{ | |
React.array( | |
Array.of_list(List.map(driver => <Driver driver />, drivers)), | |
) | |
} | |
</div>; | |
}; | |
module GetDriversQuery = ReasonApollo.CreateQuery(GetDrivers); | |
[@react.component] | |
let make = () => | |
<GetDriversQuery> | |
...{ | |
({result}) => | |
switch (result) { | |
| Loading => <div> {ReasonReact.string("Loading")} </div> | |
| Error(error) => | |
<div> {ReasonReact.string(error##message)} </div> | |
| Data(response) => | |
response##all_drivers | |
|> Js.Option.getWithDefault([||]) | |
|> Array.map( | |
fun | |
| Some(drivers) => <Drivers drivers /> | |
| None => React.null, | |
) | |
|> ReasonReact.array | |
} | |
} | |
</GetDriversQuery>; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment