Skip to content

Commit

Permalink
Add reset method to print, hook up to InMemoryCache.gc (#11343)
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Nov 7, 2023
1 parent be2029b commit 776631d
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 21 deletions.
5 changes: 3 additions & 2 deletions .api-reports/api-report-core.md
Expand Up @@ -20,7 +20,6 @@ import { InvariantError } from 'ts-invariant';
import { Observable } from 'zen-observable-ts';
import type { Subscription as ObservableSubscription } from 'zen-observable-ts';
import type { Observer } from 'zen-observable-ts';
import { print as print_3 } from 'graphql';
import { resetCaches } from 'graphql-tag';
import type { SelectionSetNode } from 'graphql';
import { setVerbosity as setLogVerbosity } from 'ts-invariant';
Expand Down Expand Up @@ -1614,7 +1613,9 @@ export type PossibleTypesMap = {
};

// @public (undocumented)
const print_2: typeof print_3;
const print_2: ((ast: ASTNode) => string) & {
reset(): void;
};

// @public (undocumented)
interface Printer {
Expand Down
5 changes: 3 additions & 2 deletions .api-reports/api-report-link_batch-http.md
Expand Up @@ -10,7 +10,6 @@ import type { ExecutionResult } from 'graphql';
import type { GraphQLError } from 'graphql';
import { Observable } from 'zen-observable-ts';
import type { Observer } from 'zen-observable-ts';
import { print as print_3 } from 'graphql';

// @public (undocumented)
class ApolloLink {
Expand Down Expand Up @@ -226,7 +225,9 @@ interface Operation {
type Path = ReadonlyArray<string | number>;

// @public (undocumented)
const print_2: typeof print_3;
const print_2: ((ast: ASTNode) => string) & {
reset(): void;
};

// @public (undocumented)
interface Printer {
Expand Down
5 changes: 3 additions & 2 deletions .api-reports/api-report-link_http.md
Expand Up @@ -11,7 +11,6 @@ import type { GraphQLError } from 'graphql';
import { InvariantError } from 'ts-invariant';
import { Observable } from 'zen-observable-ts';
import type { Observer } from 'zen-observable-ts';
import { print as print_3 } from 'graphql';

// @public (undocumented)
class ApolloLink {
Expand Down Expand Up @@ -261,7 +260,9 @@ export function parseAndCheckHttpResponse(operations: Operation | Operation[]):
type Path = ReadonlyArray<string | number>;

// @public (undocumented)
const print_2: typeof print_3;
const print_2: ((ast: ASTNode) => string) & {
reset(): void;
};

// @public (undocumented)
interface Printer {
Expand Down
5 changes: 3 additions & 2 deletions .api-reports/api-report-utilities.md
Expand Up @@ -20,7 +20,6 @@ import { Observable } from 'zen-observable-ts';
import type { Subscription as ObservableSubscription } from 'zen-observable-ts';
import type { Observer } from 'zen-observable-ts';
import type { OperationDefinitionNode } from 'graphql';
import { print as print_3 } from 'graphql';
import type { SelectionNode } from 'graphql';
import type { SelectionSetNode } from 'graphql';
import type { Subscriber } from 'zen-observable-ts';
Expand Down Expand Up @@ -1882,7 +1881,9 @@ type PossibleTypesMap = {
type Primitive = null | undefined | string | number | boolean | symbol | bigint;

// @public (undocumented)
const print_2: typeof print_3;
const print_2: ((ast: ASTNode) => string) & {
reset(): void;
};
export { print_2 as print }

// Warning: (ae-forgotten-export) The symbol "PendingPromise" needs to be exported by the entry point index.d.ts
Expand Down
5 changes: 3 additions & 2 deletions .api-reports/api-report.md
Expand Up @@ -22,7 +22,6 @@ import { InvariantError } from 'ts-invariant';
import { Observable } from 'zen-observable-ts';
import type { Subscription as ObservableSubscription } from 'zen-observable-ts';
import type { Observer } from 'zen-observable-ts';
import { print as print_3 } from 'graphql';
import * as React_2 from 'react';
import { ReactNode } from 'react';
import { resetCaches } from 'graphql-tag';
Expand Down Expand Up @@ -1952,7 +1951,9 @@ export type PossibleTypesMap = {
type Primitive = null | undefined | string | number | boolean | symbol | bigint;

// @public (undocumented)
const print_2: typeof print_3;
const print_2: ((ast: ASTNode) => string) & {
reset(): void;
};

// @public (undocumented)
interface Printer {
Expand Down
5 changes: 5 additions & 0 deletions .changeset/wild-dolphins-jog.md
@@ -0,0 +1,5 @@
---
"@apollo/client": patch
---

Add `reset` method to `print`, hook up to `InMemoryCache.gc`
4 changes: 2 additions & 2 deletions .size-limit.cjs
@@ -1,7 +1,7 @@
const checks = [
{
path: "dist/apollo-client.min.cjs",
limit: "38062",
limit: "38074",
},
{
path: "dist/main.cjs",
Expand All @@ -10,7 +10,7 @@ const checks = [
{
path: "dist/index.js",
import: "{ ApolloClient, InMemoryCache, HttpLink }",
limit: "32113",
limit: "32132",
},
...[
"ApolloProvider",
Expand Down
2 changes: 2 additions & 0 deletions src/cache/inmemory/inMemoryCache.ts
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
@@ -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 776631d

Please sign in to comment.