Skip to content

Commit 5cf876f

Browse files
Revert to usinng bs.deriving abstract for partialOperationContext. (#216)
1 parent 33b7a27 commit 5cf876f

File tree

6 files changed

+167
-148
lines changed

6 files changed

+167
-148
lines changed

src/Client.re

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,19 @@ let executeQuery =
251251
(),
252252
);
253253
let parse = request##parse;
254-
let optsJs: Types.partialOperationContext = {
255-
additionalTypenames,
256-
fetchOptions,
257-
fetch,
258-
requestPolicy: requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
259-
url: Some(url->Belt.Option.getWithDefault(client.url)),
260-
pollInterval,
261-
meta,
262-
suspense,
263-
preferGetMethod,
264-
};
254+
let optsJs =
255+
Types.partialOperationContext(
256+
~additionalTypenames?,
257+
~fetchOptions?,
258+
~fetch?,
259+
~requestPolicy=?requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
260+
~url?,
261+
~pollInterval?,
262+
~meta?,
263+
~suspense?,
264+
~preferGetMethod?,
265+
(),
266+
);
265267

266268
executeQueryJs(~client, ~query=req, ~opts=optsJs, ())
267269
|> Wonka.map((. response) => urqlClientResponseToReason(~response, ~parse));
@@ -300,17 +302,19 @@ let executeMutation =
300302
(),
301303
);
302304
let parse = request##parse;
303-
let optsJs: Types.partialOperationContext = {
304-
additionalTypenames,
305-
fetchOptions,
306-
fetch,
307-
requestPolicy: requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
308-
url: Some(url->Belt.Option.getWithDefault(client.url)),
309-
pollInterval,
310-
meta,
311-
suspense,
312-
preferGetMethod,
313-
};
305+
let optsJs =
306+
Types.partialOperationContext(
307+
~additionalTypenames?,
308+
~fetchOptions?,
309+
~fetch?,
310+
~requestPolicy=?requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
311+
~url?,
312+
~pollInterval?,
313+
~meta?,
314+
~suspense?,
315+
~preferGetMethod?,
316+
(),
317+
);
314318

315319
executeMutationJs(~client, ~mutation=req, ~opts=optsJs, ())
316320
|> Wonka.map((. response) => urqlClientResponseToReason(~response, ~parse));
@@ -349,17 +353,19 @@ let executeSubscription =
349353
(),
350354
);
351355
let parse = request##parse;
352-
let optsJs: Types.partialOperationContext = {
353-
additionalTypenames,
354-
fetchOptions,
355-
fetch,
356-
requestPolicy: requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
357-
url: Some(url->Belt.Option.getWithDefault(client.url)),
358-
pollInterval,
359-
meta,
360-
suspense,
361-
preferGetMethod,
362-
};
356+
let optsJs =
357+
Types.partialOperationContext(
358+
~additionalTypenames?,
359+
~fetchOptions?,
360+
~fetch?,
361+
~requestPolicy=?requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
362+
~url?,
363+
~pollInterval?,
364+
~meta?,
365+
~suspense?,
366+
~preferGetMethod?,
367+
(),
368+
);
363369

364370
executeSubscriptionJs(~client, ~subscription=req, ~opts=optsJs, ())
365371
|> Wonka.map((. response) => urqlClientResponseToReason(~response, ~parse));

src/Types.re

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,26 @@ type operationContext = {
4646
preferGetMethod: option(bool),
4747
};
4848

49+
[@bs.deriving {abstract: light}]
4950
type partialOperationContext = {
50-
additionalTypenames: option(array(string)),
51-
fetch: option((string, Fetch.requestInit) => Js.Promise.t(Fetch.response)),
52-
fetchOptions: option(Fetch.requestInit),
53-
requestPolicy: option(string),
54-
url: option(string),
55-
pollInterval: option(int),
56-
meta: option(operationDebugMeta),
57-
suspense: option(bool),
58-
preferGetMethod: option(bool),
51+
[@bs.optional]
52+
additionalTypenames: array(string),
53+
[@bs.optional]
54+
fetch: (string, Fetch.requestInit) => Js.Promise.t(Fetch.response),
55+
[@bs.optional]
56+
fetchOptions: Fetch.requestInit,
57+
[@bs.optional]
58+
requestPolicy: string,
59+
[@bs.optional]
60+
url: string,
61+
[@bs.optional]
62+
pollInterval: int,
63+
[@bs.optional]
64+
meta: operationDebugMeta,
65+
[@bs.optional]
66+
suspense: bool,
67+
[@bs.optional]
68+
preferGetMethod: bool,
5969
};
6070

6171
/* The active GraphQL operation. */

src/Types.rei

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,26 @@ type operationContext = {
3636
preferGetMethod: option(bool),
3737
};
3838

39+
[@bs.deriving {abstract: light}]
3940
type partialOperationContext = {
40-
additionalTypenames: option(array(string)),
41-
fetch: option((string, Fetch.requestInit) => Js.Promise.t(Fetch.response)),
42-
fetchOptions: option(Fetch.requestInit),
43-
requestPolicy: option(string),
44-
url: option(string),
45-
pollInterval: option(int),
46-
meta: option(operationDebugMeta),
47-
suspense: option(bool),
48-
preferGetMethod: option(bool),
41+
[@bs.optional]
42+
additionalTypenames: array(string),
43+
[@bs.optional]
44+
fetch: (string, Fetch.requestInit) => Js.Promise.t(Fetch.response),
45+
[@bs.optional]
46+
fetchOptions: Fetch.requestInit,
47+
[@bs.optional]
48+
requestPolicy: string,
49+
[@bs.optional]
50+
url: string,
51+
[@bs.optional]
52+
pollInterval: int,
53+
[@bs.optional]
54+
meta: operationDebugMeta,
55+
[@bs.optional]
56+
suspense: bool,
57+
[@bs.optional]
58+
preferGetMethod: bool,
4959
};
5060

