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

Commit f23745f

Browse files
committed
feat(mutation): update for second argument of apollo hooks
1 parent fd9c7bb commit f23745f

File tree

1 file changed

+78
-28
lines changed

1 file changed

+78
-28
lines changed

src/Mutation.re

+78-28
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,35 @@ module type Config = {
44
let parse: Js.Json.t => t;
55
};
66

7-
type error = {. "message": string};
7+
type graphqlErrors;
88

9+
type error = {
10+
.
11+
"message": string,
12+
"graphlErrors": graphqlErrors,
13+
};
14+
15+
/* Result that is return by the hook */
916
type result('a) =
1017
| Data('a)
1118
| Error(error)
1219
| NoData;
1320

21+
/* Result that is return by the hook */
22+
type controlledResult('a) = {
23+
loading: bool,
24+
called: bool,
25+
data: option('a),
26+
error: option(error),
27+
};
28+
29+
type controledVariantResult('a) =
30+
| Loading
31+
| Called
32+
| Data('a)
33+
| Error(error)
34+
| NoData;
35+
1436
module Make = (Config: Config) => {
1537
[@bs.module] external gql: ReasonApolloTypes.gql = "graphql-tag";
1638

@@ -22,37 +44,65 @@ module Make = (Config: Config) => {
2244
client: ApolloClient.generatedApolloClient,
2345
};
2446

47+
type jsResult = {
48+
.
49+
"data": Js.Nullable.t(Js.Json.t),
50+
"loading": bool,
51+
"called": bool,
52+
"error": Js.Nullable.t(error),
53+
};
54+
55+
type jsMutate = (. options) => Js.Promise.t(jsResult);
56+
2557
[@bs.module "@apollo/react-hooks"]
2658
external useMutation:
27-
(. ReasonApolloTypes.queryString, (options)) =>
28-
(. options) =>
29-
Js.Promise.t({
30-
.
31-
"data": Js.Nullable.t(Js.Json.t),
32-
"error": Js.Nullable.t(error),
33-
}) =
59+
(. ReasonApolloTypes.queryString, options) => (jsMutate, jsResult) =
3460
"useMutation";
3561

3662
let use = (~variables=?, ~client=?, ()) => {
37-
let jsMutate =
38-
useMutation(. gql(. Config.query), (options(~variables?, ~client?, ())));
39-
40-
let mutate = (~variables=?, ~client=?, ()) =>
41-
jsMutate(. options(~variables?, ~client?, ()))
42-
|> Js.Promise.then_(jsResult =>
43-
(
44-
switch (
45-
Js.Nullable.toOption(jsResult##data),
46-
Js.Nullable.toOption(jsResult##error),
47-
) {
48-
| (Some(data), _) => Data(Config.parse(data))
49-
| (None, Some(error)) => Error(error)
50-
| (None, None) => NoData
51-
}
52-
)
53-
|> Js.Promise.resolve
54-
);
55-
56-
mutate
63+
let (jsMutate, jsResult) =
64+
useMutation(.
65+
gql(. Config.query),
66+
options(~variables?, ~client?, ()),
67+
);
68+
69+
let mutate =
70+
React.useMemo1(
71+
((), ~variables=?, ~client=?, ()) =>
72+
jsMutate(. options(~variables?, ~client?, ()))
73+
|> Js.Promise.then_(jsResult =>
74+
(
75+
switch (
76+
Js.Nullable.toOption(jsResult##data),
77+
Js.Nullable.toOption(jsResult##error),
78+
) {
79+
| (Some(data), _) => Data(Config.parse(data))
80+
| (None, Some(error)) => Error(error)
81+
| (None, None) => NoData
82+
}
83+
)
84+
|> Js.Promise.resolve
85+
),
86+
[|variables|],
87+
);
88+
89+
let full = {
90+
loading: jsResult##loading,
91+
called: jsResult##called,
92+
data:
93+
jsResult##data->Js.Nullable.toOption->Belt.Option.map(Config.parse),
94+
error: jsResult##error->Js.Nullable.toOption,
95+
};
96+
97+
let simple =
98+
switch (full) {
99+
| {loading: true} => Loading
100+
| {error: Some(error)} => Error(error)
101+
| {data: Some(data)} => Data(data)
102+
| {called: true} => Called
103+
| _ => NoData
104+
};
105+
106+
(mutate, simple, full);
57107
};
58108
};

0 commit comments

Comments
 (0)