From c4e03b29dcdcfab2cd778e9ba05576a6e13d8805 Mon Sep 17 00:00:00 2001 From: Nikolay Nemshilov Date: Wed, 24 Jul 2019 10:54:27 +1000 Subject: [PATCH] Removing the wrapper Promise in useMutation to enable apollo client stubs --- src/useMutation.ts | 61 +++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/src/useMutation.ts b/src/useMutation.ts index a755ee0..0b88afe 100644 --- a/src/useMutation.ts +++ b/src/useMutation.ts @@ -130,37 +130,36 @@ export function useMutation( }; const runMutation = React.useCallback( - (mutateOptions: MutationHookOptions = {}) => { - return new Promise>((resolve, reject) => { - onMutationStart(); - const mutationId = generateNewMutationId(); - - // merge together variables from baseOptions (if specified) - // and the execution - const mutateVariables = options.variables - ? { ...mutateOptions.variables, ...options.variables } - : mutateOptions.variables; - - client - .mutate({ - mutation, - ...options, - ...mutateOptions, - variables: mutateVariables, - }) - .then(response => { - onMutationCompleted(response, mutationId); - resolve(response as ExecutionResult); - }) - .catch(err => { - onMutationError(err, mutationId); - if (rethrow) { - reject(err); - return; - } - resolve(({} as unknown) as ExecutionResult); - }); - }); + ( + mutateOptions: MutationHookOptions = {} + ): Promise> => { + onMutationStart(); + const mutationId = generateNewMutationId(); + + // merge together variables from baseOptions (if specified) + // and the execution + const mutateVariables = options.variables + ? { ...mutateOptions.variables, ...options.variables } + : mutateOptions.variables; + + return client + .mutate({ + mutation, + ...options, + ...mutateOptions, + variables: mutateVariables, + }) + .then(response => { + onMutationCompleted(response, mutationId); + return response as ExecutionResult; + }) + .catch(err => { + onMutationError(err, mutationId); + if (rethrow) { + throw err; + } + return ({} as unknown) as ExecutionResult; + }); }, [client, mutation, objToKey(baseOptions)] );