5161
/* The active GraphQL operation. */

src/hooks/UseMutation.re

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ let useMutation = (~request) => {
4343
let variables = request##variables;
4444
let parse = request##parse;
4545

46-
let client = UseClient.useClient();
47-
4846
let (stateJs, executeMutationJs) = useMutationJs(query);
4947

5048
let state =
@@ -54,7 +52,7 @@ let useMutation = (~request) => {
5452
);
5553

5654
let executeMutation =
57-
React.useMemo3(
55+
React.useMemo2(
5856
(
5957
(),
6058
~additionalTypenames=?,
@@ -68,22 +66,24 @@ let useMutation = (~request) => {
6866
~preferGetMethod=?,
6967
(),
7068
) => {
71-
let ctx: Types.partialOperationContext = {
72-
additionalTypenames,
73-
fetchOptions,
74-
fetch,
75-
requestPolicy:
76-
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
77-
url: Some(url->Belt.Option.getWithDefault(client.url)),
78-
pollInterval,
79-
meta,
80-
suspense,
81-
preferGetMethod,
82-
};
69+
let ctx =
70+
Types.partialOperationContext(
71+
~additionalTypenames?,
72+
~fetchOptions?,
73+
~fetch?,
74+
~url?,
75+
~requestPolicy=?
76+
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
77+
~pollInterval?,
78+
~meta?,
79+
~suspense?,
80+
~preferGetMethod?,
81+
(),
82+
);
8383

8484
executeMutationJs(variables, ctx);
8585
},
86-
(executeMutationJs, variables, client),
86+
(executeMutationJs, variables),
8787
);
8888

8989
(state, executeMutation);
@@ -102,8 +102,6 @@ let useDynamicMutation = definition => {
102102
let (parse, query, composeVariables) = definition;
103103
let (stateJs, executeMutationJs) = useMutationJs(query);
104104

105-
let client = UseClient.useClient();
106-
107105
let state =
108106
React.useMemo2(
109107
() => Types.urqlResponseToReason(~response=stateJs, ~parse),
@@ -122,17 +120,20 @@ let useDynamicMutation = definition => {
122120
~suspense=?,
123121
~preferGetMethod=?,
124122
) => {
125-
let ctx: Types.partialOperationContext = {
126-
additionalTypenames,
127-
fetchOptions,
128-
fetch,
129-
requestPolicy: requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
130-
url: Some(url->Belt.Option.getWithDefault(client.url)),
131-
pollInterval,
132-
meta,
133-
suspense,
134-
preferGetMethod,
135-
};
123+
let ctx =
124+
Types.partialOperationContext(
125+
~additionalTypenames?,
126+
~fetchOptions?,
127+
~fetch?,
128+
~url?,
129+
~requestPolicy=?
130+
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
131+
~pollInterval?,
132+
~meta?,
133+
~suspense?,
134+
~preferGetMethod?,
135+
(),
136+
);
136137

137138
composeVariables(variables => executeMutationJs(variables, ctx));
138139
};

src/hooks/UseQuery.re

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ external useQueryJs: useQueryArgsJs => useQueryResponseJs = "useQuery";
3636

3737
// reason-react does not provide a binding of sufficient arity for our memoization needs
3838
[@bs.module "react"]
39-
external useMemo10:
40-
([@bs.uncurry] (unit => 'any), ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j)) =>
41-
'any =
39+
external useMemo9:
40+
([@bs.uncurry] (unit => 'any), ('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i)) => 'any =
4241
"useMemo";
4342

4443
let useQuery =
@@ -65,31 +64,26 @@ let useQuery =
6564
[|requestPolicy|],
6665
);
6766

68-
let client = UseClient.useClient();
69-
7067
let context =
71-
useMemo10(
72-
() => {
73-
let c: Types.partialOperationContext = {
74-
additionalTypenames,
75-
fetchOptions,
76-
fetch,
77-
url: Some(url->Belt.Option.getWithDefault(client.url)),
78-
requestPolicy: rp,
79-
pollInterval,
80-
meta,
81-
suspense,
82-
preferGetMethod,
83-
};
84-
85-
c;
86-
},
68+
useMemo9(
69+
() =>
70+
Types.partialOperationContext(
71+
~additionalTypenames?,
72+
~fetchOptions?,
73+
~fetch?,
74+
~url?,
75+
~requestPolicy=?rp,
76+
~pollInterval?,
77+
~meta?,
78+
~suspense?,
79+
~preferGetMethod?,
80+
(),
81+
),
8782
(
8883
additionalTypenames,
8984
fetchOptions,
9085
fetch,
9186
url,
92-
client,
9387
rp,
9488
pollInterval,
9589
meta,
@@ -128,18 +122,20 @@ let useQuery =
128122
~preferGetMethod=?,
129123
(),
130124
) => {
131-
let ctx: Types.partialOperationContext = {
132-
additionalTypenames,
133-
fetchOptions,
134-
fetch,
135-
url,
136-
requestPolicy:
137-
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
138-
pollInterval,
139-
meta,
140-
suspense,
141-
preferGetMethod,
142-
};
125+
let ctx =
126+
Types.partialOperationContext(
127+
~additionalTypenames?,
128+
~fetchOptions?,
129+
~fetch?,
130+
~url?,
131+
~requestPolicy=?
132+
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
133+
~pollInterval?,
134+
~meta?,
135+
~suspense?,
136+
~preferGetMethod?,
137+
(),
138+
);
143139

144140
executeQueryJs(ctx);
145141
},

0 commit comments

Comments
 (0)