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

Use a symbol property instead of instanceOf to check for correct ApolloClient/InMemoryCache #302

Merged
merged 4 commits into from
May 29, 2024

Conversation

phryneas
Copy link
Member

No description provided.

Copy link

relativeci bot commented May 23, 2024

#211 Bundle Size — 1.07MiB (+6.54%).

99a8cd4(current) vs afa5bcc main#189(baseline)

Warning

Bundle contains 1 duplicate package – View duplicate packages

Bundle metrics  Change 9 changes Regression 3 regressions Improvement 1 improvement
                 Current
#211
     Baseline
#189
Regression  Initial JS 939.02KiB(+5.33%) 891.55KiB
Regression  Initial CSS 70B(+100%) 0B
Change  Cache Invalidation 85.99% 0.04%
Change  Chunks 32(+33.33%) 24
Change  Assets 56(+24.44%) 45
Change  Modules 591(+15.43%) 512
Regression  Duplicate Modules 110(+266.67%) 30
Change  Duplicate Code 7.61%(+489.92%) 1.29%
Improvement  Packages 26(-10.34%) 29
No change  Duplicate Packages 1 1
Bundle size by type  Change 3 changes Regression 3 regressions
                 Current
#211
     Baseline
#189
Regression  JS 1.06MiB (+6.31%) 1MiB
Regression  Other 8.67KiB (+44.68%) 5.99KiB
Regression  CSS 70B (+100%) 0B

Bundle analysis reportBranch pr/drop-instanceofProject dashboard

if (!(clientRef.current instanceof ApolloClient)) {
throw new Error(
`When using \`ApolloClient\` in streaming SSR, you must use the \`${WrappedApolloProvider.info.client}\` export provided by \`"${WrappedApolloProvider.info.pkg}"\`.`
assertInstance(
Copy link
Member Author

Choose a reason for hiding this comment

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

No need to do this check on every render, doing it in the one render that initializes clientRef is enough.

const wrappers = Symbol.for("apollo.hook.wrappers");
class ApolloClientBase<TCacheShape> extends OrigApolloClient<TCacheShape> {
class ApolloClientBase extends OrigApolloClient<NormalizedCacheObject> {
Copy link
Member Author

@phryneas phryneas May 23, 2024

Choose a reason for hiding this comment

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

Since we now only accept InMemoryCache in the types (this was already the case before in runtime), that type argument will always be NormalizedCacheObject - it's inferred from the cache.
That also means we can drop the generic from all the internal implementations.

/**
* Information about the current package and it's export names, for use in error messages.
*
* @internal
*/
static readonly info = bundle;

constructor(options: ApolloClientOptions<TCacheShape>) {
[sourceSymbol]: string;
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 the real core of this PR. Sorry for all the other noise!

Partial<ApolloClientBrowserImpl<TCacheShape>>,
Partial<ApolloClientSSRImpl<TCacheShape>>
export class ApolloClient<
// this generic is obsolete as we require a `InMemoryStore`, which fixes this generic to `NormalizedCacheObject` anyways
Copy link
Member Author

Choose a reason for hiding this comment

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

Keeping a generic here for backwards compat, but we won't do anything with it.

Comment on lines +3 to +5
import { outsideOf } from "@internal/test-utils/runInConditions.js";
import { browserEnv } from "@internal/test-utils/react.js";
import { silenceConsoleErrors } from "@internal/test-utils/console.js";
Copy link
Member Author

Choose a reason for hiding this comment

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

Since we now have tests in both bundles, I've moved common test util code into a new package, @internal/test-utils.

Comment on lines -61 to -62
const jsdom = await import("global-jsdom");
using _cleanupJSDOM = { [Symbol.dispose]: jsdom.default() };
Copy link
Member Author

Choose a reason for hiding this comment

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

JSDom setup and other preparations moved into browserEnv.

*
* @public
*/
export class InMemoryCache extends UpstreamInMemoryCache {
Copy link
Member Author

Choose a reason for hiding this comment

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

We add our own implementation here to ensure the error messages mention this package, not the parent package.

Comment on lines +1 to +3
export function silenceConsoleErrors(): {
[Symbol.dispose](): void;
};
Copy link
Member Author

Choose a reason for hiding this comment

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

Just committing the generated .d.ts files here - setting up something with project references doesn't work nicely with tsx, and honestly it's not worth the hassle.

@@ -0,0 +1,9 @@
export function silenceConsoleErrors() {
Copy link
Member Author

Choose a reason for hiding this comment

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

As a tradeoff, these are now .js files with JsDoc, not .ts files.

@phryneas phryneas marked this pull request as ready for review May 23, 2024 11:07
@phryneas phryneas added this to the 0.11.0 milestone May 23, 2024
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.

LGTM!

packages/client-react-streaming/src/importErrors.test.tsx Outdated Show resolved Hide resolved
Co-authored-by: Jerel Miller <jerelmiller@gmail.com>
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.

2 participants