feat(react-router): add SerializesTo
brand type
#12264
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: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, whileTData
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 }
, sinceSerialize
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 typeSerializesTo<QueryRef<TData, TVariables>>
.