From 0ed7998c716c52534ba1507276f6b3c300364685 Mon Sep 17 00:00:00 2001 From: Lenz Weber-Tronic Date: Wed, 27 Mar 2024 14:58:06 +0100 Subject: [PATCH] ApolloClient: make browser event-replaying logic available in SSR --- .../WrappedApolloClient.tsx | 138 +++++++++--------- 1 file changed, 69 insertions(+), 69 deletions(-) diff --git a/packages/client-react-streaming/src/DataTransportAbstraction/WrappedApolloClient.tsx b/packages/client-react-streaming/src/DataTransportAbstraction/WrappedApolloClient.tsx index f7d822ee..1c47bbf1 100644 --- a/packages/client-react-streaming/src/DataTransportAbstraction/WrappedApolloClient.tsx +++ b/packages/client-react-streaming/src/DataTransportAbstraction/WrappedApolloClient.tsx @@ -59,75 +59,7 @@ class ApolloClientBase extends OrigApolloClient { } } -class ApolloClientSSRImpl extends ApolloClientBase { - constructor(options: ApolloClientOptions) { - super(options); - - getQueryManager(this)[wrappers] = hookWrappers; - } - - watchQueryQueue = createBackpressuredCallback<{ - event: Extract; - observable: Observable>; - }>(); - - watchQuery< - T = any, - TVariables extends OperationVariables = OperationVariables, - >(options: WatchQueryOptions) { - if ( - options.fetchPolicy !== "cache-only" && - options.fetchPolicy !== "standby" - ) { - const observableQuery = super.watchQuery(options); - const queryInfo = observableQuery["queryInfo"] as QueryInfo; - const id = queryInfo.queryId as TransportIdentifier; - - const streamObservable = new Observable< - Exclude - >((subscriber) => { - const { markResult, markError, markReady } = queryInfo; - queryInfo.markResult = function (result: FetchResult) { - subscriber.next({ - type: "data", - id, - result, - }); - return markResult.apply(queryInfo, arguments as any); - }; - queryInfo.markError = function () { - subscriber.next({ - type: "error", - id, - }); - subscriber.complete(); - return markError.apply(queryInfo, arguments as any); - }; - queryInfo.markReady = function () { - subscriber.next({ - type: "complete", - id, - }); - subscriber.complete(); - return markReady.apply(queryInfo, arguments as any); - }; - }); - - this.watchQueryQueue.push({ - event: { - type: "started", - options: options as WatchQueryOptions, - id, - }, - observable: streamObservable, - }); - return observableQuery; - } - return super.watchQuery(options); - } -} - -export class ApolloClientBrowserImpl< +export class ApolloClientClientBaseImpl< TCacheShape, > extends ApolloClientBase { constructor(options: ApolloClientOptions) { @@ -310,6 +242,74 @@ export class ApolloClientBrowserImpl< }; } +class ApolloClientSSRImpl< + TCacheShape, +> extends ApolloClientClientBaseImpl { + watchQueryQueue = createBackpressuredCallback<{ + event: Extract; + observable: Observable>; + }>(); + + watchQuery< + T = any, + TVariables extends OperationVariables = OperationVariables, + >(options: WatchQueryOptions) { + if ( + options.fetchPolicy !== "cache-only" && + options.fetchPolicy !== "standby" + ) { + const observableQuery = super.watchQuery(options); + const queryInfo = observableQuery["queryInfo"] as QueryInfo; + const id = queryInfo.queryId as TransportIdentifier; + + const streamObservable = new Observable< + Exclude + >((subscriber) => { + const { markResult, markError, markReady } = queryInfo; + queryInfo.markResult = function (result: FetchResult) { + subscriber.next({ + type: "data", + id, + result, + }); + return markResult.apply(queryInfo, arguments as any); + }; + queryInfo.markError = function () { + subscriber.next({ + type: "error", + id, + }); + subscriber.complete(); + return markError.apply(queryInfo, arguments as any); + }; + queryInfo.markReady = function () { + subscriber.next({ + type: "complete", + id, + }); + subscriber.complete(); + return markReady.apply(queryInfo, arguments as any); + }; + }); + + this.watchQueryQueue.push({ + event: { + type: "started", + options: options as WatchQueryOptions, + id, + }, + observable: streamObservable, + }); + return observableQuery; + } + return super.watchQuery(options); + } +} + +export class ApolloClientBrowserImpl< + TCacheShape, +> extends ApolloClientClientBaseImpl {} + const ApolloClientImplementation = /*#__PURE__*/ process.env.REACT_ENV === "ssr" ? ApolloClientSSRImpl