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

ApolloContext: handling in React Server Components #10872

Merged
merged 6 commits into from May 16, 2023

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented May 15, 2023

The "per-React-Version-Singleton" ApolloContext is now stored on globalThis, not React.createContext, and throws an error message when accessed from React Server Components.

Before this change, rendering an ApolloProvider or using any of our hooks, would have resulted in this error message.
Screenshot 2023-05-15 at 14-27-36 Screenshot

This error message is very specific to Apollo Client, and not easy to google.

So instead, now this error message is displayed:

Screenshot 2023-05-15 at 15-19-48 Screenshot

Also, we do not try to store the "per-React-Singleton" version of ApolloContext on React.createContext any more, but store it in globalThis[Symbol.for('__APOLLO_CONTEXT__')].get(React), which does not pollute the react module and will likely avoid similar problems in the future.

For historical context why it was stored there, see #8798.

If all existing tests work, I would assume this is "tested enough".

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 May 15, 2023

🦋 Changeset detected

Latest commit: 2446ace

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

@phryneas phryneas self-assigned this May 15, 2023
Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

This change looks good. I had a couple questions, but nothing blocking. Thanks for making the library more resilient to new React features! 🔥

src/react/context/ApolloContext.ts Show resolved Hide resolved
src/react/context/ApolloContext.ts Show resolved Hide resolved
@phryneas
Copy link
Member Author

Note to self: As I based this on #10869, that PR needs to be merged before this PR.

@phryneas phryneas changed the base branch from pr/fix-7626 to release-3.8 May 16, 2023 08:42
@github-actions
Copy link
Contributor

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 35.8 KB (-0.02% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" 45.48 KB (+0.48% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" 34.05 KB (0%)
import { ApolloProvider } from "dist/react/index.js" 1.05 KB (+23.31% 🔺)
import { useQuery } from "dist/react/index.js" 4.55 KB (+4.65% 🔺)
import { useLazyQuery } from "dist/react/index.js" 4.86 KB (+4.34% 🔺)
import { useMutation } from "dist/react/index.js" 2.72 KB (+7.91% 🔺)
import { useSubscription } from "dist/react/index.js" 2.54 KB (+7.96% 🔺)
import { useSuspenseQuery_experimental } from "dist/react/index.js" 3.74 KB (+5.81% 🔺)
import { useBackgroundQuery_experimental } from "dist/react/index.js" 3.17 KB (+6.75% 🔺)
import { useReadQuery_experimental } from "dist/react/index.js" 1.12 KB (0%)
import { useFragment_experimental } from "dist/react/index.js" 1.98 KB (+11.32% 🔺)

@phryneas phryneas merged commit 96b4f88 into release-3.8 May 16, 2023
5 checks passed
alessbell added a commit that referenced this pull request Jun 6, 2023
@alessbell alessbell mentioned this pull request Jun 6, 2023
3 tasks
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants