Skip to content

Commit 93bda88

Browse files
authored
Reset query store errors via ObservableQuery.resetQueryStoreErrors (#4941)
* Reset query store errors via `ObservableQuery.resetQueryStoreErrors` React Apollo relies on the `ObervableQuery->getCurrentResult` method to retrieve query results to show within its components. When an error occurs while fetching results, that error is stored in the query store that each `ObservableQuery` instance is configured to use. Unfortunately, `getCurrentResult` will only retrieve subsequent results if no error exists in the query store. `ObservableQuery` doesn't currently provide a way to clear out query store errors, which means when React Apollo originating requests that cause an error occur, the error is stored, and future valid requests can no longer be processed. This commit adds a `resetQueryStoreErrors` method to the `ObservableQuery` public API, that will allow React Apollo (and other consumers) to be able to clear out query store errors. Related to: apollographql/react-apollo#3090 * Changelog update * Remove unnecessary expect
1 parent a0e14f3 commit 93bda88

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
### Apollo Client (vNext)
66

7+
- A new `ObservableQuery.resetQueryStoreErrors()` method is now available that
8+
can be used to clear out `ObservableQuery` query store errors. <br/>
9+
[@hwillson](https://github.com/hwillson) in [#4941](https://github.com/apollographql/apollo-client/pull/4941)
710
- Documentation updates. <br/>
811
[@michael-watson](https://github.com/michael-watson) in [#4940](https://github.com/apollographql/apollo-client/pull/4940) <br/>
912
[@hwillson](https://github.com/hwillson) in [#4969](https://github.com/apollographql/apollo-client/pull/4969)

packages/apollo-client/src/core/ObservableQuery.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ export class ObservableQuery<
280280
this.isTornDown = false;
281281
}
282282

283+
public resetQueryStoreErrors() {
284+
const queryStore = this.queryManager.queryStore.get(this.queryId);
285+
if (queryStore) {
286+
queryStore.networkError = null;
287+
queryStore.graphQLErrors = [];
288+
}
289+
}
290+
283291
/**
284292
* Update the variables of this observable query, and fetch the new results.
285293
* This method should be preferred over `setVariables` in most use cases.

packages/apollo-client/src/core/__tests__/ObservableQuery.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
InMemoryCache,
55
IntrospectionFragmentMatcher,
66
} from 'apollo-cache-inmemory';
7+
import { GraphQLError } from 'graphql';
78

89
import mockQueryManager from '../../__mocks__/mockQueryManager';
910
import mockWatchQuery from '../../__mocks__/mockWatchQuery';
@@ -1998,4 +1999,48 @@ describe('ObservableQuery', () => {
19981999
});
19992000
});
20002001
});
2002+
2003+
describe('resetQueryStoreErrors', () => {
2004+
it("should remove any GraphQLError's stored in the query store", (done) => {
2005+
const graphQLError = new GraphQLError('oh no!');
2006+
2007+
const observable: ObservableQuery<any> = mockWatchQuery({
2008+
request: { query, variables },
2009+
result: { errors: [graphQLError] },
2010+
});
2011+
2012+
observable.subscribe({
2013+
error() {
2014+
const { queryManager } = (observable as any);
2015+
const queryStore = queryManager.queryStore.get(observable.queryId);
2016+
expect(queryStore.graphQLErrors).toEqual([graphQLError]);
2017+
2018+
observable.resetQueryStoreErrors();
2019+
expect(queryStore.graphQLErrors).toEqual([]);
2020+
2021+
done();
2022+
}
2023+
});
2024+
});
2025+
2026+
it("should remove network error's stored in the query store", (done) => {
2027+
const networkError = new Error('oh no!');
2028+
2029+
const observable: ObservableQuery<any> = mockWatchQuery({
2030+
request: { query, variables },
2031+
result: { data: dataOne },
2032+
});
2033+
2034+
observable.subscribe({
2035+
next() {
2036+
const { queryManager } = (observable as any);
2037+
const queryStore = queryManager.queryStore.get(observable.queryId);
2038+
queryStore.networkError = networkError;
2039+
observable.resetQueryStoreErrors();
2040+
expect(queryStore.networkError).toBeNull();
2041+
done();
2042+
}
2043+
});
2044+
});
2045+
});
20012046
});

0 commit comments

Comments
 (0)