Home

Awesome

NOTE

The reason-apollo bindings can be found in the official apollographql repository: https://github.com/apollographql/reason-apollo

Reason-apollo

Easily use the Apollo Client 2 with ReasonML

Install

npm || yarn
yarn add reason-apollo
or
npm install reason-apollo
bsconfig

In bsconfig.json, add reason-apollo to your bs-dependencies:

"bs-dependencies": [
  "reason-react",
  "reason-apollo"
]

Usage

here is a repository showing the usage of the package.

Create your Client

Apollo.re

module Client = ReasonApollo.Create({ let uri = "http://localhost:3010/graphql" });

Fetching data
Query

Create a query with the graphql-tag

let query =
  gql {|
    query getUser {
      name
    }
  |} [@bs];
Defining the data structure of the result
type user = {. "name": string};
type data = {. "user": user};
Optional variables passed to the query
let variables = {
  "limit": 2
};
All in a module

data structure of the response and optional variables should be represented in a module

module Config = {
  type responseType = data;
  type variables = {. limit: int}; /* or `type variables;` if none are used */
};

Passing the configuration to the Apollo Client
module FetchUserName = Apollo.Client.Query(Config);
Executing the query

someFile.re

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