regql

0.4.6 • Public • Published

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:

  • integration with graphql_ppx
  • Cache queries/requests
  • Optimistic Updates

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>

Readme

Keywords

none

Package Sidebar

Install

npm i regql

Weekly Downloads

25

Version

0.4.6

License

MIT

Last publish

Collaborators

  • kennetpostigo