Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add useDynamicMutation hook #130

Merged
merged 2 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions examples/3-mutation/src/Dog.re
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ let make =
[|id|],
);

// Example of using hooks with graphql_ppx_re (or graphql_ppx)
let (_, executeLikeMutation) =
Hooks.useMutation(~request=Mutations.LikeDog.make(~key=id, ()));

// Example of using hooks without graphql_ppx_re (or graphql_ppx)
let (_, executeTreatMutation) =
Hooks.useMutation(
~request={
Expand All @@ -74,6 +76,14 @@ let make =
},
);

// Example of using hooks where the variables are only known when the
// mutation runs
let (_, executePatMutation) =
Hooks.useDynamicMutation(
~query=Mutations.PatDog.query,
~parse=Mutations.PatDog.parse,
);

<div className=DogStyles.container>
<img src=imageUrl alt=name className=DogStyles.image />
<h3 className=DogStyles.title> {j|$name|j}->React.string </h3>
Expand All @@ -84,22 +94,21 @@ let make =
hex="48a9dc"
onClick={_ => executeLikeMutation() |> ignore}
/>
<Mutation request={Mutations.PatDog.make(~key=id, ())}>
...{({executeMutation}) =>
<EmojiButton
emoji={j|✋|j}
count={string_of_int(pats)}
hex="db4d3f"
onClick={_ => executeMutation() |> ignore}
/>
<EmojiButton
emoji={j|✋|j}
count={string_of_int(pats)}
hex="db4d3f"
onClick={_ =>
executePatMutation(Mutations.PatDog.make(~key=id, ())) |> ignore
}
</Mutation>
/>
<EmojiButton
emoji={j|🍖|j}
count={string_of_int(treats)}
hex="7b16ff"
onClick={_ => executeTreatMutation() |> ignore}
/>
// Example of using the Mutation component
<Mutation request={Mutations.BellyscratchDog.make(~key=id, ())}>
...{({executeMutation}) =>
<EmojiButton
Expand Down
18 changes: 18 additions & 0 deletions src/hooks/UrqlUseMutation.re
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,21 @@ let useMutation = (~request) => {

(response, executeMutation);
};

let useDynamicMutation = (~query, ~parse) => {
let (responseJs, executeMutationJs) = useMutationJs(query);

let response =
React.useMemo1(
() => responseJs |> urqlResponseToReason(parse),
[|responseJs|],
);

let executeMutation =
React.useCallback1(
request => executeMutationJs(Some(request##variables)),
[|executeMutationJs|],
);

(response, executeMutation);
};
8 changes: 8 additions & 0 deletions src/hooks/UrqlUseMutation.rei
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ let useMutation:
UrqlTypes.hookResponse('response),
unit => Js.Promise.t(UrqlClient.ClientTypes.operationResult),
);

let useDynamicMutation:
(~query: string, ~parse: Js.Json.t => 'response) =>
(
UrqlTypes.hookResponse('response),
{.. "variables": Js.Json.t} =>
parkerziegler marked this conversation as resolved.
Show resolved Hide resolved
Js.Promise.t(UrqlClient.ClientTypes.operationResult),
);