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

Don't throw on missing results with cache.diff and other changes #12304

Merged
merged 55 commits into from
Jan 28, 2025

Conversation

jerelmiller
Copy link
Member

@jerelmiller jerelmiller commented Jan 24, 2025

This PR changes cache.diff to return null instead of throwing a MissingFieldError when returnPartialData is false. This better aligns with some of the other cache read APIs, such as readFragment where incomplete results return null instead of throwing errors.

This should allow us to update some of our usages of cache.diff throughout the codebase where we set returnPartialData to true. I believe this was done to avoid having cache.diff throw in the case of missing data. Ideally we only need to set returnPartialData when specified by the user (subject to future exploration and testing).

This change better aligns with some of the other cache read operations that return null with partial results, such as cache.readFragment().

Some other changes/improvements in this PR:

  • Cache.DiffResult<TData> is now a bit smarter and behaves closer to useFragment where it now returns a union to handle when complete is true or false. This should give better type safety, especially when complete is false since the behavior today returns TData instead of DeepPartial<TData> in this case.
  • The missing property on Cache.DiffResult is now either undefined or an instance of MissingFieldError. The array was removed as this information is all embedded in the MissingFieldError instance itself.

Copy link

changeset-bot bot commented Jan 24, 2025

🦋 Changeset detected

Latest commit: 3313ca3

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 Major

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

@jerelmiller jerelmiller requested a review from phryneas January 24, 2025 06:58
@jerelmiller jerelmiller marked this pull request as draft January 24, 2025 06:58
@jerelmiller jerelmiller removed the request for review from phryneas January 24, 2025 06:58
@svc-apollo-docs
Copy link

svc-apollo-docs commented Jan 24, 2025

⚠️ Docs preview not attached to branch

The preview was not built because the PR's base branch release-4.0 is not in the list of sources.

An Apollo team member can comment one of the following commands to dictate which branch to attach the preview to:

  • !docs set-base-branch version-2.6
  • !docs set-base-branch main

Build ID: c42b4b960001813e1dc22f42

Copy link

pkg-pr-new bot commented Jan 24, 2025

npm i https://pkg.pr.new/@apollo/client@12304

commit: 3313ca3

Copy link
Contributor

github-actions bot commented Jan 24, 2025

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 33.47 KB (+0.05% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" 41.41 KB (-0.01% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" (production) 38.72 KB (-0.11% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" 36.15 KB (-0.06% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" (production) 33.55 KB (-0.08% 🔽)
import { ApolloProvider } from "dist/react/index.js" 1.26 KB (0%)
import { ApolloProvider } from "dist/react/index.js" (production) 1.24 KB (0%)
import { useQuery } from "dist/react/index.js" 5.12 KB (0%)
import { useQuery } from "dist/react/index.js" (production) 4.21 KB (0%)
import { useLazyQuery } from "dist/react/index.js" 5.61 KB (0%)
import { useLazyQuery } from "dist/react/index.js" (production) 4.68 KB (0%)
import { useMutation } from "dist/react/index.js" 3.62 KB (0%)
import { useMutation } from "dist/react/index.js" (production) 2.84 KB (0%)
import { useSubscription } from "dist/react/index.js" 4.43 KB (0%)
import { useSubscription } from "dist/react/index.js" (production) 3.48 KB (0%)
import { useSuspenseQuery } from "dist/react/index.js" 5.51 KB (0%)
import { useSuspenseQuery } from "dist/react/index.js" (production) 4.17 KB (0%)
import { useBackgroundQuery } from "dist/react/index.js" 5.01 KB (0%)
import { useBackgroundQuery } from "dist/react/index.js" (production) 3.66 KB (0%)
import { useLoadableQuery } from "dist/react/index.js" 5.09 KB (0%)
import { useLoadableQuery } from "dist/react/index.js" (production) 3.74 KB (0%)
import { useReadQuery } from "dist/react/index.js" 3.41 KB (0%)
import { useReadQuery } from "dist/react/index.js" (production) 3.35 KB (0%)
import { useFragment } from "dist/react/index.js" 2.01 KB (-15.16% 🔽)
import { useFragment } from "dist/react/index.js" (production) 1.95 KB (-15.47% 🔽)

Copy link

netlify bot commented Jan 24, 2025

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit 3313ca3
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/67994db4119c4e00087591e4
😎 Deploy Preview https://deploy-preview-12304--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 configuration.

@jerelmiller jerelmiller force-pushed the jerel/cache-diff-improvements branch from db5aea0 to 8a26e33 Compare January 24, 2025 16:42
@@ -551,7 +551,7 @@ describe("ObservableQuery", () => {
{
const result = await stream.takeNext();

expect(result.data).toEqual({});
expect(result.data).toEqual(undefined);
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 is one great benefit of this change. We report this type as TData | undefined, yet at times we returned an empty object at runtime. With the change in place, we now properly get undefined.

@jerelmiller jerelmiller force-pushed the jerel/cache-diff-improvements branch from 294f707 to 5f341c1 Compare January 24, 2025 22:43
@jerelmiller jerelmiller marked this pull request as ready for review January 24, 2025 23:40
@jerelmiller jerelmiller requested a review from phryneas January 24, 2025 23:40
@@ -710,9 +712,11 @@ describe("cache-and-network", function () {
await observable.setVariables({ id: "2" });

await expect(stream).toEmitValue({
data: {},
data: undefined,
Copy link
Member Author

@jerelmiller jerelmiller Jan 24, 2025

Choose a reason for hiding this comment

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

Much better 🎉. It was always odd that we'd get back an empty object when returnPartialData was false.

@jerelmiller
Copy link
Member Author

Add a doc block to cache.diff for cache implementors

Copy link
Member

@phryneas phryneas left a comment

Choose a reason for hiding this comment

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

Some small things, but once those have been tackled: ✅

@github-actions github-actions bot added the auto-cleanup 🤖 label Jan 28, 2025
@jerelmiller jerelmiller merged commit 86469a2 into release-4.0 Jan 28, 2025
48 checks passed
@jerelmiller jerelmiller deleted the jerel/cache-diff-improvements branch January 28, 2025 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants