Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.

Commit c7875e0

Browse files
committedApr 23, 2019
Initial commit
0 parents  commit c7875e0

10 files changed

+4795
-0
lines changed
 

‎.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
.merlin
3+
.bsb.lock
4+
lib/
5+
*.bs.js
6+
yarn-error.log

‎.npmignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
.merlin
3+
.bsb.lock
4+
lib/

‎LICENSE

Whitespace-only changes.

‎README.md

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# reason-apollo-hooks
2+
3+
Reason bindings for https://github.com/trojanowski/react-apollo-hooks
4+
5+
## Installation
6+
7+
```
8+
yarn add reason-apollo-hooks reason-apollo
9+
```
10+
11+
Follow the installation instructions of [https://github.com/mhallin/graphql_ppx](graphql_ppx).
12+
13+
Then update your bsconfig.json
14+
15+
```diff
16+
"bs-dependencies": [
17+
...
18+
+ "reason-apollo-hooks",
19+
+ "reason-apollo"
20+
]
21+
```
22+
23+
# Available hooks
24+
25+
## useQuery
26+
27+
```reason
28+
module UserQueryConfig = [%graphql {|
29+
query UserQuery {
30+
currentUser {
31+
name
32+
}
33+
}
34+
|}];
35+
36+
module UserQuery = ReasonApolloHooks.Query.Make(UserQueryConfig);
37+
38+
[@react.component]
39+
let make = () => {
40+
/* Both variant and records available */
41+
let ( result, { data, loading, error } ) = UserQuery.use(());
42+
43+
<div>
44+
{
45+
switch(result) {
46+
| Loading => <p>{React.string("Loading...")}</p>
47+
| Data(data) =>
48+
<p>{React.string(data##currentUser##name)}</p>
49+
| NoData
50+
| Error(_) => <p>{React.string("Get off my lawn!")}</p>
51+
}
52+
}
53+
</div>
54+
}
55+
```
56+
57+
58+
## useMutation
59+
60+
```reason
61+
module ScreamMutationConfig = [%graphql {|
62+
mutation ScreamMutation($screamLevel: Int!) {
63+
scream(level: $screamLevel) {
64+
error
65+
}
66+
}
67+
|}];
68+
69+
module ScreamMutation = ReasonApolloHooks.Mutation.Make(ScreamMutationConfig);
70+
71+
[@react.component]
72+
let make = () => {
73+
/* Both variant and records available */
74+
let screamMutation = ScreaMutation.use(());
75+
let scream = (_) => {
76+
screamMutation(~variables=ScreamMutationConfig.make(~screamLevel=10, ())##variables)
77+
|> Js.Promise.then_(result => {
78+
switch(result) {
79+
| Data(data) => ...
80+
| Error(error) => ...
81+
| NoData => ...
82+
}
83+
Js.Promise.resolve()
84+
})
85+
|> ignore
86+
}
87+
88+
<div>
89+
<button onClick={scream}>
90+
{React.string("You kids get off my lawn!")}
91+
</button>
92+
</div>
93+
}
94+
```
95+
96+
## Getting it running
97+
98+
```sh
99+
npm install
100+
npm start
101+
```

‎bsconfig.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
{
3+
"name": "reason-apollo-hooks",
4+
"reason": {
5+
"react-jsx": 3
6+
},
7+
"sources": {
8+
"dir" : "src",
9+
"subdirs" : true
10+
},
11+
"package-specs": [{
12+
"module": "commonjs",
13+
"in-source": true
14+
}],
15+
"suffix": ".bs.js",
16+
"namespace": true,
17+
"bs-dependencies": [
18+
"reason-react",
19+
"reason-apollo"
20+
],
21+
"refmt": 3
22+
}

‎package.json

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "reason-apollo-hooks",
3+
"version": "0.2.0-beta.2",
4+
"scripts": {
5+
"build": "bsb -make-world",
6+
"start": "bsb -make-world -w",
7+
"clean": "bsb -clean-world",
8+
"test": "echo \"Error: no test specified\" && exit 1",
9+
"webpack": "webpack -w",
10+
"webpack:production": "NODE_ENV=production webpack",
11+
"server": "webpack-dev-server"
12+
},
13+
"keywords": [
14+
"BuckleScript"
15+
],
16+
"author": "",
17+
"license": "MIT",
18+
"dependencies": {
19+
"react": "^16.8.1",
20+
"react-apollo-hooks": "^0.4.5",
21+
"react-dom": "^16.8.1",
22+
"reason-react": ">=0.7.0"
23+
},
24+
"devDependencies": {
25+
"bs-platform": "^5.0.2",
26+
"html-webpack-plugin": "^3.2.0",
27+
"reason-apollo": "^0.15.2",
28+
"webpack": "^4.0.1",
29+
"webpack-cli": "^3.1.1",
30+
"webpack-dev-server": "^3.1.8"
31+
},
32+
"peerDependencies": {
33+
"reason-apollo": "^0.15.2"
34+
}
35+
}

