Skip to content

Commit c8d9a14

Browse files
rhberroJames Baxley
authored andcommitted
Skip should prevent options from being called when component properties were updated (apollographql#1181)
* Should declare undefined variables within the options property when skipping * Check if should skip before calling the mapPropsToOptions method * Create a separated test. * Add the fix to the Changelog.md file
1 parent e6526f4 commit c8d9a14

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

Changelog.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Change log
22

33
### vNext
4+
- fix skip on component update.
5+
- Fix: ensure `client` option can be used with mutation query [#1145](https://github.com/apollographql/react-apollo/pull/1145)
46

57
### 1.4.16
68
- upgrade to react-16
79
- fix shallowEqual bug.
810
- Added notifyOnNetworkStatusChange to QueryOpts and MutationOpts Typesccript definitions [#1034](https://github.com/apollographql/react-apollo/pull/1034)
911
- Added variables types with Typescript [#997](https://github.com/apollographql/react-apollo/pull/997)
1012
- 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)
1213

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

src/graphql.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,16 @@ export default function graphql<
172172
}
173173

174174
componentWillReceiveProps(nextProps, nextContext) {
175+
if (this.shouldSkip(nextProps)) {
176+
if (!this.shouldSkip(this.props)) {
177+
// if this has changed, we better unsubscribe
178+
this.unsubscribeFromQuery();
179+
}
180+
return;
181+
}
182+
175183
const { client } = mapPropsToOptions(nextProps);
184+
176185
if (
177186
shallowEqual(this.props, nextProps) &&
178187
(this.client === client || this.client === nextContext.client)
@@ -215,14 +224,6 @@ export default function graphql<
215224
return;
216225
}
217226

218-
if (this.shouldSkip(nextProps)) {
219-
if (!this.shouldSkip(this.props)) {
220-
// if this has changed, we better unsubscribe
221-
this.unsubscribeFromQuery();
222-
}
223-
return;
224-
}
225-
226227
this.updateQuery(nextProps);
227228
this.subscribeToQuery();
228229
}

test/react-web/client/graphql/queries/skip.test.tsx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,64 @@ describe('[queries] skip', () => {
300300
}, 25);
301301
});
302302

303+
it("doesn't run options or props when skipped even if the component updates", done => {
304+
const query = gql`
305+
query people {
306+
allPeople(first: 1) {
307+
people {
308+
name
309+
}
310+
}
311+
}
312+
`;
313+
314+
const networkInterface = mockNetworkInterface();
315+
316+
const client = new ApolloClient({ networkInterface, addTypename: false });
317+
318+
let queryWasSkipped = true;
319+
320+
@graphql(query, {
321+
skip: true,
322+
options: () => {
323+
queryWasSkipped = false;
324+
return {};
325+
},
326+
props: () => {
327+
queryWasSkipped = false;
328+
return {};
329+
},
330+
})
331+
class Container extends React.Component<any, any> {
332+
componentWillReceiveProps(props) {
333+
expect(queryWasSkipped).toBe(true);
334+
done();
335+
}
336+
render() {
337+
return null;
338+
}
339+
}
340+
341+
class Parent extends React.Component<any, any> {
342+
constructor() {
343+
super();
344+
this.state = { foo: 'bar' };
345+
}
346+
componentDidMount() {
347+
this.setState({ foo: 'baz' });
348+
}
349+
render() {
350+
return <Container foo={this.state.foo} />;
351+
}
352+
}
353+
354+
renderer.create(
355+
<ApolloProvider client={client}>
356+
<Parent />
357+
</ApolloProvider>,
358+
);
359+
});
360+
303361
it('allows you to skip a query without running it (alternate syntax)', done => {
304362
const query = gql`
305363
query people {

0 commit comments

Comments
 (0)