Skip to content

Commit

Permalink
Revert to usinng bs.deriving abstract for partialOperationContext. (#216
Browse files Browse the repository at this point in the history
)
  • Loading branch information
parkerziegler authored Sep 10, 2020
1 parent 33b7a27 commit 5cf876f
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 148 deletions.
72 changes: 39 additions & 33 deletions src/Client.re
Original file line number Diff line number Diff line change
Expand Up @@ -251,17 +251,19 @@ let executeQuery =
(),
);
let parse = request##parse;
let optsJs: Types.partialOperationContext = {
additionalTypenames,
fetchOptions,
fetch,
requestPolicy: requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
url: Some(url->Belt.Option.getWithDefault(client.url)),
pollInterval,
meta,
suspense,
preferGetMethod,
};
let optsJs =
Types.partialOperationContext(
~additionalTypenames?,
~fetchOptions?,
~fetch?,
~requestPolicy=?requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
~url?,
~pollInterval?,
~meta?,
~suspense?,
~preferGetMethod?,
(),
);

executeQueryJs(~client, ~query=req, ~opts=optsJs, ())
|> Wonka.map((. response) => urqlClientResponseToReason(~response, ~parse));
Expand Down Expand Up @@ -300,17 +302,19 @@ let executeMutation =
(),
);
let parse = request##parse;
let optsJs: Types.partialOperationContext = {
additionalTypenames,
fetchOptions,
fetch,
requestPolicy: requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
url: Some(url->Belt.Option.getWithDefault(client.url)),
pollInterval,
meta,
suspense,
preferGetMethod,
};
let optsJs =
Types.partialOperationContext(
~additionalTypenames?,
~fetchOptions?,
~fetch?,
~requestPolicy=?requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
~url?,
~pollInterval?,
~meta?,
~suspense?,
~preferGetMethod?,
(),
);

executeMutationJs(~client, ~mutation=req, ~opts=optsJs, ())
|> Wonka.map((. response) => urqlClientResponseToReason(~response, ~parse));
Expand Down Expand Up @@ -349,17 +353,19 @@ let executeSubscription =
(),
);
let parse = request##parse;
let optsJs: Types.partialOperationContext = {
additionalTypenames,
fetchOptions,
fetch,
requestPolicy: requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
url: Some(url->Belt.Option.getWithDefault(client.url)),
pollInterval,
meta,
suspense,
preferGetMethod,
};
let optsJs =
Types.partialOperationContext(
~additionalTypenames?,
~fetchOptions?,
~fetch?,
~requestPolicy=?requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
~url?,
~pollInterval?,
~meta?,
~suspense?,
~preferGetMethod?,
(),
);

executeSubscriptionJs(~client, ~subscription=req, ~opts=optsJs, ())
|> Wonka.map((. response) => urqlClientResponseToReason(~response, ~parse));
Expand Down
28 changes: 19 additions & 9 deletions src/Types.re
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,26 @@ type operationContext = {
preferGetMethod: option(bool),
};

[@bs.deriving {abstract: light}]
type partialOperationContext = {
additionalTypenames: option(array(string)),
fetch: option((string, Fetch.requestInit) => Js.Promise.t(Fetch.response)),
fetchOptions: option(Fetch.requestInit),
requestPolicy: option(string),
url: option(string),
pollInterval: option(int),
meta: option(operationDebugMeta),
suspense: option(bool),
preferGetMethod: option(bool),
[@bs.optional]
additionalTypenames: array(string),
[@bs.optional]
fetch: (string, Fetch.requestInit) => Js.Promise.t(Fetch.response),
[@bs.optional]
fetchOptions: Fetch.requestInit,
[@bs.optional]
requestPolicy: string,
[@bs.optional]
url: string,
[@bs.optional]
pollInterval: int,
[@bs.optional]
meta: operationDebugMeta,
[@bs.optional]
suspense: bool,
[@bs.optional]
preferGetMethod: bool,
};

/* The active GraphQL operation. */
Expand Down
28 changes: 19 additions & 9 deletions src/Types.rei
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,26 @@ type operationContext = {
preferGetMethod: option(bool),
};

