Skip to content

Commit

Permalink
Merge pull request #8000 from apollographql/client.refetchQueries-revamp
Browse files Browse the repository at this point in the history
Improve standalone client.refetchQueries method to support automatic
detection of queries needing to be refetched.
  • Loading branch information
benjamn authored May 18, 2021
2 parents 24f894d + 491eeaf commit a5c3308
Show file tree
Hide file tree
Showing 21 changed files with 1,361 additions and 268 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
- `InMemoryCache` supports a new method called `batch`, which is similar to `performTransaction` but takes named options rather than positional parameters. One of these named options is an `onDirty(watch, diff)` callback, which can be used to determine which watched queries were invalidated by the `batch` operation. <br/>
[@benjamn](https://github.com/benjamn) in [#7819](https://github.com/apollographql/apollo-client/pull/7819)

- Mutations now accept an optional callback function called `reobserveQuery`, which will be passed the `ObservableQuery` and `Cache.DiffResult` objects for any queries invalidated by cache writes performed by the mutation's final `update` function. Using `reobserveQuery`, you can override the default `FetchPolicy` of the query, by (for example) calling `ObservableQuery` methods like `refetch` to force a network request. This automatic detection of invalidated queries provides an alternative to manually enumerating queries using the `refetchQueries` mutation option. Also, if you return a `Promise` from `reobserveQuery`, the mutation will automatically await that `Promise`, rendering the `awaitRefetchQueries` option unnecessary. <br/>
- Mutations now accept an optional callback function called `onQueryUpdated`, which will be passed the `ObservableQuery` and `Cache.DiffResult` objects for any queries invalidated by cache writes performed by the mutation's final `update` function. Using `onQueryUpdated`, you can override the default `FetchPolicy` of the query, by (for example) calling `ObservableQuery` methods like `refetch` to force a network request. This automatic detection of invalidated queries provides an alternative to manually enumerating queries using the `refetchQueries` mutation option. Also, if you return a `Promise` from `onQueryUpdated`, the mutation will automatically await that `Promise`, rendering the `awaitRefetchQueries` option unnecessary. <br/>
[@benjamn](https://github.com/benjamn) in [#7827](https://github.com/apollographql/apollo-client/pull/7827)

- Support `client.refetchQueries` as an imperative way to refetch queries, without having to pass `options.refetchQueries` to `client.mutate`. <br/>
[@dannycochran](https://github.com/dannycochran) in [#7431](https://github.com/apollographql/apollo-client/pull/7431)

- Improve standalone `client.refetchQueries` method to support automatic detection of queries needing to be refetched. <br/>
[@benjamn](https://github.com/benjamn) in [#8000](https://github.com/apollographql/apollo-client/pull/8000)

- When `@apollo/client` is imported as CommonJS (for example, in Node.js), the global `process` variable is now shadowed with a stripped-down object that includes only `process.env.NODE_ENV` (since that's all Apollo Client needs), eliminating the significant performance penalty of repeatedly accessing `process.env` at runtime. <br/>
[@benjamn](https://github.com/benjamn) in [#7627](https://github.com/apollographql/apollo-client/pull/7627)

Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,14 @@ Array [
"graphQLResultHasError",
"hasClientExports",
"hasDirectives",
"isDocumentNode",
"isField",
"isInlineFragment",
"isNonEmptyArray",
"isReference",
"iterateObserversSafely",
"makeReference",
"makeUniqueId",
"maybeDeepFreeze",
"mergeDeep",
"mergeDeepArray",
Expand Down
12 changes: 7 additions & 5 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2428,16 +2428,18 @@ describe('client', () => {
});

it('has a refetchQueries method which calls QueryManager', async () => {
// TODO(dannycochran)
const client = new ApolloClient({
link: ApolloLink.empty(),
cache: new InMemoryCache(),
});

// @ts-ignore
const spy = jest.spyOn(client.queryManager, 'refetchQueries');
await client.refetchQueries(['Author1']);
expect(spy).toHaveBeenCalled();
const spy = jest.spyOn(client['queryManager'], 'refetchQueries');
spy.mockImplementation(() => new Map);

const options = { include: ['Author1'] };
await client.refetchQueries(options);

expect(spy).toHaveBeenCalledWith(options);
});

itAsync('should propagate errors from network interface to observers', (resolve, reject) => {
Expand Down
Loading

0 comments on commit a5c3308

Please sign in to comment.