Skip to content

Create mechanism to add experimental features to Apollo Client#12915

Merged
phryneas merged 3 commits intorelease-4.1from
pr/experiments
Sep 24, 2025
Merged

Create mechanism to add experimental features to Apollo Client#12915
phryneas merged 3 commits intorelease-4.1from
pr/experiments

Conversation

@phryneas
Copy link
Copy Markdown
Member

@phryneas phryneas commented Sep 9, 2025

This will allow to execute code in the constructor of Apollo Client, essentially allowing for us to modify anything, from overwriting methods, swapping out prototypes, overwriting hook behaviour etc.

These could be exposed as exports from @apollo/client/experiments/name, with usage like

import { throwOnErrorFieldAccess } from "@apollo/client/experiments/toe"
import { matchFragmentsByDocumentTransform } from "@apollo/client/experiments/noMorePossibleTypes"

export const client = new ApolloClient({
  link,
  cache,
  experiments: [throwOnErrorFieldAccess, matchFragmentsByQueryManipulation]
})

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Sep 9, 2025

🦋 Changeset detected

Latest commit: 49c0097

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

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 requested a review from jerelmiller September 9, 2025 09:38
@apollo-librarian
Copy link
Copy Markdown

apollo-librarian bot commented Sep 9, 2025

✅ Docs preview has no changes

The preview was not built because there were no changes.

Build ID: 7f75dba09c2b39a5bca5f17f
Build Logs: View logs

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Sep 9, 2025

npm i https://pkg.pr.new/apollographql/apollo-client/@apollo/client@12915

commit: 3fe4502

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 9, 2025

size-limit report 📦

Path Size
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (CJS) 43.5 KB (-0.08% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) (CJS) 38.54 KB (+0.26% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" 32.91 KB (-0.03% 🔽)
import { ApolloClient, InMemoryCache, HttpLink } from "@apollo/client" (production) 27.06 KB (+0.03% 🔺)
import { ApolloProvider } from "@apollo/client/react" 5.97 KB (0%)
import { ApolloProvider } from "@apollo/client/react" (production) 1001 B (0%)
import { useQuery } from "@apollo/client/react" 7.43 KB (0%)
import { useQuery } from "@apollo/client/react" (production) 2.39 KB (0%)
import { useLazyQuery } from "@apollo/client/react" 7.15 KB (0%)
import { useLazyQuery } from "@apollo/client/react" (production) 2.16 KB (0%)
import { useMutation } from "@apollo/client/react" 6.53 KB (0%)
import { useMutation } from "@apollo/client/react" (production) 1.52 KB (0%)
import { useSubscription } from "@apollo/client/react" 6.84 KB (0%)
import { useSubscription } from "@apollo/client/react" (production) 1.82 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" 8.64 KB (0%)
import { useSuspenseQuery } from "@apollo/client/react" (production) 3.67 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" 8.42 KB (0%)
import { useBackgroundQuery } from "@apollo/client/react" (production) 3.44 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" 8.38 KB (0%)
import { useLoadableQuery } from "@apollo/client/react" (production) 3.43 KB (0%)
import { useReadQuery } from "@apollo/client/react" 6.65 KB (0%)
import { useReadQuery } from "@apollo/client/react" (production) 1.66 KB (0%)
import { useFragment } from "@apollo/client/react" 6.71 KB (0%)
import { useFragment } from "@apollo/client/react" (production) 1.71 KB (0%)

@github-actions github-actions bot added the auto-cleanup 🤖 label Sep 22, 2025
@phryneas phryneas changed the base branch from main to release-4.1 September 23, 2025 07:33
@phryneas phryneas marked this pull request as ready for review September 23, 2025 07:37
Copy link
Copy Markdown
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.

Comment looks good. Up to you whether you like the suggestion or want to keep it as-is.

* Allows passing in "experiments", experimental features that might one day
* become part of Apollo Client's core functionality.
* Keep in mind that these features might change the core of Apollo Client.
* Do not pass in experiments that are not provided by Apollo.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
* Do not pass in experiments that are not provided by Apollo.
* Proceed with caution when supplying experiments that are not provided by Apollo.

Could we perhaps be a bit gentler here and instead of fully discouraging custom experiments, just say that they carry some risk? I don't want to entirely discourage someone from trying something in their own environment that could potentially be contributed back to the client, but agree we need to say that it carries risk. Thoughts?

Copy link
Copy Markdown
Member Author

@phryneas phryneas Sep 24, 2025

Choose a reason for hiding this comment

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

I don't feel good with that idea - by themselves, experiments don't allow you to do anything you couldn't already do in userland without them. They just encapsulate and hide that dangerous stuff happens.

So if someone were to create something like this in userland, I think I'd prefer if they would do

const client = new ApolloClient()
client['queryManager'].prototype.newMethod = () => { /* ... */ }

which makes it very visible that this is a very flimsy and potentially dangerous thing to do.
I don't really want experiments to live outside of our control, since when we change any of our internals (e.g. adding newMethod on our side), it might break experimentes, or get broken by an experiment. We can only guarantee that that doesn't happen if we are the only people writing and distributing them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We talked this through on video :)

@phryneas phryneas merged commit c97b145 into release-4.1 Sep 24, 2025
39 checks passed
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 25, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants