Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Include ApolloClient in MutationResult #1945

Merged
merged 4 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions src/Mutation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface MutationResult<TData = Record<string, any>> {
error?: ApolloError;
loading: boolean;
called: boolean;
client: ApolloClient<Object>;
}
export interface MutationContext {
client: ApolloClient<Object>;
Expand Down Expand Up @@ -167,6 +168,7 @@ class Mutation<TData = any, TVariables = OperationVariables> extends React.Compo
loading,
data,
error,
client: this.client,
};

return children(this.runMutation, result);
Expand Down
146 changes: 52 additions & 94 deletions test/client/Mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,18 @@ it('performs a mutation', done => {
<Mutation mutation={mutation}>
{(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++;
Expand All @@ -111,10 +105,8 @@ it('can bind only the mutation and not rerender by props', done => {
<Mutation mutation={mutation} ignoreResults>
{(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);
Expand Down Expand Up @@ -214,26 +206,20 @@ it('only shows result for the latest mutation that is in flight', done => {
<Mutation mutation={mutation} onCompleted={onCompleted}>
{(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 <div />;
Expand Down Expand Up @@ -263,26 +249,20 @@ it('only shows the error for the latest mutation in flight', done => {
<Mutation mutation={mutation} onError={onError}>
{(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 <div />;
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -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++;
Expand Down Expand Up @@ -953,10 +917,8 @@ it('updates if the client changes', done => {
<Mutation mutation={mutation}>
{(createTodo, result) => {
if (count === 0) {
expect(result).toEqual({
called: false,
loading: false,
});
expect(result.called).toEqual(false);
expect(result.loading).toEqual(false);
setTimeout(() => {
createTodo();
});
Expand All @@ -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();
});
Expand Down Expand Up @@ -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++;
Expand Down