[@bs.deriving {abstract: light}]
type partialOperationContext = {
additionalTypenames: option(array(string)),
fetch: option((string, Fetch.requestInit) => Js.Promise.t(Fetch.response)),
fetchOptions: option(Fetch.requestInit),
requestPolicy: option(string),
url: option(string),
pollInterval: option(int),
meta: option(operationDebugMeta),
suspense: option(bool),
preferGetMethod: option(bool),
[@bs.optional]
additionalTypenames: array(string),
[@bs.optional]
fetch: (string, Fetch.requestInit) => Js.Promise.t(Fetch.response),
[@bs.optional]
fetchOptions: Fetch.requestInit,
[@bs.optional]
requestPolicy: string,
[@bs.optional]
url: string,
[@bs.optional]
pollInterval: int,
[@bs.optional]
meta: operationDebugMeta,
[@bs.optional]
suspense: bool,
[@bs.optional]
preferGetMethod: bool,
};

/* The active GraphQL operation. */
Expand Down
59 changes: 30 additions & 29 deletions src/hooks/UseMutation.re
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ let useMutation = (~request) => {
let variables = request##variables;
let parse = request##parse;

let client = UseClient.useClient();

let (stateJs, executeMutationJs) = useMutationJs(query);

let state =
Expand All @@ -54,7 +52,7 @@ let useMutation = (~request) => {
);

let executeMutation =
React.useMemo3(
React.useMemo2(
(
(),
~additionalTypenames=?,
Expand All @@ -68,22 +66,24 @@ let useMutation = (~request) => {
~preferGetMethod=?,
(),
) => {
let ctx: Types.partialOperationContext = {
additionalTypenames,
fetchOptions,
fetch,
requestPolicy:
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
url: Some(url->Belt.Option.getWithDefault(client.url)),
pollInterval,
meta,
suspense,
preferGetMethod,
};
let ctx =
Types.partialOperationContext(
~additionalTypenames?,
~fetchOptions?,
~fetch?,
~url?,
~requestPolicy=?
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
~pollInterval?,
~meta?,
~suspense?,
~preferGetMethod?,
(),
);

executeMutationJs(variables, ctx);
},
(executeMutationJs, variables, client),
(executeMutationJs, variables),
);

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

let client = UseClient.useClient();

let state =
React.useMemo2(
() => Types.urqlResponseToReason(~response=stateJs, ~parse),
Expand All @@ -122,17 +120,20 @@ let useDynamicMutation = definition => {
~suspense=?,
~preferGetMethod=?,
) => {
let ctx: Types.partialOperationContext = {
additionalTypenames,
fetchOptions,
fetch,
requestPolicy: requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
url: Some(url->Belt.Option.getWithDefault(client.url)),
pollInterval,
meta,
suspense,
preferGetMethod,
};
let ctx =
Types.partialOperationContext(
~additionalTypenames?,
~fetchOptions?,
~fetch?,
~url?,
~requestPolicy=?
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
~pollInterval?,
~meta?,
~suspense?,
~preferGetMethod?,
(),
);

composeVariables(variables => executeMutationJs(variables, ctx));
};
Expand Down
64 changes: 30 additions & 34 deletions src/hooks/UseQuery.re
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ external useQueryJs: useQueryArgsJs => useQueryResponseJs = "useQuery";

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

let useQuery =
Expand All @@ -65,31 +64,26 @@ let useQuery =
[|requestPolicy|],
);

let client = UseClient.useClient();

let context =
useMemo10(
() => {
let c: Types.partialOperationContext = {
additionalTypenames,
fetchOptions,
fetch,
url: Some(url->Belt.Option.getWithDefault(client.url)),
requestPolicy: rp,
pollInterval,
meta,
suspense,
preferGetMethod,
};

c;
},
useMemo9(
() =>
Types.partialOperationContext(
~additionalTypenames?,
~fetchOptions?,
~fetch?,
~url?,
~requestPolicy=?rp,
~pollInterval?,
~meta?,
~suspense?,
~preferGetMethod?,
(),
),
(
additionalTypenames,
fetchOptions,
fetch,
url,
client,
rp,
pollInterval,
meta,
Expand Down Expand Up @@ -128,18 +122,20 @@ let useQuery =
~preferGetMethod=?,
(),
) => {
let ctx: Types.partialOperationContext = {
additionalTypenames,
fetchOptions,
fetch,
url,
requestPolicy:
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
pollInterval,
meta,
suspense,
preferGetMethod,
};
let ctx =
Types.partialOperationContext(
~additionalTypenames?,
~fetchOptions?,
~fetch?,
~url?,
~requestPolicy=?
requestPolicy->Belt.Option.map(Types.requestPolicyToJs),
~pollInterval?,
~meta?,
~suspense?,
~preferGetMethod?,
(),
);

executeQueryJs(ctx);
},
Expand Down
Loading

0 comments on commit 5cf876f

Please sign in to comment.