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

add (TeeTo|ReadFrom)ReadableStreamLink links #387

Merged
merged 9 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-squids-deny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client-react-streaming": minor
---

add `TeeToReadableStreamLink` and `ReadFromReadableStreamLink`
19 changes: 19 additions & 0 deletions packages/client-react-streaming/package-shape.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"registerApolloClient",
"ReadFromReadableStreamLink",
"TeeToReadableStreamLink",
"readFromReadableStream",
"teeToReadableStream",
"built_for_rsc"
],
"browser": [
Expand All @@ -17,7 +21,12 @@
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"WrapApolloProvider",
"skipDataTransport",
"resetApolloSingletons",
"ReadFromReadableStreamLink",
"TeeToReadableStreamLink",
"readFromReadableStream",
"teeToReadableStream",
"built_for_browser"
],
"node": [
Expand All @@ -28,7 +37,12 @@
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"WrapApolloProvider",
"skipDataTransport",
"resetApolloSingletons",
"ReadFromReadableStreamLink",
"TeeToReadableStreamLink",
"readFromReadableStream",
"teeToReadableStream",
"built_for_ssr"
],
"edge-light,worker,browser": [
Expand All @@ -39,7 +53,12 @@
"RemoveMultipartDirectivesLink",
"SSRMultipartLink",
"WrapApolloProvider",
"skipDataTransport",
"resetApolloSingletons",
"ReadFromReadableStreamLink",
"TeeToReadableStreamLink",
"readFromReadableStream",
"teeToReadableStream",
"built_for_ssr"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,25 @@ export class ApolloClientClientBaseImpl extends ApolloClientBase {
};
}

const skipDataTransportKey = Symbol.for("apollo.dataTransport.skip");
interface InternalContext {
[skipDataTransportKey]?: boolean;
}

/**
* Apply to a context to prevent this operation from being transported over the SSR data transport mechanism.
* @param readableStream
* @param context
* @returns
*/
export function skipDataTransport<T extends Record<string, any>>(
context: T
): T & InternalContext {
return Object.assign(context, {
[skipDataTransportKey]: true,
});
}

