Skip to content

Commit

Permalink
Only return newData when querying current results for no-cache or net…
Browse files Browse the repository at this point in the history
…work-only queries
  • Loading branch information
danilobuerger committed Jan 21, 2019
1 parent e5b238b commit e10275f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
11 changes: 6 additions & 5 deletions packages/apollo-client/src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ export class QueryManager<TStore> {
// `no-cache` since `getCurrentQueryResult` attemps to pull from
// `newData` first, following by trying the cache (which won't
// find a hit for `no-cache`).
if (fetchPolicy !== 'no-cache') {
if (fetchPolicy !== 'no-cache' && fetchPolicy !== 'network-only') {
this.setQuery(queryId, () => ({ newData: null }));
}

Expand Down Expand Up @@ -932,7 +932,7 @@ export class QueryManager<TStore> {
obs.complete();
}
});
}
},
};

// TODO: Should subscriptions also accept a `context` option to pass
Expand Down Expand Up @@ -968,12 +968,14 @@ export class QueryManager<TStore> {
observableQuery: ObservableQuery<T>,
optimistic: boolean = true,
) {
const { variables, query } = observableQuery.options;
const { variables, query, fetchPolicy } = observableQuery.options;
const lastResult = observableQuery.getLastResult();
const { newData } = this.getQuery(observableQuery.queryId);
// XXX test this
if (newData && newData.complete) {
return { data: newData.result, partial: false };
} else if (fetchPolicy === 'no-cache' || fetchPolicy === 'network-only') {
return { data: {}, partial: false };
} else {
try {
// the query is brand new, so we read from the store to see if anything is there
Expand Down Expand Up @@ -1093,7 +1095,7 @@ export class QueryManager<TStore> {
return new Promise<ApolloQueryResult<T>>((resolve, reject) => {
// Need to assign the reject function to the rejectFetchPromise variable
// in the outer scope so that we can refer to it in the .catch handler.
this.fetchQueryRejectFns.add(rejectFetchPromise = reject);
this.fetchQueryRejectFns.add((rejectFetchPromise = reject));

const subscription = execute(this.deduplicator, operation).subscribe({
next: (result: ExecutionResult) => {
Expand Down Expand Up @@ -1188,7 +1190,6 @@ export class QueryManager<TStore> {
this.setQuery(queryId, ({ subscriptions }) => ({
subscriptions: subscriptions.concat([subscription]),
}));

}).catch(error => {
this.fetchQueryRejectFns.delete(rejectFetchPromise);
throw error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ describe('ObservableQuery', () => {
fetchPolicy: 'network-only',
});
expect(stripSymbols(observable.currentResult())).toEqual({
data: dataOne,
data: {},
loading: true,
networkStatus: 1,
partial: false,
Expand Down

0 comments on commit e10275f

Please sign in to comment.