Skip to content

Commit

Permalink
Add useDynamicMutation hook (#130)
Browse files Browse the repository at this point in the history
* Add useDynamicMutation hook

* Incorporate PR feedback
  • Loading branch information
Schmavery authored and parkerziegler committed Nov 22, 2019
1 parent fc01954 commit d77f294
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
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} =>
Js.Promise.t(UrqlClient.ClientTypes.operationResult),
);

0 comments on commit d77f294

Please sign in to comment.