Skip to content

Commit

Permalink
Add reset method to print, hook up to ApolloCache.gc
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Nov 6, 2023
1 parent be2029b commit 7a850ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-dolphins-jog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Add `reset` method to `print`, hook up to `ApolloCache.gc`
2 changes: 2 additions & 0 deletions src/cache/inmemory/inMemoryCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
isReference,
DocumentTransform,
canonicalStringify,
print,
} from "../../utilities/index.js";
import type { InMemoryCacheConfig, NormalizedCacheObject } from "./types.js";
import { StoreReader } from "./readFromStore.js";
Expand Down Expand Up @@ -293,6 +294,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
resetResultIdentities?: boolean;
}) {
canonicalStringify.reset();
print.reset();
const ids = this.optimisticData.gc();
if (options && !this.txCount) {
if (options.resetResultCache) {
Expand Down
29 changes: 20 additions & 9 deletions src/utilities/graphql/print.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import type { ASTNode } from "graphql";
import { print as origPrint } from "graphql";
import { canUseWeakMap } from "../common/canUse.js";

const printCache = canUseWeakMap ? new WeakMap() : undefined;
export const print: typeof origPrint = (ast) => {
let result;
result = printCache?.get(ast);
let printCache: undefined | WeakMap<ASTNode, string>;
// further TODO: replace with `optimism` with a `WeakCache` once those are available
export const print = Object.assign(
(ast: ASTNode) => {
let result;
result = printCache?.get(ast);

if (!result) {
result = origPrint(ast);
printCache?.set(ast, result);
if (!result) {
result = origPrint(ast);
printCache?.set(ast, result);
}
return result;
},
{
reset() {
printCache = canUseWeakMap ? new WeakMap() : undefined;
},
}
return result;
};
);

print.reset();

0 comments on commit 7a850ac

Please sign in to comment.