diff --git a/Changelog.md b/Changelog.md index c5c0b7453c..1e4c82c3b5 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,16 @@ provider value is reset to the previous value it had after its children are walked.
[@mitchellhamilton](https://github.com/mitchellhamilton) in [#2304](https://github.com/apollographql/react-apollo/pull/2304) +- Revert:
+ When a query failed on the first result, the query result `data` was being + returned as `undefined`. This behavior has been changed so that `data` is + returned as an empty object. This makes checking for data (e.g. + instead of `data && data.user` you can just check `data.user`) and + destructring (e.g. `{ data: { user } }`) easier. **Note:** this could + potentially hurt applications that are relying on a falsey check of `data` + to see if any query errors have occurred. A better (and supported) way to + check for errors is to use the result `errors` property.
+ [#1983](https://github.com/apollographql/react-apollo/pull/1983) ## 2.2.1 (September 26, 2018) diff --git a/src/Query.tsx b/src/Query.tsx index b428b9fae2..4b76b35414 100644 --- a/src/Query.tsx +++ b/src/Query.tsx @@ -74,7 +74,7 @@ export interface QueryResult // I'm aware of. So intead we enforce checking for data // like so result.data!.user. This tells TS to use TData // XXX is there a better way to do this? - data: TData | {}; + data: TData | undefined; error?: ApolloError; loading: boolean; networkStatus: NetworkStatus; @@ -391,12 +391,9 @@ export default class Query extends if (loading) { Object.assign(data.data, this.previousData, currentResult.data); } else if (error) { - const lastResult = this.queryObservable!.getLastResult(); - if (lastResult) { - Object.assign(data, { - data: lastResult.data, - }); - } + Object.assign(data, { + data: (this.queryObservable!.getLastResult() || {}).data, + }); } else { const { fetchPolicy } = this.queryObservable!.options; const { partialRefetch } = this.props; diff --git a/test/client/Query.test.tsx b/test/client/Query.test.tsx index f7e96246b5..1e12482627 100644 --- a/test/client/Query.test.tsx +++ b/test/client/Query.test.tsx @@ -163,7 +163,6 @@ describe('Query component', () => { return null; } catchAsyncError(done, () => { - expect(result.data).toEqual({}); expect(result.error).toEqual(new Error('Network error: error occurred')); done(); }); diff --git a/test/client/getDataFromTree.test.tsx b/test/client/getDataFromTree.test.tsx index c3ef8df508..d86da0beff 100644 --- a/test/client/getDataFromTree.test.tsx +++ b/test/client/getDataFromTree.test.tsx @@ -538,11 +538,9 @@ describe('SSR', () => { class CurrentUserQuery extends Query {} - const hasOwn = Object.prototype.hasOwnProperty; - const WrappedElement = () => ( - {({ data, loading }: { data: Data; loading: boolean }) => ( + {({ data, loading }) => (
{loading || !data ? 'loading' : data.currentUser!.firstName}
)}
@@ -1291,7 +1289,7 @@ describe('SSR', () => { const Element = (props: { id: string }) => ( - {({ data, loading }: { data: Data; loading: boolean }) => ( + {({ data, loading }) => (
{loading || !data ? 'loading' : data.currentUser!.firstName}
)}