class ApolloClientSSRImpl extends ApolloClientClientBaseImpl {
private forwardedQueries = new (getTrieConstructor(this))();

Expand All @@ -315,6 +334,9 @@ class ApolloClientSSRImpl extends ApolloClientClientBaseImpl {
if (
options.fetchPolicy !== "cache-only" &&
options.fetchPolicy !== "standby" &&
!(options.context as InternalContext | undefined)?.[
skipDataTransportKey
] &&
!this.forwardedQueries.peekArray(cacheKeyArr)
) {
// don't transport the same query over twice
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { InMemoryCache } from "./WrappedInMemoryCache.js";
export { ApolloClient } from "./WrappedApolloClient.js";
export { ApolloClient, skipDataTransport } from "./WrappedApolloClient.js";

export { resetApolloSingletons } from "./testHelpers.js";

Expand Down
137 changes: 137 additions & 0 deletions packages/client-react-streaming/src/ReadableStreamLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
import { ApolloLink, Observable } from "@apollo/client/index.js";
import type { FetchResult } from "@apollo/client/index.js";

export type ReadableStreamLinkEvent =
| { type: "next"; value: FetchResult }
| { type: "completed" }
| { type: "error" };

interface InternalContext {
[teeToReadableStreamKey]?: ReadableStreamDefaultController<ReadableStreamLinkEvent>;
[readFromReadableStreamKey]?: ReadableStream<ReadableStreamLinkEvent>;
}

const teeToReadableStreamKey = Symbol.for(
"apollo.tee.readableStreamController"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"apollo.tee.readableStreamController"
"apollo.readableStreamController"

Is there anything else we'd consider putting on apollo.tee.*? If not, I'd simplify this to a single namespace (same goes for the read key)

Copy link
Member Author

Choose a reason for hiding this comment

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

These are common enough that I see them colliding with something else in the future - it's so close to a web standard type that I would combine it with the method calling it: https://developer.mozilla.org/en-US/docs/Web/API/ReadableStreamDefaultController

);
const readFromReadableStreamKey = Symbol.for("apollo.read.readableStream");

/**
* Apply to a context that will be passed to a link chain containing `TeeToReadableStreamLink`.
* @param controller
* @param context
* @returns
*/
export function teeToReadableStream<T extends Record<string, any>>(
controller: ReadableStreamDefaultController<ReadableStreamLinkEvent>,
context: T
): T & InternalContext {
return Object.assign(context, {
[teeToReadableStreamKey]: controller,
});
}

/**
* Apply to a context that will be passed to a link chain containing `ReadFromReadableStreamLink`.
* @param readableStream
* @param context
* @returns
*/
export function readFromReadableStream<T extends Record<string, any>>(
Copy link
Member

Choose a reason for hiding this comment

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

Same question as the link. Should these be marked as @internal since they seem to be used internally in #389?

readableStream: ReadableStream<ReadableStreamLinkEvent>,
context: T
): T & InternalContext {
return Object.assign(context, {
[readFromReadableStreamKey]: readableStream,
});
}

/**
* A link that allows the request to be cloned into a readable stream, e.g. for
* transport of multipart responses from RSC or a server loader to the browser.
*/
export const TeeToReadableStreamLink = new ApolloLink((operation, forward) => {
jerelmiller marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +49 to +53
Copy link
Member

Choose a reason for hiding this comment

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

Looking at #389, it looks like these are used internally and not actually set by the user. If that is the case, should we mark these links as @internal? (same goes for the read link)

Suggested change
/**
* A link that allows the request to be cloned into a readable stream, e.g. for
* transport of multipart responses from RSC or a server loader to the browser.
*/
export const TeeToReadableStreamLink = new ApolloLink((operation, forward) => {
/**
* A link that allows the request to be cloned into a readable stream, e.g. for
* transport of multipart responses from RSC or a server loader to the browser.
*
* @internal
*/
export const TeeToReadableStreamLink = new ApolloLink((operation, forward) => {

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd say that everything in this package could be considered "internal" or as a building block for "build-your-own-framework". These links will e.g. be used outside of the ApolloClient implementations of this package in the React-Router implementation.

const context = operation.getContext() as InternalContext;

const controller = context[teeToReadableStreamKey];

if (controller) {
return new Observable((observer) => {
const subscription = forward(operation).subscribe({
next(result) {
controller.enqueue({ type: "next", value: result });
observer.next(result);
},
error(error) {
controller.enqueue({ type: "error" });
controller.close();
observer.error(error);
},
complete() {
controller.enqueue({ type: "completed" });
controller.close();
observer.complete();
},
});

return () => {
subscription.unsubscribe();
};
});
}

return forward(operation);
});

/**
* A link that allows the response to be read from a readable stream, e.g. for
* hydration of a multipart response from RSC or a server loader in the browser.
*/
export const ReadFromReadableStreamLink = new ApolloLink(
(operation, forward) => {
const context = operation.getContext() as InternalContext;

const eventSteam = context[readFromReadableStreamKey];
if (eventSteam) {
return new Observable((observer) => {
let aborted = false as boolean;
const reader = eventSteam.getReader();
consumeReader();

return () => {
aborted = true;
reader.cancel();
};

async function consumeReader() {
let event:
| ReadableStreamReadResult<ReadableStreamLinkEvent>
| undefined = undefined;
while (!aborted && !event?.done) {
event = await reader.read();
if (aborted) break;
if (event.value) {
switch (event.value.type) {
case "next":
observer.next(event.value.value);
break;
case "completed":
observer.complete();
break;
case "error":
observer.error(
new Error(
"Error from event stream. Redacted for security concerns."
)
);
break;
}
}
}
}
});
}

return forward(operation);
}
);
7 changes: 7 additions & 0 deletions packages/client-react-streaming/src/index.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ export {
InMemoryCache,
} from "./DataTransportAbstraction/index.js";
export type { TransportedQueryRef } from "./transportedQueryRef.js";
export {
ReadFromReadableStreamLink,
TeeToReadableStreamLink,
readFromReadableStream,
teeToReadableStream,
type ReadableStreamLinkEvent,
} from "./ReadableStreamLink.js";
1 change: 1 addition & 0 deletions packages/client-react-streaming/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export {
QueryEvent,
WrapApolloProvider,
WrappedApolloProvider,
skipDataTransport,
} from "./DataTransportAbstraction/index.js";
2 changes: 1 addition & 1 deletion scripts/verify-package-shape.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async function verifyESM(condition, entryPoint, pkg, shape) {
child.stdout?.on("data", (data) => (result += data.toString()));
child.stderr?.pipe(process.stderr);
await new Promise((resolve) => child.on("exit", resolve));
assert.deepStrictEqual(JSON.parse(result).sort(), shape);
assert.deepStrictEqual(JSON.parse(result).sort(), shape.sort());
}

/**
Expand Down
Loading