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

feat(react-router): add SerializesTo brand type #12264

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

phryneas
Copy link

Apart from the turbo-stream support for ReadableStream, this is the only other change that we would need to add support for preloading Apollo Client data during SSR in loader functions.

The problem here is that Apollo Client uses a branded type QueryRef<TData, TVariables> that looks like this:

export interface QueryRef<TData = unknown, TVariables = unknown> {
  /** @internal */
  [QUERY_REF_BRAND]?(variables: TVariables): TData;
}

We are using an "imaginary" function here to preserve variance when passing around the type - TVariables should be contravariant, so it is referenced as a method argument, while TData is covariant, so it is passed as a method return value.

This function doesn't really exist on the transport object and no other properties on it are typed, since the user should never interact with the object apart from passing it into a consuming useReadQuery(queryRef) hook in the browser.

Unfortunately, currently that QueryRef above is turned into { [QUERY_REF_BRAND]?: undefined }, since Serialize removes all functions.

This SerializesTo type would be an escape hatch to allow it for e.g. library authors to pass around branded values like this by specifying what the "serialized" result type should be.

So the preloadQuery function that we provide for loaders would return something of the type SerializesTo<QueryRef<TData, TVariables>>.

Copy link

changeset-bot bot commented Nov 12, 2024

🦋 Changeset detected

Latest commit: b383d3c

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

This PR includes changesets to release 10 packages
Name Type
react-router Major
@react-router/architect Major
@react-router/cloudflare Major
@react-router/dev Major
react-router-dom Major
@react-router/express Major
@react-router/node Major
@react-router/serve Major
@react-router/fs-routes Major
@react-router/remix-routes-option-adapter 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

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Nov 12, 2024

Hi @phryneas,

Welcome, and thank you for contributing to React Router!

Before we consider your pull request, we ask that you sign our Contributor License Agreement (CLA). We require this only once.

You may review the CLA and sign it by adding your name to contributors.yml.

Once the CLA is signed, the CLA Signed label will be added to the pull request.

If you have already signed the CLA and received this response in error, or if you have any questions, please contact us at [email protected].

Thanks!

- The Remix team

@remix-cla-bot
Copy link
Contributor

remix-cla-bot bot commented Nov 12, 2024

Thank you for signing the Contributor License Agreement. Let's get this merged! 🥳

@MichaelDeBoey MichaelDeBoey changed the title Add SerializesTo brand type. feat(react-router): add SerializesTo brand type Nov 13, 2024
.changeset/sour-avocados-lick.md Outdated Show resolved Hide resolved
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.

2 participants