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

Commit

Permalink
Merge pull request #548 from Vydia/include-cached-data-in-child-error…
Browse files Browse the repository at this point in the history
…-data

Include cached data with error
  • Loading branch information
helfer committed Apr 21, 2017
2 parents 52d153a + b52834f commit 0cd8c12
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 1 or 2 months), so that we can take advantage of SemVer to signify breaking changes from that point on.

### vNext
- Pass cached data to the child component along with the error. [PR #548](https://github.com/apollographql/react-apollo/pull/548)
- Fix version lock down for peer dependency version of React. [PR #626](https://github.com/apollographql/react-apollo/pull/626)
- Switch `graphql-tag` dependency to `2.0.0`. This isn't really a breaking change because we only export `gql` from `react-apollo`.
- Fix: convert deprecated `React.PropTypes` to `PropTypes` provided by the `prop-types` package. [PR #628](https://github.com/apollographql/react-apollo/pull/628)
Expand Down
3 changes: 3 additions & 0 deletions src/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,9 @@ export default function graphql(
if (loading) {
// while loading, we should use any previous data we have
assign(data, this.previousData, currentResult.data);
} else if (error) {
// if there is error, use any previously cached data
assign(data, (this.queryObservable.getLastResult() || {}).data);
} else {
assign(data, currentResult.data);
this.previousData = currentResult.data;
Expand Down
40 changes: 40 additions & 0 deletions test/react-web/client/graphql/queries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2580,4 +2580,44 @@ describe('queries', () => {
]);
});

it('passes any cached data when there is a GraphQL error', (done) => {
const query = gql`query people { allPeople(first: 1) { people { name } } }`;
const data = { allPeople: { people: [ { name: 'Luke Skywalker' } ] } };
const networkInterface = mockNetworkInterface(
{ request: { query }, result: { data } },
{ request: { query }, error: new Error('No Network Connection') }
);
const client = new ApolloClient({ networkInterface, addTypename: false });

let count = 0;
@graphql(query, { options: { notifyOnNetworkStatusChange: true } })
class Container extends React.Component<any, any> {
componentWillReceiveProps = (props) => {
switch (count++) {
case 0:
expect(props.data.allPeople).toEqual(data.allPeople);
props.data.refetch();
break;
case 1:
expect(props.data.loading).toBe(true);
expect(props.data.allPeople).toEqual(data.allPeople);
break;
case 2:
expect(props.data.loading).toBe(false);
expect(props.data.error).toBeTruthy();
expect(props.data.allPeople).toEqual(data.allPeople);
done();
break;
}
}

render() {
return null;
}
};

const wrapper = renderer.create(<ApolloProvider client={client}><Container /></ApolloProvider>);
}));


});

0 comments on commit 0cd8c12

Please sign in to comment.