Skip to content

Commit e6526f4

Browse files
valerybugakovJames Baxley
authored andcommitted
Do not update query after changing ApiClient (apollographql#1145)
* Do not update query after changing ApiClient if mutation is provided to the container * Added test case and changelog entry for apollographql#1145
1 parent 5e4d1ce commit e6526f4

File tree

3 files changed

+72
-69
lines changed

3 files changed

+72
-69
lines changed

Changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
### vNext
44

55
### 1.4.16
6-
- upgrade to react-16
6+
- upgrade to react-16
77
- fix shallowEqual bug.
88
- Added notifyOnNetworkStatusChange to QueryOpts and MutationOpts Typesccript definitions [#1034](https://github.com/apollographql/react-apollo/pull/1034)
99
- Added variables types with Typescript [#997](https://github.com/apollographql/react-apollo/pull/997)
1010
- Made `ChildProps.data` non-optional [#1143](https://github.com/apollographql/react-apollo/pull/1143)
11+
- Fix: ensure `client` option can be used with mutation query [#1145](https://github.com/apollographql/react-apollo/pull/1145)
1112

1213
### 1.4.15
1314
- Fix: handle calling refetch in child componentDidMount

src/graphql.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ export default function graphql<
163163
// call any stacked refetch functions
164164
if (this.refetcherQueue) {
165165
const { args, resolve, reject } = this.refetcherQueue;
166-
this.queryObservable.refetch(args).then(resolve).catch(reject);
166+
this.queryObservable
167+
.refetch(args)
168+
.then(resolve)
169+
.catch(reject);
167170
}
168171
}
169172
}
@@ -179,6 +182,10 @@ export default function graphql<
179182

180183
this.shouldRerender = true;
181184

185+
if (this.type === DocumentType.Mutation) {
186+
return;
187+
}
188+
182189
if (this.client !== client && this.client !== nextContext.client) {
183190
if (client) {
184191
this.client = client;
@@ -195,9 +202,7 @@ export default function graphql<
195202
}
196203
return;
197204
}
198-
if (this.type === DocumentType.Mutation) {
199-
return;
200-
}
205+
201206
if (
202207
this.type === DocumentType.Subscription &&
203208
operationOptions.shouldResubscribe &&
@@ -209,6 +214,7 @@ export default function graphql<
209214
this.subscribeToQuery();
210215
return;
211216
}
217+
212218
if (this.shouldSkip(nextProps)) {
213219
if (!this.shouldSkip(this.props)) {
214220
// if this has changed, we better unsubscribe

test/react-web/client/graphql/mutations/lifecycle.test.tsx

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,36 @@ import assign = require('object-assign');
55

66
import ApolloClient from 'apollo-client';
77

8-
declare function require(name: string)
8+
declare function require(name: string);
99

1010
import { mockNetworkInterface } from '../../../../../src/test-utils';
1111

1212
import { ApolloProvider, graphql } from '../../../../../src';
1313

14+
const query = gql`
15+
mutation addPerson($id: Int) {
16+
allPeople(id: $id) {
17+
people {
18+
name
19+
}
20+
}
21+
}
22+
`;
23+
24+
const data = { allPeople: { people: [{ name: 'Luke Skywalker' }] } };
25+
26+
const createClient = variables => {
27+
const networkInterface = mockNetworkInterface({
28+
request: { query, variables },
29+
result: { data },
30+
});
31+
32+
return new ApolloClient({ networkInterface, addTypename: false });
33+
};
34+
1435
describe('[mutations] lifecycle', () => {
1536
it('allows falsy values in the mapped variables from props', done => {
16-
const query = gql`
17-
mutation addPerson($id: Int) {
18-
allPeople(id: $id) {
19-
people {
20-
name
21-
}
22-
}
23-
}
24-
`;
25-
const data = { allPeople: { people: [{ name: 'Luke Skywalker' }] } };
26-
const variables = { id: null };
27-
const networkInterface = mockNetworkInterface({
28-
request: { query, variables },
29-
result: { data },
30-
});
31-
const client = new ApolloClient({ networkInterface, addTypename: false });
37+
const client = createClient({ id: null });
3238

3339
@graphql(query)
3440
class Container extends React.Component<any, any> {
@@ -51,22 +57,7 @@ describe('[mutations] lifecycle', () => {
5157
});
5258

5359
it("errors if the passed props don't contain the needed variables", () => {
54-
const query = gql`
55-
mutation addPerson($first: Int) {
56-
allPeople(first: $first) {
57-
people {
58-
name
59-
}
60-
}
61-
}
62-
`;
63-
const data = { allPeople: { people: [{ name: 'Luke Skywalker' }] } };
64-
const variables = { first: 1 };
65-
const networkInterface = mockNetworkInterface({
66-
request: { query, variables },
67-
result: { data },
68-
});
69-
const client = new ApolloClient({ networkInterface, addTypename: false });
60+
const client = createClient({ first: 1 });
7061
const Container = graphql(query)(() => null);
7162

7263
try {
@@ -81,21 +72,7 @@ describe('[mutations] lifecycle', () => {
8172
});
8273

8374
it('rebuilds the mutation on prop change when using `options`', done => {
84-
const query = gql`
85-
mutation addPerson {
86-
allPeople(first: 1) {
87-
people {
88-
name
89-
}
90-
}
91-
}
92-
`;
93-
const data = { allPeople: { people: [{ name: 'Luke Skywalker' }] } };
94-
const networkInterface = mockNetworkInterface({
95-
request: { query },
96-
result: { data },
97-
});
98-
const client = new ApolloClient({ networkInterface, addTypename: false });
75+
const client = createClient({});
9976

10077
function options(props) {
10178
// expect(props.listId).toBe(2);
@@ -112,6 +89,7 @@ describe('[mutations] lifecycle', () => {
11289
return null;
11390
}
11491
}
92+
11593
class ChangingProps extends React.Component<any, any> {
11694
state = { listId: 1 };
11795

@@ -132,22 +110,7 @@ describe('[mutations] lifecycle', () => {
132110
});
133111

134112
it('can execute a mutation with custom variables', done => {
135-
const query = gql`
136-
mutation addPerson($id: Int) {
137-
allPeople(id: $id) {
138-
people {
139-
name
140-
}
141-
}
142-
}
143-
`;
144-
const data = { allPeople: { people: [{ name: 'Luke Skywalker' }] } };
145-
const variables = { id: 1 };
146-
const networkInterface = mockNetworkInterface({
147-
request: { query, variables },
148-
result: { data },
149-
});
150-
const client = new ApolloClient({ networkInterface, addTypename: false });
113+
const client = createClient({ id: 1 });
151114

152115
@graphql(query)
153116
class Container extends React.Component<any, any> {
@@ -168,4 +131,37 @@ describe('[mutations] lifecycle', () => {
168131
</ApolloProvider>,
169132
);
170133
});
134+
135+
it('accepts client in options', done => {
136+
const client = createClient({});
137+
138+
@graphql(query, { options: () => ({ client }) })
139+
class Container extends React.Component<any, any> {
140+
componentWillReceiveProps(props) {
141+
if (props.listId !== 2) return;
142+
props.mutate().then(x => done());
143+
}
144+
render() {
145+
return null;
146+
}
147+
}
148+
149+
class ChangingProps extends React.Component<any, any> {
150+
state = { listId: 1 };
151+
152+
componentDidMount() {
153+
setTimeout(() => this.setState({ listId: 2 }), 50);
154+
}
155+
156+
render() {
157+
return <Container listId={this.state.listId} />;
158+
}
159+
}
160+
161+
renderer.create(
162+
<ApolloProvider client={client}>
163+
<ChangingProps />
164+
</ApolloProvider>,
165+
);
166+
});
171167
});

0 commit comments

Comments
 (0)