apollo-feature-requests icon indicating copy to clipboard operation
apollo-feature-requests copied to clipboard

Include `variables` in mutation `update` function

Open justinanastos opened this issue 6 years ago • 10 comments

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 }
        });
      }}
    />
  );
};

justinanastos avatar Nov 15 '19 16:11 justinanastos

Also: the Apollo Context!

fbartho avatar Nov 15 '19 17:11 fbartho

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

riccardoferretti avatar Feb 13 '20 18:02 riccardoferretti

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.

slorber avatar Mar 27 '20 18:03 slorber

strange that this has not been addressed yet, is it solved in v3?

Ericnr avatar Apr 03 '20 00:04 Ericnr

@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.

xinghul avatar Nov 04 '20 20:11 xinghul

+1

julioxavierr avatar Sep 13 '21 17:09 julioxavierr

+1

tien avatar Nov 01 '21 23:11 tien

+1

AndrewVolosevich avatar Jan 25 '22 13:01 AndrewVolosevich

+1

mo-ms avatar Feb 18 '22 17:02 mo-ms

+1

ulrokx avatar Oct 04 '22 22:10 ulrokx

+1

eLysgaard avatar Feb 02 '23 13:02 eLysgaard

This feature request is abandoned, can we open a pull request?

Taytay884 avatar Sep 05 '23 09:09 Taytay884