-
Notifications
You must be signed in to change notification settings - Fork 28
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
Passing variables only when calling executeMutation #128
Comments
Alright, so diving back into this, I feel like the two implementations I'm seeing discussed fall into two categories.
let (result, executeMutation) = Hooks.useMutation(~request=MyQuery.makeWithVariables, ()); In this case
module type QuerySig {
type t;
let parse: Js.Json.t => t;
let query: string;
};
let useMutation:
(~request: (module QuerySig with type t = 'response)) =>
(
hookResponse('response),
Js.Json.t => Js.Promise.t(UrqlClient.ClientTypes.operationResult)
) With this approach I think we get both the type inference and the ability to parse the response for the user (the best of both worlds), but we do lose the consistency of the APIs with cc/ @robinweser @Schmavery @gugahoa any thoughts here? |
FWIW here's a proof of concept for the functor approach (the second one above) that appears to be working well in one of our examples: https://github.com/FormidableLabs/reason-urql/compare/task/functor-useMutation-api |
As much as I dislike the fact that it's going to break API consistency, I feel like the second approach is the better one. The first one, while elegant, feels a bit inconvenient to use... I have a use case I have to write my own parse instead of leveraging |
Still thinking about it a little, but I can't understand the problem @parkerziegler describes here:
wouldn't the type of useMutation be something like let useMutation:
~request=(('variables => UrqlTypes.request('response))
=> ('response, 'variables => unit)) It seems like adding an additional useDynamicMutation or something API could help the API consistency problem while allowing people to do this too.. just an idea. @robinweser Re: custom parse function -- FWIW I included an example for this in the original change in case it's helpful, didn't want to have a hard requirement on the ppx https://github.com/FormidableLabs/reason-urql/blob/fc01954e9c8e8353f6e6248932f0fc5b995fe115/examples/3-mutation/src/Dog.re#L68-L75 |
@Schmavery the challenge I was seeing is that, in order to get access to the type of the query / mutation / subscription, I think you have to call What @robinweser seems to be running into is that the variables he wants to pass into module MyMutation = [%graphql {|
mutation doSomething($variable: String!) {
doSomething(variable: $variable) {
id
name
field
}
}
|}];
[@react.component]
let make = () => {
let (result, executeMutation) = Hooks.useMutation(~request=MyMutation.make(~variable=/* What happens if variable isn't defined yet? */, ()));
} has to apply an empty or fallback variable of some kind until I also have an additional functor API worked out for all three hooks locally, so I'll push up that diff tomorrow so we can look at it and discuss. I like the idea of a |
^ Here's the PR with the proposed functor API. @Schmavery if you're able to cook up a branch or PR for |
@parkerziegler we should be able to do an API like this if people want, I have some code that seems to be typechecking locally mocked up for it module MyMutation = [%graphql {|
mutation doSomething($variable: String!) {
doSomething(variable: $variable) {
id
name
field
}
}
|}];
[@react.component]
let make = () => {
let (result, executeMutation) = Hooks.useDynamicMutation(~query=MyMutation.query, ~parse=MyMutation.parse));
// later...
executeMutation(MyMutation.make(~var1="hello", ~var2="world", ()))
} It's a little more verbose but allows everything to be typed without using functors (I noticed in your PR that it seems like the vars are an untyped Js.Json.t? Unless I misread) It's kinda funny the limitations we're under because of needing to bind to the js API cause I can't imagine either the query or the parse function needing to be used before the vars are received... it feels like it would almost be easier to reimplement the hook on the reason side. Another thing I was considering involved a PR to graphql_ppx_re to add a record on the MyMutation module containing the query, parse fn, and a fn of type let (result, executeMutation) = Hooks.useDynamicMutation(MyMutation.definition);
executeMutation({"var1": "hello", "var2": "world"}) Cutest API but maybe too much work to get upstreamed haha |
@Schmavery Oh I really like that proposed Also on the custom parse stuff: Yeah it's quite straight-forward to just use |
@Schmavery Ok, I think I'm seeing it now! I think for some reason I didn't think you could access |
Closed by #130 and will be released in v1.4.0! |
In the PR #69 by @Schmavery where typed use mutation was introduced, there was also a suggestion on making the interface for
useMutation
takemakeWithVariables
as parameter to let users pass variables later. In hindsight, it seems this is the expected behavior by users as can be seen here: https://spectrum.chat/urql/reason/usemutation-api~72b6306a-8d92-4d17-9fff-15826dd64070 and here #103What are the thoughts on creating another
useMutation
/useQuery
function where it's possible to pass the variables when callingexecuteMutation
/executeQuery
?cc @parkerziegler
The text was updated successfully, but these errors were encountered: