Include `variables` in mutation `update` function
I would find it useful to include a mutation's variables in the update function parameters. This would make it easier to define most of the behaviors of a mutation in the useMutation; thus avoiding us having to include an update function in the mutation invocation because it needs access to the mutation variables.
Current
const SomeComponent: React.FC = () => {
const [setSomething] = useMutation(mutationName);
return (
<form
onSubmit={async formValues => {
await setSomething({
variables: { something: true },
update: store => {
store.writeFragment({
...
});
}
});
}}
/>
);
};
Desired
const SomeComponent: React.FC = () => {
const [setSomething] = useMutation(mutationName, {
update: (store, mutationResult, mutationOptions) => {
store.writeFragment({
...
});
}
});
return (
<form
onSubmit={async formValues => {
await setSomething({
variables: { something: true }
});
}}
/>
);
};
Also: the Apollo Context!
This would allow to handle cache cases that are currently impossible to cover.
By quickly glancing at the code it seems like it's a one liner at apollo-client/src/core/QueryManager.ts:1512
Really need this in multiple mutations of my app
Note, optimisticResponse already has vars injected.
Current workaround is to query again for vars in the response, so that they are accessible in data, but this is not convenient, and not possible in all cases, particularly for non-CRUD behaviors.
strange that this has not been addressed yet, is it solved in v3?
@slorber you don't need to query again for the variables, you just need to curry the function so that variables are saved, for example:
const [mutationMethod] = useMutation(SOME_MUTATION);
const _mutationMethod: typeof mutationMethod = (...args) => {
const [_options = {}] = args;
args[0] = {
..._options,
update(...args) {
if (options.update) {
options.update(...args);
}
// save your variables here
const { id } = _options.variables || options.variables;
// and update the query you want
updateAnotherQuery({
id,
});
},
};
return mutationMethod(...args);
};
But yeah, as you can see, this can be totally avoided if we just have the variables available in the update function.
+1
+1
+1
+1
+1
+1
This feature request is abandoned, can we open a pull request?