From 7241e28e3021fc1172e93c43fbc15402d6fb6065 Mon Sep 17 00:00:00 2001 From: Kevin Cooper Date: Tue, 24 Apr 2018 18:22:04 -0400 Subject: [PATCH 1/3] Include ApolloClient in MutationResult --- src/Mutation.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Mutation.tsx b/src/Mutation.tsx index 5a3acfa4ba..df452a78b8 100644 --- a/src/Mutation.tsx +++ b/src/Mutation.tsx @@ -14,6 +14,7 @@ export interface MutationResult> { error?: ApolloError; loading: boolean; called: boolean; + client: ApolloClient; } export interface MutationContext { client: ApolloClient; @@ -157,6 +158,7 @@ class Mutation extends React.Compo loading, data, error, + client: this.client, }; return children(this.runMutation, result); From a809f75e66fa22136a24e4c9ccca4771784eefe8 Mon Sep 17 00:00:00 2001 From: Hugh Willson Date: Mon, 18 Jun 2018 18:19:32 -0400 Subject: [PATCH 2/3] Adjust tests to handle newly returned MutationResult client --- test/client/Mutation.test.tsx | 146 ++++++++++++---------------------- 1 file changed, 52 insertions(+), 94 deletions(-) diff --git a/test/client/Mutation.test.tsx b/test/client/Mutation.test.tsx index 07fd023a29..5bfc3b929b 100644 --- a/test/client/Mutation.test.tsx +++ b/test/client/Mutation.test.tsx @@ -72,24 +72,18 @@ it('performs a mutation', done => { {(createTodo, result) => { if (count === 0) { - expect(result).toEqual({ - loading: false, - called: false, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(false); setTimeout(() => { createTodo(); }); } else if (count === 1) { - expect(result).toEqual({ - called: true, - loading: true, - }); + expect(result.called).toEqual(true); + expect(result.loading).toEqual(true); } else if (count === 2) { - expect(result).toEqual({ - called: true, - loading: false, - data, - }); + expect(result.called).toEqual(true); + expect(result.loading).toEqual(false); + expect(result.data).toEqual(data); done(); } count++; @@ -111,10 +105,8 @@ it('can bind only the mutation and not rerender by props', done => { {(createTodo, result) => { if (count === 0) { - expect(result).toEqual({ - loading: false, - called: false, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(false); setTimeout(() => { createTodo().then(r => { expect(r!.data).toEqual(data); @@ -214,26 +206,20 @@ it('only shows result for the latest mutation that is in flight', done => { {(createTodo, result) => { if (count === 0) { - expect(result).toEqual({ - called: false, - loading: false, - }); + expect(result.called).toEqual(false); + expect(result.loading).toEqual(false); setTimeout(() => { createTodo(); createTodo(); }); } else if (count === 1) { - expect(result).toEqual({ - loading: true, - called: true, - }); + expect(result.called).toEqual(true); + expect(result.loading).toEqual(true); } else if (count === 2) { - expect(result).toEqual({ - loading: false, - called: true, - data: data2, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(true); + expect(result.data).toEqual(data2); } count++; return
; @@ -263,26 +249,20 @@ it('only shows the error for the latest mutation in flight', done => { {(createTodo, result) => { if (count === 0) { - expect(result).toEqual({ - called: false, - loading: false, - }); + expect(result.called).toEqual(false); + expect(result.loading).toEqual(false); setTimeout(() => { createTodo(); createTodo(); }); } else if (count === 1) { - expect(result).toEqual({ - loading: true, - called: true, - }); + expect(result.loading).toEqual(true); + expect(result.called).toEqual(true); } else if (count === 2) { - expect(result).toEqual({ - loading: false, - data: undefined, - called: true, - error: new Error('Network error: Error 2'), - }); + expect(result.loading).toEqual(false); + expect(result.data).toEqual(undefined); + expect(result.called).toEqual(true); + expect(result.error).toEqual(new Error('Network error: Error 2')); } count++; return
; @@ -463,16 +443,12 @@ it('performs a mutation with variables prop', done => { createTodo(); }); } else if (count === 1) { - expect(result).toEqual({ - loading: true, - called: true, - }); + expect(result.loading).toEqual(true); + expect(result.called).toEqual(true); } else if (count === 2) { - expect(result).toEqual({ - loading: false, - called: true, - data, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(true); + expect(result.data).toEqual(data); done(); } count++; @@ -509,16 +485,12 @@ it('allows passing a variable to the mutate function', done => { createTodo({ variables }); }); } else if (count === 1) { - expect(result).toEqual({ - loading: true, - called: true, - }); + expect(result.loading).toEqual(true); + expect(result.called).toEqual(true); } else if (count === 2) { - expect(result).toEqual({ - loading: false, - called: true, - data, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(true); + expect(result.data).toEqual(data); done(); } count++; @@ -569,16 +541,12 @@ it('allows an optimistic response prop', done => { expect(dataInStore['Todo:99']).toEqual(optimisticResponse.createTodo); }); } else if (count === 1) { - expect(result).toEqual({ - loading: true, - called: true, - }); + expect(result.loading).toEqual(true); + expect(result.called).toEqual(true); } else if (count === 2) { - expect(result).toEqual({ - loading: false, - called: true, - data, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(true); + expect(result.data).toEqual(data); done(); } count++; @@ -622,11 +590,9 @@ it('allows passing an optimistic response to the mutate function', done => { expect(dataInStore['Todo:99']).toEqual(optimisticResponse.createTodo); }); } else if (count === 2) { - expect(result).toEqual({ - loading: false, - called: true, - data, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(true); + expect(result.data).toEqual(data); done(); } count++; @@ -880,11 +846,9 @@ it('allows for overriding the options passed in the props by passing them in the createTodo({ variables: variablesMutateFn }); }); } else if (count === 2) { - expect(result).toEqual({ - loading: false, - called: true, - data: data2, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(true); + expect(result.data).toEqual(data2); done(); } count++; @@ -953,10 +917,8 @@ it('updates if the client changes', done => { {(createTodo, result) => { if (count === 0) { - expect(result).toEqual({ - called: false, - loading: false, - }); + expect(result.called).toEqual(false); + expect(result.loading).toEqual(false); setTimeout(() => { createTodo(); }); @@ -968,10 +930,8 @@ it('updates if the client changes', done => { }); }); } else if (count === 3) { - expect(result).toEqual({ - called: false, - loading: false, - }); + expect(result.called).toEqual(false); + expect(result.loading).toEqual(false); setTimeout(() => { createTodo(); }); @@ -1023,11 +983,9 @@ it('uses client from props instead of one provided by context', () => { } if (count === 2) { - expect(result).toEqual({ - loading: false, - called: true, - data: data2, - }); + expect(result.loading).toEqual(false); + expect(result.called).toEqual(true); + expect(result.data).toEqual(data2); } count++; From b461e05e39a82c54e575358bfce93150b4492074 Mon Sep 17 00:00:00 2001 From: Hugh Willson Date: Mon, 18 Jun 2018 18:25:54 -0400 Subject: [PATCH 3/3] Changelog update --- Changelog.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Changelog.md b/Changelog.md index 5a4f4a9e13..fabb9c9fab 100644 --- a/Changelog.md +++ b/Changelog.md @@ -10,6 +10,9 @@ an `ApolloClient` instance set via `context`, by the `ApolloProvider` component. [PR #1890](https://github.com/apollographql/react-apollo/pull/1890) +- The `ApolloClient` instance used by a Mutation is now available in that + Mutation's result. + [PR #1945](https://github.com/apollographql/react-apollo/pull/1945) ### 2.1.5