Home

Awesome

Regql

version downloads MIT License

GraphQL Client in Pure ReasonML. Stupid simple, magic-free client backed by plain-old fetch. Inspired by reason-apollo.

RoadMap

In the near future these are the planned additional features:

Install

yarn add regql

bsconfig

"bs-dependencies": [
  "reason-react",
  "bs-fetch",
  "bs-json",
  "regql"
]

Usage

Instantiate the client and pass it configuration:

// Gql.re
module Client = Regql.Create({
  let uri = "http://localhost:8000/graphql"
});

Create a query

let query = {|
    query getUser {
      name
    }
  |};

Define the response shape

type user = {name: string};
type data = {user: user};

Define Decoder for your response

let user = (json) =>
  Json.Decode.{
    name: json |> field("name", string),
  };

let data = (json) =>
  Json.Decode.{
    user: json |> field("user", user)
  };

Define Container configuration

module Container = {
  type shape = data;
  type variables; /* or some type `type variables = {"one": 1};` if used */
  let decoder = data;
};

Pass Container configuration to Gql.Client

module FetchUserName = Gql.Client(Container);

Use the FetchUserName Component

render: (_) =>
<FetchUserName query>
  ((response) => {
    switch response {
       | Loading => <div> (ReasonReact.stringToElement("Loading")) </div>
       | Failed(error) => <div> (ReasonReact.stringToElement(error)) </div>
       | Loaded(result) =><div> (ReasonReact.stringToElement(result.user.name)) </div>
  })
</FetchUserName>