From 6af440d4ab9d8a8788986f63fb9fa5f2231872c1 Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Fri, 1 May 2020 13:36:28 -0700 Subject: [PATCH 1/3] Pass context from mutation call to onCompleted/onError handlers Is it possible to modify the hook implementation to allow access to context variables during mutation? Is there a way I can do this without modifying the hook implementation? --- src/useMutation.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/useMutation.ts b/src/useMutation.ts index a755ee0..c9c5925 100644 --- a/src/useMutation.ts +++ b/src/useMutation.ts @@ -130,7 +130,7 @@ export function useMutation( }; const runMutation = React.useCallback( - (mutateOptions: MutationHookOptions = {}) => { + (mutateOptions: MutationHookOptions = {}, context: TData | undefined) => { return new Promise>((resolve, reject) => { onMutationStart(); const mutationId = generateNewMutationId(); @@ -149,11 +149,11 @@ export function useMutation( variables: mutateVariables, }) .then(response => { - onMutationCompleted(response, mutationId); + onMutationCompleted(response, mutationId, context); resolve(response as ExecutionResult); }) .catch(err => { - onMutationError(err, mutationId); + onMutationError(err, mutationId, context); if (rethrow) { reject(err); return; From 3e90c7843d216654b7b131d06300a96f5d9ea2f4 Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Fri, 1 May 2020 14:01:40 -0700 Subject: [PATCH 2/3] Fix linting --- src/useMutation.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/useMutation.ts b/src/useMutation.ts index c9c5925..772afb8 100644 --- a/src/useMutation.ts +++ b/src/useMutation.ts @@ -130,7 +130,10 @@ export function useMutation( }; const runMutation = React.useCallback( - (mutateOptions: MutationHookOptions = {}, context: TData | undefined) => { + ( + mutateOptions: MutationHookOptions = {}, + context: TData | undefined + ) => { return new Promise>((resolve, reject) => { onMutationStart(); const mutationId = generateNewMutationId(); From 9b9814d233ebce1fbd258ce29e5e61ea6b96a5ec Mon Sep 17 00:00:00 2001 From: Kevin Lee Date: Fri, 1 May 2020 14:19:49 -0700 Subject: [PATCH 3/3] Fix linting --- src/useMutation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/useMutation.ts b/src/useMutation.ts index 772afb8..adade8b 100644 --- a/src/useMutation.ts +++ b/src/useMutation.ts @@ -131,7 +131,7 @@ export function useMutation( const runMutation = React.useCallback( ( - mutateOptions: MutationHookOptions = {}, + mutateOptions: MutationHookOptions = {}, context: TData | undefined ) => { return new Promise>((resolve, reject) => {