@@ -4,13 +4,35 @@ module type Config = {
4
4
let parse : Js . Json . t => t ;
5
5
};
6
6
7
- type error = {. "message" : string } ;
7
+ type graphqlErrors ;
8
8
9
+ type error = {
10
+ .
11
+ "message": string ,
12
+ "graphlErrors": graphqlErrors ,
13
+ };
14
+
15
+ /* Result that is return by the hook */
9
16
type result (' a ) =
10
17
| Data ('a)
11
18
| Error (error)
12
19
| NoData ;
13
20
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
+
14
36
module Make = (Config : Config ) => {
15
37
[@ bs . module ] external gql : ReasonApolloTypes . gql = "graphql-tag" ;
16
38
@@ -22,37 +44,65 @@ module Make = (Config: Config) => {
22
44
client: ApolloClient . generatedApolloClient ,
23
45
};
24
46
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
+
25
57
[@ bs . module "@apollo/react-hooks" ]
26
58
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 ) =
34
60
"useMutation" ;
35
61
36
62
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);
57
107
};
58
108
};
0 commit comments