‎src/ApolloProvider.re

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[@bs.module "react-apollo-hooks"]
2+
[@react.component]
3+
external make: (~client: ApolloClient.generatedApolloClient) => React.element = "ApolloProvider";

‎src/Mutation.re

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
module type Config = {
2+
let query: string;
3+
type t;
4+
let parse: Js.Json.t => t;
5+
};
6+
7+
module Make = (Config: Config) => {
8+
type error = {. "message": string};
9+
10+
[@bs.module "react-apollo-hooks"]
11+
external useMutation:
12+
(
13+
string,
14+
{. "variables": Js.Nullable.t(Js.Json.t)},
15+
{. "variables": Js.Nullable.t(Js.Json.t)}
16+
) =>
17+
Js.Promise.t({
18+
.
19+
"data": Js.Nullable.t(Js.Json.t),
20+
"error": Js.Nullable.t(error),
21+
}) =
22+
"useMutation";
23+
24+
type result =
25+
| Data(Config.t)
26+
| Error(error)
27+
| NoData;
28+
29+
let use = (~variables=?, ()) => {
30+
let jsMutate =
31+
useMutation(
32+
Config.query,
33+
{"variables": Js.Nullable.fromOption(variables)},
34+
);
35+
36+
(~variables=?, ()) =>
37+
jsMutate({"variables": Js.Nullable.fromOption(variables)})
38+
|> Js.Promise.then_(jsResult =>
39+
(
40+
switch (
41+
Js.Nullable.toOption(jsResult##data),
42+
Js.Nullable.toOption(jsResult##error),
43+
) {
44+
| (Some(data), _) => Data(Config.parse(data))
45+
| (None, Some(error)) => Error(error)
46+
| (None, None) => NoData
47+
}
48+
)
49+
|> Js.Promise.resolve
50+
);
51+
};
52+
};

‎src/Query.re

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module type Config = {
2+
let query: string;
3+
type t;
4+
let parse: Js.Json.t => t;
5+
};
6+
7+
module Make = (Config: Config) => {
8+
type error = {. "message": string};
9+
10+
[@bs.module "react-apollo-hooks"]
11+
external useQuery:
12+
(string, {. "variables": Js.Nullable.t(Js.Json.t)}) =>
13+
{
14+
.
15+
"data": Js.Nullable.t(Js.Json.t),
16+
"loading": bool,
17+
"error": Js.Nullable.t(error),
18+
} =
19+
"useQuery";
20+
21+
type variant =
22+
| Data(Config.t)
23+
| Error(error)
24+
| Loading
25+
| NoData;
26+
type result = {
27+
data: option(Config.t),
28+
loading: bool,
29+
error: option(error),
30+
};
31+
32+
let use = (~variables=?, ()) => {
33+
let jsResult =
34+
useQuery(
35+
Config.query,
36+
{"variables": Js.Nullable.fromOption(variables)},
37+
);
38+
39+
let result = {
40+
data:
41+
jsResult##data->Js.Nullable.toOption->Belt.Option.map(Config.parse),
42+
loading: jsResult##loading,
43+
error: jsResult##error->Js.Nullable.toOption,
44+
};
45+
46+
(
47+
switch (result) {
48+
| {data: Some(data)} => Data(data)
49+
| {error: Some(error)} => Error(error)
50+
| {loading: true} => Loading
51+
| _ => NoData
52+
},
53+
result,
54+
);
55+
};
56+
};

0 commit comments

Comments
 (0)
This repository has been archived.