Skip to content

chenglou/reason-apollo

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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>

About

Reason binding for Apollo Client and React Apollo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • OCaml 100.0%