https://github.com/kennetpostigo/regql
Reason Graphql
https://github.com/kennetpostigo/regql
client graphql reason
Last synced: 10 months ago
JSON representation
Reason Graphql
- Host: GitHub
- URL: https://github.com/kennetpostigo/regql
- Owner: kennetpostigo
- License: mit
- Created: 2017-11-22T04:06:00.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-05-31T04:24:08.000Z (over 7 years ago)
- Last Synced: 2025-04-03T18:55:34.910Z (10 months ago)
- Topics: client, graphql, reason
- Language: JavaScript
- Homepage:
- Size: 41 KB
- Stars: 19
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# Regql
[](http://npm.im/regql)
[](http://npm-stat.com/charts.html?package=regql)
[](http://opensource.org/licenses/MIT)
GraphQL Client in Pure [ReasonML](https://reasonml.github.io). Stupid simple,
magic-free client backed by plain-old fetch. Inspired by
[reason-apollo](https://github.com/Gregoirevda/reason-apollo).
## RoadMap
In the near future these are the planned additional features:
* [ ] integration with [graphql_ppx](https://github.com/mhallin/graphql_ppx)
* [ ] Cache queries/requests
* [ ] Optimistic Updates
## Install
```bash
yarn add regql
```
## bsconfig
```json
"bs-dependencies": [
"reason-react",
"bs-fetch",
"bs-json",
"regql"
]
```
## Usage
#### Instantiate the client and pass it configuration:
```reason
// Gql.re
module Client = Regql.Create({
let uri = "http://localhost:8000/graphql"
});
```
#### Create a query
```reason
let query = {|
query getUser {
name
}
|};
```
#### Define the response shape
```reason
type user = {name: string};
type data = {user: user};
```
#### Define Decoder for your response
```reason
let user = (json) =>
Json.Decode.{
name: json |> field("name", string),
};
let data = (json) =>
Json.Decode.{
user: json |> field("user", user)
};
```
#### Define Container configuration
```reason
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
```reason
module FetchUserName = Gql.Client(Container);
```
### Use the FetchUserName Component
```reason
render: (_) =>
((response) => {
switch response {
| Loading =>
(ReasonReact.stringToElement("Loading"))
| Failed(error) => (ReasonReact.stringToElement(error))
| Loaded(result) => (ReasonReact.stringToElement(result.user.name))
})
```