Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve standalone client.refetchQueries method to support automatic detection of queries needing to be refetched. #8000

Merged
merged 47 commits into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
eddad5a
Rename cache.batch onDirty option to onWatchUpdated.
benjamn Apr 6, 2021
97857ba
Rename mutation reobserveQuery callback to onQueryUpdated.
benjamn Apr 6, 2021
204ae77
Remove optimistic layer at same time as final mutation update.
benjamn Apr 6, 2021
82c68c8
Improve options API of client.refetchQueries method.
benjamn Apr 6, 2021
55bc6be
Ensure optimistic layers get removed in QueryManager#refetchQueries.
benjamn Apr 8, 2021
b8c18d4
Fix bug due to Promise.all not understanding iterators.
benjamn Apr 8, 2021
3936f04
Fix test failing due to incorrect usage of queryManager.refetchQueries.
benjamn Apr 8, 2021
d096261
Test that refetchQueries now warns when named query not found.
benjamn Apr 8, 2021
c760500
Ensure ObservableQuery is created for PureQueryOptions refetches.
benjamn Apr 8, 2021
21f0986
Separate {Public,Private}RefetchQueriesOptions.
benjamn Apr 9, 2021
b94956e
Simplify client.refetchQueries return type.
benjamn Apr 16, 2021
87dddf2
Allow onQueryUpdated callbacks to return a boolean.
benjamn Apr 16, 2021
8ef7d65
Pick unique optimistic layer ID for optimistic client.refetchQueries.
benjamn Apr 16, 2021
1282b91
Pass previous diff to watch.callback, onWatchUpdated, and onQueryUpda…
benjamn Apr 19, 2021
4b66ff2
Use deep equality check before calling watch.callback.
benjamn Apr 19, 2021
58d320b
Improve spy mocking for queryManager.refetchQueries test.
benjamn Apr 20, 2021
897a4ff
Promote BatchOptions from ApolloCache module to Cache.BatchOptions.
benjamn Apr 20, 2021
6eb4d2c
Reuse diff/lastDiff terminology, instead of newDiff/oldDiff.
benjamn Apr 20, 2021
30fe809
Consolidate options destructuring.
benjamn Apr 20, 2021
f913260
{Public,Private}RefetchQueriesOptions => {,Internal}RefetchQueriesOpt…
benjamn Apr 20, 2021
929feab
Rename cache.batch options.transaction function to options.update.
benjamn Apr 20, 2021
8a52c58
Make OnQueryUpdated lastDiff parameter non-optional.
benjamn Apr 21, 2021
3439fd6
Improve/simplify refetchQueries options.include processing.
benjamn Apr 21, 2021
c9d0428
Make client.refetchQueries(...) awaitable, and rename results array.
benjamn Apr 21, 2021
6bfa317
Call fallback() if onQueryUpdated returns true for an included query.
benjamn Apr 22, 2021
0e9d5a5
Allow onQueryUpdated functions to return anything.
benjamn May 11, 2021
6bc76ac
Make explicitly-included refetchQueries queries (re)read from cache.
benjamn May 11, 2021
fd8a94c
Basic tests of client.refetchQueries API.
benjamn May 11, 2021
e02f8a1
Refetch updateCache-triggered queries if onQueryUpdated not provided.
benjamn May 11, 2021
967ade3
Add a few TODOs to client.refetchQueries test file.
benjamn May 12, 2021
dfe9e68
Make dirtying of removed EntityStore Layer fields more precise.
benjamn Jan 12, 2021
bee43b0
Test client.refetchQueries({ optimistic: true }).
benjamn May 12, 2021
70bd843
Move refetchQueries-related types into src/core/types.ts.
benjamn May 13, 2021
b75bd7d
Infer better refetchQueries return type from onQueryUpdated return type.
benjamn May 13, 2021
30139fc
Improve comments about IsStrictlyAny<T> and related types.
benjamn May 13, 2021
028fd75
Use interface instead of type for RefetchQueriesResult<TResult>.
benjamn May 13, 2021
8ccfbb2
Add a test of returning true from onQueryUpdated.
benjamn May 13, 2021
b000064
Add a test of returning false from onQueryUpdated.
benjamn May 13, 2021
dcb8288
Clarify remaining TODO about testing no-cache with refetchQueries.
benjamn May 13, 2021
0c3fad6
Failing test of using updateCache and returning true from onQueryUpda…
benjamn May 13, 2021
c948655
Inline maybeAddResult helper function.
benjamn May 13, 2021
01b13c7
Store both lastDiff and diff in includedQueriesById.
benjamn May 13, 2021
75e455c
Fix failing test of updateCache with onQueryUpdated returning true.
benjamn May 14, 2021
311f692
Use fewer forced typecasts in QueryManager#refetchQueries.
benjamn May 14, 2021
c2bce76
Move makeUniqueId from QueryManager.ts into @apollo/client/utilities.
benjamn May 14, 2021
512804c
Allow including client.refetchQueries queries by DocumentNode.
benjamn May 18, 2021
491eeaf
Update CHANGELOG.md to reflect changes made in PR #8000.
benjamn May 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
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
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
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