Easily use the Apollo Client 2 with ReasonML
yarn add reason-apollo
or
npm install reason-apollo
In bsconfig.json, add reason-apollo
to your bs-dependencies
:
"bs-dependencies": [
"reason-react",
"reason-apollo"
]
here is a repository showing the usage of the package.
Apollo.re
module Client = ReasonApollo.Create({ let uri = "http://localhost:3010/graphql" });
Create a query with the graphql-tag
let query =
gql {|
query getUser {
name
}
|} [@bs];
type user = {. "name": string};
type data = {. "user": user};
let variables = {
"limit": 2
};
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 */
};
module FetchUserName = Apollo.Client.Query(Config);
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>