Skip to content

Commit

Permalink
Drop workarounds to work with both Apollo Client 3.8 and 3.9 (#185)
Browse files Browse the repository at this point in the history
* Drop workarounds to work with both Apollo Client 3.8 and 3.9

* remove PR build from test  matrix

* update lockfile
  • Loading branch information
phryneas authored Feb 1, 2024
1 parent 8c20e5c commit 88dc5b9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 64 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
fail-fast: false
matrix:
version: ["latest", "next", "0.0.0-pr-11345-20231129164802"]
version: ["latest", "next"]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
4 changes: 2 additions & 2 deletions package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apollo/experimental-nextjs-app-support",
"version": "0.7.1",
"version": "0.8.0",
"repository": {
"url": "git+https://github.com/apollographql/apollo-client-nextjs"
},
Expand Down Expand Up @@ -62,7 +62,7 @@
"vitest": "1.2.1"
},
"peerDependencies": {
"@apollo/client": ">=3.8.0-rc || ^3.8.0 || >=3.9.0-alpha || >=3.9.0-beta || >=3.9.0-rc || ^3.9.0",
"@apollo/client": "^3.9.0",
"next": "^13.4.1 || ^14.0.0",
"react": "^18"
},
Expand Down
77 changes: 17 additions & 60 deletions package/src/ssr/NextSSRApolloClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,9 @@ import {
} from "./ApolloRehydrateSymbols";
import invariant from "ts-invariant";

function getQueryManager<TCacheShape>(client: ApolloClient<unknown>): Pick<
QueryManager<any>,
"generateQueryId" | "fetchQuery" | "stopQuery" | "getDocumentInfo"
> & {
fetchCancelFns: QueryManager<TCacheShape>["fetchCancelFns"];
inFlightLinkObservables:
| Map<string, Map<string, Observable<FetchResult>>>
| (import("@wry/trie").Trie<{
observable?: Observable<FetchResult<any>>;
}> & {
remove(...args: any[]): void;
});
} {
function getQueryManager<TCacheShape>(
client: ApolloClient<unknown>
): QueryManager<TCacheShape> {
return client["queryManager"];
}

Expand Down Expand Up @@ -96,32 +86,13 @@ export class NextSSRApolloClient<
const printedServerQuery = print(query);
const queryManager = getQueryManager<TCacheShape>(this);

let hasRunningQuery: boolean;
let byVariables: Map<any, any> | undefined;
if ("lookup" in queryManager["inFlightLinkObservables"]) {
hasRunningQuery = !!queryManager["inFlightLinkObservables"].peek(
if (
!queryManager["inFlightLinkObservables"].peek(
printedServerQuery,
varJson
)?.observable;
} else {
byVariables =
queryManager["inFlightLinkObservables"].get(
printedServerQuery
) || new Map();
queryManager["inFlightLinkObservables"].set(
printedServerQuery,
byVariables
);
hasRunningQuery = byVariables.has(varJson);
}
if (!hasRunningQuery) {
)?.observable
) {
let simulatedStreamingQuery: SimulatedQueryInfo,
// eslint-disable-next-line prefer-const
observable: Observable<FetchResult>,
fetchCancelFn: (reason: unknown) => void;

const cleanup = () => {
Expand All @@ -130,19 +101,10 @@ export class NextSSRApolloClient<
)
queryManager["fetchCancelFns"].delete(cacheKey);

if (byVariables) {
if (byVariables.get(varJson) === observable)
byVariables.delete(varJson);
} else if (
"lookup" in queryManager["inFlightLinkObservables"]
) {
queryManager["inFlightLinkObservables"].remove(
printedServerQuery,
varJson
);
} else {
throw new Error("unexpected shape of QueryManager");
}
queryManager["inFlightLinkObservables"].remove(
printedServerQuery,
varJson
);

if (
this.simulatedStreamingQueries.get(cacheKey) ===
Expand All @@ -160,7 +122,7 @@ export class NextSSRApolloClient<

promise.finally(cleanup);

observable = new Observable<FetchResult>((observer) => {
const observable = new Observable<FetchResult>((observer) => {
promise
.then((result) => {
observer.next(result);
Expand All @@ -170,16 +132,11 @@ export class NextSSRApolloClient<
observer.error(err);
});
});
if (byVariables) {
byVariables.set(varJson, observable);
} else if ("lookup" in queryManager["inFlightLinkObservables"]) {
queryManager["inFlightLinkObservables"].lookup(
printedServerQuery,
varJson
).observable = observable;
} else {
throw new Error("unexpected shape of QueryManager");
}

queryManager["inFlightLinkObservables"].lookup(
printedServerQuery,
varJson
).observable = observable;

queryManager["fetchCancelFns"].set(
cacheKey,
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ __metadata:
typescript: "npm:5.3.3"
vitest: "npm:1.2.1"
peerDependencies:
"@apollo/client": ">=3.8.0-rc || ^3.8.0 || >=3.9.0-alpha || >=3.9.0-beta || >=3.9.0-rc || ^3.9.0"
"@apollo/client": ^3.9.0
next: ^13.4.1 || ^14.0.0
react: ^18
languageName: unknown
Expand Down

0 comments on commit 88dc5b9

Please # to comment.