-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import type { ReactNode } from "react"; | ||
import { SimulatePreloadedQuery } from "./index.cc.js"; | ||
import type { ApolloClient, QueryOptions } from "@apollo/client"; | ||
import React from "react"; | ||
|
||
export function PreloadQuery({ | ||
options, | ||
getClient, | ||
children, | ||
}: { | ||
options: QueryOptions; | ||
getClient: () => ApolloClient<any>; | ||
children: ReactNode; | ||
}) { | ||
const resultPromise = getClient().query({ | ||
...options, | ||
// TODO: create a second Client instance only for `PreloadQuery` calls | ||
// We want to prevent "client" data from leaking into our "RSC" cache, | ||
// as that data should always be strictly separated. | ||
fetchPolicy: "no-cache", | ||
}); | ||
return ( | ||
<SimulatePreloadedQuery options={options} result={resultPromise}> | ||
{children} | ||
</SimulatePreloadedQuery> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"use client"; | ||
|
||
import type { FetchResult, WatchQueryOptions } from "@apollo/client/index.js"; | ||
import { useApolloClient } from "@apollo/client/index.js"; | ||
import type { ApolloClient as WrappedApolloClient } from "./DataTransportAbstraction/WrappedApolloClient.js"; | ||
import type { TransportIdentifier } from "./DataTransportAbstraction/DataTransportAbstraction.js"; | ||
import type { QueryManager } from "@apollo/client/core/QueryManager.js"; | ||
import type { ReactNode } from "react"; | ||
|
||
const handledRequests = new WeakMap<WatchQueryOptions, TransportIdentifier>(); | ||
|
||
export function SimulatePreloadedQuery({ | ||
options, | ||
result, | ||
children, | ||
}: { | ||
options: WatchQueryOptions; | ||
result: Promise<FetchResult>; | ||
children: ReactNode; | ||
}) { | ||
const client = useApolloClient() as WrappedApolloClient<any>; | ||
if (!handledRequests.has(options)) { | ||
const id = | ||
`preloadedQuery:${(client["queryManager"] as QueryManager<any>).generateQueryId()}` as TransportIdentifier; | ||
handledRequests.set(options, id); | ||
client.onQueryStarted!({ | ||
type: "started", | ||
id, | ||
options, | ||
}); | ||
result.then( | ||
(result) => { | ||
client.onQueryProgress!({ | ||
type: "data", | ||
id, | ||
result, | ||
}); | ||
client.onQueryProgress!({ | ||
type: "complete", | ||
id, | ||
}); | ||
}, | ||
() => { | ||
// TODO: | ||
// This will restart the query in SSR **and** in the browser. | ||
// Currently there is no way of transporting the result received in SSR to the browser. | ||
// Layers over layers... | ||
// Maybe instead we should just "fail" the simulated request on the SSR level | ||
// and only have it re-attempt in the browser? | ||
client.onQueryProgress!({ | ||
type: "error", | ||
id, | ||
}); | ||
} | ||
); | ||
} | ||
return children; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { registerApolloClient } from "./registerApolloClient.js"; | ||
export * from "./index.shared.js"; | ||
export { PreloadQuery } from "./PreloadQuery.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters