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

Experiment: tracked networkStatus on useBackgroundQuery #11183

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

phryneas
Copy link
Member

This is an experiment, and if we want to follow up could also be very useful on other hooks:

It adds a networkStatus field to useBackgroundQuery, but in an "access-tracking" way: If the component doesn't read the field from the result object, the component will not rerender if networkStatus changes, but if the component does read the field, the component will rerender.

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

@changeset-bot
Copy link

changeset-bot bot commented Aug 30, 2023

⚠️ No Changeset found

Latest commit: ba9e58a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@github-actions
Copy link
Contributor

github-actions bot commented Aug 30, 2023

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 37.52 KB (+0.65% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" 44.57 KB (+1.87% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" (production) 43.09 KB (+1.83% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" 32.52 KB (0%)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" (production) 31.27 KB (0%)
import { ApolloProvider } from "dist/react/index.js" 1.21 KB (0%)
import { ApolloProvider } from "dist/react/index.js" (production) 1.2 KB (0%)
import { useQuery } from "dist/react/index.js" 4.27 KB (-0.03% 🔽)
import { useQuery } from "dist/react/index.js" (production) 4.08 KB (0%)
import { useLazyQuery } from "dist/react/index.js" 4.58 KB (+0.03% 🔺)
import { useLazyQuery } from "dist/react/index.js" (production) 4.39 KB (-0.03% 🔽)
import { useMutation } from "dist/react/index.js" 2.52 KB (0%)
import { useMutation } from "dist/react/index.js" (production) 2.5 KB (-0.04% 🔽)
import { useSubscription } from "dist/react/index.js" 2.23 KB (-0.05% 🔽)
import { useSubscription } from "dist/react/index.js" (production) 2.19 KB (-0.05% 🔽)
import { useSuspenseQuery } from "dist/react/index.js" 4.75 KB (-0.03% 🔽)
import { useSuspenseQuery } from "dist/react/index.js" (production) 4.19 KB (0%)
import { useBackgroundQuery } from "dist/react/index.js" 4.81 KB (+12.25% 🔺)
import { useBackgroundQuery } from "dist/react/index.js" (production) 4.17 KB (+12.77% 🔺)
import { useReadQuery } from "dist/react/index.js" 2.96 KB (0%)
import { useReadQuery } from "dist/react/index.js" (production) 2.91 KB (0%)
import { useFragment } from "dist/react/index.js" 2.07 KB (0%)
import { useFragment } from "dist/react/index.js" (production) 2.02 KB (0%)

Comment on lines +3210 to +3231
const c1 = screen.getByTestId("Consumer1");
const c1rc = within(c1).getByTestId("renderCount");
const c1ns = within(c1).getByTestId("networkStatus");
const c2 = screen.getByTestId("Consumer2");
const c2rc = within(c2).getByTestId("renderCount");

expect(c1rc).toHaveTextContent("1");
expect(c1ns).toHaveTextContent(String(NetworkStatus.loading));
expect(c2rc).toHaveTextContent("1");

const c1childRc = await within(c1).findByTestId("childRenderCount");
const c1childNs = within(c1).getByTestId("childNetworkStatus");
const c2childRc = within(c2).getByTestId("childRenderCount");
const c2childNs = within(c2).getByTestId("childNetworkStatus");

expect(c1rc).toHaveTextContent("2"); // !!!
expect(c1ns).toHaveTextContent(String(NetworkStatus.ready));
expect(c1childRc).toHaveTextContent("1");
expect(c1childNs).toHaveTextContent(String(NetworkStatus.ready));
expect(c2rc).toHaveTextContent("1"); // !!!
expect(c2childRc).toHaveTextContent("1");
expect(c2childNs).toHaveTextContent(String(NetworkStatus.ready));
Copy link
Member Author

Choose a reason for hiding this comment

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

Here we have Component 1 accessing networkStatus while Component 2 does not.
As a result, Component 1 renders twice, while Component 2 only renders once.

@netlify
Copy link

netlify bot commented Aug 30, 2023

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit ba9e58a
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/64f19246fc2b300008d065c2
😎 Deploy Preview https://deploy-preview-11183--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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant