diff --git a/packages/apollo-client/src/core/ObservableQuery.ts b/packages/apollo-client/src/core/ObservableQuery.ts index e89a8f08acd..3681c08dbd0 100644 --- a/packages/apollo-client/src/core/ObservableQuery.ts +++ b/packages/apollo-client/src/core/ObservableQuery.ts @@ -77,6 +77,7 @@ export class ObservableQuery< private subscriptionHandles: Subscription[]; private lastResult: ApolloQueryResult; + private lastResultSnapshot: ApolloQueryResult; private lastError: ApolloError; constructor({ @@ -201,7 +202,7 @@ export class ObservableQuery< } const result = { - data: cloneDeep(data), + data, loading: isNetworkRequestInFlight(networkStatus), networkStatus, } as ApolloQueryResult; @@ -216,11 +217,24 @@ export class ObservableQuery< if (!partial) { this.lastResult = { ...result, stale: false }; + this.lastResultSnapshot = cloneDeep(this.lastResult); } return { ...result, partial } as ApolloCurrentResult; } + // Compares newResult to the snapshot we took of this.lastResult when it was + // first received. + public isDifferentFromLastResult(newResult: ApolloQueryResult) { + const { lastResultSnapshot: snapshot } = this; + return !( + snapshot && newResult && + snapshot.networkStatus === newResult.networkStatus && + snapshot.stale === newResult.stale && + isEqual(snapshot.data, newResult.data) + ); + } + // Returns the last result that observer.next was called with. This is not the same as // currentResult! If you're not sure which you need, then you probably need currentResult. public getLastResult(): ApolloQueryResult { @@ -585,7 +599,8 @@ export class ObservableQuery< const observer: Observer> = { next: (result: ApolloQueryResult) => { - this.lastResult = cloneDeep(result); + this.lastResult = result; + this.lastResultSnapshot = cloneDeep(result); this.observers.forEach(obs => obs.next && obs.next(result)); }, error: (error: ApolloError) => { diff --git a/packages/apollo-client/src/core/QueryManager.ts b/packages/apollo-client/src/core/QueryManager.ts index d14652c5772..7c2583721db 100644 --- a/packages/apollo-client/src/core/QueryManager.ts +++ b/packages/apollo-client/src/core/QueryManager.ts @@ -12,7 +12,6 @@ import { getQueryDefinition, isProduction, hasDirectives, - isEqual, } from 'apollo-utilities'; import { QueryScheduler } from '../scheduler/scheduler'; @@ -605,15 +604,9 @@ export class QueryManager { } if (observer.next) { - const isDifferentResult = !( - lastResult && - resultFromStore && - lastResult.networkStatus === resultFromStore.networkStatus && - lastResult.stale === resultFromStore.stale && - isEqual(lastResult.data, resultFromStore.data) - ); - - if (isDifferentResult || previouslyHadError) { + if (previouslyHadError || + !observableQuery || + observableQuery.isDifferentFromLastResult(resultFromStore)) { try { observer.next(resultFromStore); } catch (e) {