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

Commit

Permalink
feat(mutation): update for second argument of apollo hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
fakenickels committed Jun 26, 2019
1 parent fd9c7bb commit f23745f
Showing 1 changed file with 78 additions and 28 deletions.
106 changes: 78 additions & 28 deletions src/Mutation.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,35 @@ module type Config = {
let parse: Js.Json.t => t;
};

type error = {. "message": string};
type graphqlErrors;

type error = {
.
"message": string,
"graphlErrors": graphqlErrors,
};

/* Result that is return by the hook */
type result('a) =
| Data('a)
| Error(error)
| NoData;

/* Result that is return by the hook */
type controlledResult('a) = {
loading: bool,
called: bool,
data: option('a),
error: option(error),
};

type controledVariantResult('a) =
| Loading
| Called
| Data('a)
| Error(error)
| NoData;

module Make = (Config: Config) => {
[@bs.module] external gql: ReasonApolloTypes.gql = "graphql-tag";

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

type jsResult = {
.
"data": Js.Nullable.t(Js.Json.t),
"loading": bool,
"called": bool,
"error": Js.Nullable.t(error),
};

type jsMutate = (. options) => Js.Promise.t(jsResult);

[@bs.module "@apollo/react-hooks"]
external useMutation:
(. ReasonApolloTypes.queryString, (options)) =>
(. options) =>
Js.Promise.t({
.
"data": Js.Nullable.t(Js.Json.t),
"error": Js.Nullable.t(error),
}) =
(. ReasonApolloTypes.queryString, options) => (jsMutate, jsResult) =
"useMutation";

let use = (~variables=?, ~client=?, ()) => {
let jsMutate =
useMutation(. gql(. Config.query), (options(~variables?, ~client?, ())));

let mutate = (~variables=?, ~client=?, ()) =>
jsMutate(. options(~variables?, ~client?, ()))
|> Js.Promise.then_(jsResult =>
(
switch (
Js.Nullable.toOption(jsResult##data),
Js.Nullable.toOption(jsResult##error),
) {
| (Some(data), _) => Data(Config.parse(data))
| (None, Some(error)) => Error(error)
| (None, None) => NoData
}
)
|> Js.Promise.resolve
);

mutate
let (jsMutate, jsResult) =
useMutation(.
gql(. Config.query),
options(~variables?, ~client?, ()),
);

let mutate =
React.useMemo1(
((), ~variables=?, ~client=?, ()) =>
jsMutate(. options(~variables?, ~client?, ()))
|> Js.Promise.then_(jsResult =>
(
switch (
Js.Nullable.toOption(jsResult##data),
Js.Nullable.toOption(jsResult##error),
) {
| (Some(data), _) => Data(Config.parse(data))
| (None, Some(error)) => Error(error)
| (None, None) => NoData
}
)
|> Js.Promise.resolve
),
[|variables|],
);

let full = {
loading: jsResult##loading,
called: jsResult##called,
data:
jsResult##data->Js.Nullable.toOption->Belt.Option.map(Config.parse),
error: jsResult##error->Js.Nullable.toOption,
};

let simple =
switch (full) {
| {loading: true} => Loading
| {error: Some(error)} => Error(error)
| {data: Some(data)} => Data(data)
| {called: true} => Called
| _ => NoData
};

(mutate, simple, full);
};
};

0 comments on commit f23745f

Please sign in to comment.