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

ObservableQuery.getCurrentResult: skip the cache #10631

Merged
merged 17 commits into from
Jun 22, 2023
Merged

ObservableQuery.getCurrentResult: skip the cache #10631

merged 17 commits into from
Jun 22, 2023

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented Mar 8, 2023

This is my attempt at solving #10222.

The test is taken from @jerelmiller over in #10239.

I'm not sure if this covers all possible cases (what should happen if all of this plays out on a subsequent query? Is that even possible there?), but at least it gets all the tests green.

One of the HOC tests is now rerendering one less time, but not getting any new data.

I'll add a changeset if we decide that this solution is feasible.

Checklist:

  • If this PR contains changes to the library itself (not necessary for e.g. docs updates), please include a changeset (see CONTRIBUTING.md)
  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests

jerelmiller and others added 2 commits March 8, 2023 11:22
if currently running the first query, started with a fetchPolicy meant
to ignore cache results
@changeset-bot
Copy link

changeset-bot bot commented Mar 8, 2023

🦋 Changeset detected

Latest commit: ee8dda5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -7,5 +7,6 @@
"typescript.tsdk": "node_modules/typescript/lib",
"cSpell.enableFiletypes": [
"mdx"
]
],
"jest.jestCommandLine": "node_modules/.bin/jest --config ./config/jest.config.js --ignoreProjects 'ReactDOM 17' --runInBand",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to add this with some commit - the Jest VSCode plugin cannot display it when two projects run the same test twice, so I just set it to exclude the React17 tests. Also adds --runInBand for the extension, which makes tests less flaky in general.

@@ -152,6 +154,7 @@ export class ObservableQuery<
this.queryManager = queryManager;

// active state
this.waitForOwnResult = skipCacheDataFor(options.fetchPolicy);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has to be initialized in the constructor, as getCurrentResult() will be called before the ObservableQuery is subscribed to for the first time.

@@ -421,7 +421,7 @@ describe('[queries] loading', () => {
]);
}, { interval: 1 });
await waitFor(() => {
expect(count).toBe(6);
expect(count).toBe(5);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now rerenders one less time. I checked it - render 5 and 6 had identical this.props.data, so this is probably a good thing:tm:?

@phryneas
Copy link
Member Author

phryneas commented Mar 8, 2023

(Tests are all green now, rerunning the test suite a third time. All that's red at this point is the file size.)

@phryneas phryneas changed the title ObservableQuery.currentResult: skip the cache ObservableQuery.getCurrentResult: skip the cache Mar 9, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 31, 2023

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 33.74 KB (+0.23% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" 42.48 KB (+0.2% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" 33.54 KB (+0.23% 🔺)
import { ApolloProvider } from "dist/react/index.js" 917 B (0%)
import { useQuery } from "dist/react/index.js" 25.05 KB (+0.38% 🔺)
import { useLazyQuery } from "dist/react/index.js" 25.37 KB (+0.41% 🔺)
import { useMutation } from "dist/react/index.js" 23.6 KB (+0.4% 🔺)
import { useSubscription } from "dist/react/index.js" 2.43 KB (0%)
import { useFragment_experimental } from "dist/react/index.js" 1.84 KB (0%)

@phryneas
Copy link
Member Author

/release:pr

@github-actions
Copy link
Contributor

A new release has been made for this PR. You can install it with npm i @apollo/client@0.0.0-pr-10631-20230531131910.

@phryneas phryneas self-assigned this Jun 13, 2023
@phryneas phryneas marked this pull request as ready for review June 14, 2023 15:56
@phryneas
Copy link
Member Author

phryneas commented Jun 14, 2023

I'm happy with the state of this now and have just added a ton of tests. Ready for review!

@phryneas
Copy link
Member Author

/release:pr

@github-actions
Copy link
Contributor

A new release has been made for this PR. You can install it with npm i @apollo/client@0.0.0-pr-10631-20230614160035.

Copy link
Member

@benjamn benjamn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finally cracking this important puzzle @phryneas!

Comment on lines 1009 to 1010
function skipCacheDataFor(fetchPolicy?: WatchQueryFetchPolicy) {
return fetchPolicy === "network-only" || fetchPolicy === "no-cache" || fetchPolicy === "standby";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth a comment that when typeof fetchPolicy === "undefined", that means cache-first by default, so skipCacheDataFor should return false (as it does already).

src/core/ObservableQuery.ts Outdated Show resolved Hide resolved
src/core/ObservableQuery.ts Show resolved Hide resolved
@efstathiosntonas
Copy link
Contributor

Thanks for this guys, @benjamn do you have an ETA on this?

@phryneas
Copy link
Member Author

@efstathiosntonas If nothing super-weird pops up, you can expect this to probably be in a patch release next week or the week after

Co-authored-by: Ben Newman <ben@apollographql.com>
src/core/ObservableQuery.ts Outdated Show resolved Hide resolved
@netlify
Copy link

netlify bot commented Jun 22, 2023

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit b360183
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/64946e0df3935f00085b8a2d
😎 Deploy Preview https://deploy-preview-10631--apollo-client-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Jun 22, 2023

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit ee8dda5
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/64947133c1ed1f000828c31c
😎 Deploy Preview https://deploy-preview-10631--apollo-client-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@phryneas phryneas merged commit b93388d into main Jun 22, 2023
@phryneas phryneas deleted the pr/10222 branch June 22, 2023 16:18
This was referenced Jun 22, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants