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 1 commit
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
parkerziegler marked this conversation as resolved.
Show resolved Hide resolved
let (_, executeLikeMutation) =
Hooks.useMutation(~request=Mutations.LikeDog.make(~key=id, ()));

// Example of using hooks without 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(
varsObj => executeMutationJs(Some(varsObj##variables)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A nit on naming here. This is the Js.t created by calling .make() on the graphql_ppx_re module, yeah? Could we use the same nomenclature we do in useMutation and name this request?

Suggested change
varsObj => executeMutationJs(Some(varsObj##variables)),
request => executeMutationJs(Some(request##variables)),

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The type is slightly different (explained below), which is why I went with a different name. Doesn't bother me to change it though, if you think it's clearer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that makes sense. I really wish we could destructure "open" objects like this so it'd be more like:

{ variables } => executeMutationJs(Some(variables))

Still I do find request a little easier to understand. This object passed to this callback is the result of calling .make() on that graphql_ppx_re module, and we have been calling that request throughout the codebase. Even tho we're restricting its type a bit more, it's more or less the same thing.

[|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),
);