Skip to content

Instantly share code, notes, and snippets.

@johnwesonga
Created August 9, 2019 17:27
Show Gist options
  • Save johnwesonga/ac3e2f2dc90d7ca52fcd3533e4fcb6f4 to your computer and use it in GitHub Desktop.
Save johnwesonga/ac3e2f2dc90d7ca52fcd3533e4fcb6f4 to your computer and use it in GitHub Desktop.
reasonml+graphql
module GetDrivers = [%graphql
{|
query getDrivers{
all_drivers {
id
name
email
}
}
|}
];
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