Skip to content

Commit

Permalink
Reset query store errors via ObservableQuery.resetQueryStoreErrors (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
hwillson authored Jun 17, 2019
1 parent a0e14f3 commit 93bda88
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Apollo Client (vNext)

- A new `ObservableQuery.resetQueryStoreErrors()` method is now available that
can be used to clear out `ObservableQuery` query store errors. <br/>
[@hwillson](https://github.com/hwillson) in [#4941](https://github.com/apollographql/apollo-client/pull/4941)
- Documentation updates. <br/>
[@michael-watson](https://github.com/michael-watson) in [#4940](https://github.com/apollographql/apollo-client/pull/4940) <br/>
[@hwillson](https://github.com/hwillson) in [#4969](https://github.com/apollographql/apollo-client/pull/4969)
Expand Down
8 changes: 8 additions & 0 deletions packages/apollo-client/src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ export class ObservableQuery<
this.isTornDown = false;
}

public resetQueryStoreErrors() {
const queryStore = this.queryManager.queryStore.get(this.queryId);
if (queryStore) {
queryStore.networkError = null;
queryStore.graphQLErrors = [];
}
}

/**
* Update the variables of this observable query, and fetch the new results.
* This method should be preferred over `setVariables` in most use cases.
Expand Down
45 changes: 45 additions & 0 deletions packages/apollo-client/src/core/__tests__/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
InMemoryCache,
IntrospectionFragmentMatcher,
} from 'apollo-cache-inmemory';
import { GraphQLError } from 'graphql';

import mockQueryManager from '../../__mocks__/mockQueryManager';
import mockWatchQuery from '../../__mocks__/mockWatchQuery';
Expand Down Expand Up @@ -1998,4 +1999,48 @@ describe('ObservableQuery', () => {
});
});
});

describe('resetQueryStoreErrors', () => {
it("should remove any GraphQLError's stored in the query store", (done) => {
const graphQLError = new GraphQLError('oh no!');

const observable: ObservableQuery<any> = mockWatchQuery({
request: { query, variables },
result: { errors: [graphQLError] },
});

observable.subscribe({
error() {
const { queryManager } = (observable as any);
const queryStore = queryManager.queryStore.get(observable.queryId);
expect(queryStore.graphQLErrors).toEqual([graphQLError]);

observable.resetQueryStoreErrors();
expect(queryStore.graphQLErrors).toEqual([]);

done();
}
});
});

it("should remove network error's stored in the query store", (done) => {
const networkError = new Error('oh no!');

const observable: ObservableQuery<any> = mockWatchQuery({
request: { query, variables },
result: { data: dataOne },
});

observable.subscribe({
next() {
const { queryManager } = (observable as any);
const queryStore = queryManager.queryStore.get(observable.queryId);
queryStore.networkError = networkError;
observable.resetQueryStoreErrors();
expect(queryStore.networkError).toBeNull();
done();
}
});
});
});
});

0 comments on commit 93bda88

Please sign in to comment.