Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reset method to print, hook up to InMemoryCache.gc #11343

Merged
merged 4 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .api-reports/api-report-core.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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 `InMemoryCache.gc`
4 changes: 2 additions & 2 deletions .size-limit.cjs
Original file line number Diff line number Diff line change
@@ -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
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();
Loading