Skip to content

Commit

Permalink
experimental ApolloClient.getMemoryInternals helper (#11409)
Browse files Browse the repository at this point in the history
* `print`: use `WeakCache` instead of `WeakMap`

* format

* pull in memory testing tools from PR 11358

* Persisted Query Link: improve memory management

* re-add accidentally removed dependency

* update api

* update size limit

* size-limit

* fix test failure

* better cleanup of interval/timeout

* apply formatting

* remove unneccessary type

* format again after updating prettier

* add central confiuguration for Apollo Client cache sizes

* resolve import cycle

* add exports

* reduce cache collection throttle timeout

* typo in comment

* experimental `ApolloClient.getCacheStatus` helper

* update size-limits

* fix circular import

* size-limits

* update type to remove `WeakKey`

* api-extractor

* work around ES5 class compat

* update api-report

* fix typo in comment

* chores

* changeset

* stop recursion

* add some internal annotations and optional readonly cacheSize property

* slight remodel, add more caches

* chores

* add more caches

* add more caches

* chores

* add type export

* update test

* chores

* formatting

* adjust more tests

* chores

* rename, add more caches

* rename file

* chores

* size-limits

* Update wet-forks-rhyme.md

* PR feedback, long explanatory comment

* Clean up Prettier, Size-limit, and Api-Extractor

* adjust comments

* remove `expect.objectContaining`

* flip conditionals

* extract function

* Clean up Prettier, Size-limit, and Api-Extractor

* report correct size limits

* Clean up Prettier, Size-limit, and Api-Extractor

* adjust report shape to roughly match configuration

* Clean up Prettier, Size-limit, and Api-Extractor

* Update src/utilities/caching/getMemoryInternals.ts

* Clean up Prettier, Size-limit, and Api-Extractor

* better types

* Clean up Prettier, Size-limit, and Api-Extractor

---------

Co-authored-by: phryneas <phryneas@users.noreply.github.com>
  • Loading branch information
phryneas and phryneas committed Dec 15, 2023
1 parent ae7c765 commit 2e7203b
Show file tree
Hide file tree
Showing 39 changed files with 1,502 additions and 53 deletions.
35 changes: 35 additions & 0 deletions .api-reports/api-report-cache.md
Expand Up @@ -30,6 +30,10 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
abstract extract(optimistic?: boolean): TSerialized;
// (undocumented)
gc(): string[];
// Warning: (ae-forgotten-export) The symbol "getApolloCacheMemoryInternals" needs to be exported by the entry point index.d.ts
//
// @internal
getMemoryInternals?: typeof getApolloCacheMemoryInternals;
// (undocumented)
identify(object: StoreObject | Reference): string | undefined;
// (undocumented)
Expand Down Expand Up @@ -474,6 +478,33 @@ export interface FragmentRegistryAPI {
transform<D extends DocumentNode>(document: D): D;
}

// @internal
const getApolloCacheMemoryInternals: (() => {
cache: {
fragmentQueryDocuments: number | undefined;
};
}) | undefined;

// @internal
const getInMemoryCacheMemoryInternals: (() => {
addTypenameDocumentTransform: {
cache: number;
}[];
inMemoryCache: {
executeSelectionSet: number | undefined;
executeSubSelectedArray: number | undefined;
maybeBroadcastWatch: number | undefined;
};
fragmentRegistry: {
findFragmentSpreads: number | undefined;
lookup: number | undefined;
transform: number | undefined;
};
cache: {
fragmentQueryDocuments: number | undefined;
};
}) | undefined;

// @public (undocumented)
export type IdGetter = (value: IdGetterObj) => string | undefined;

Expand Down Expand Up @@ -513,6 +544,10 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
resetResultCache?: boolean;
resetResultIdentities?: boolean;
}): string[];
// Warning: (ae-forgotten-export) The symbol "getInMemoryCacheMemoryInternals" needs to be exported by the entry point index.d.ts
//
// @internal
getMemoryInternals?: typeof getInMemoryCacheMemoryInternals;
// (undocumented)
identify(object: StoreObject | Reference): string | undefined;
// (undocumented)
Expand Down
89 changes: 88 additions & 1 deletion .api-reports/api-report-core.md
Expand Up @@ -46,6 +46,10 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {
abstract extract(optimistic?: boolean): TSerialized;
// (undocumented)
gc(): string[];
// Warning: (ae-forgotten-export) The symbol "getApolloCacheMemoryInternals" needs to be exported by the entry point index.d.ts
//
// @internal
getMemoryInternals?: typeof getApolloCacheMemoryInternals;
// (undocumented)
identify(object: StoreObject | Reference): string | undefined;
// (undocumented)
Expand Down Expand Up @@ -104,6 +108,10 @@ export class ApolloClient<TCacheShape> implements DataProxy {
disableNetworkFetches: boolean;
get documentTransform(): DocumentTransform;
extract(optimistic?: boolean): TCacheShape;
// Warning: (ae-forgotten-export) The symbol "getApolloClientMemoryInternals" needs to be exported by the entry point index.d.ts
//
// @internal
getMemoryInternals?: typeof getApolloClientMemoryInternals;
getObservableQueries(include?: RefetchQueriesInclude): Map<string, ObservableQuery<any>>;
getResolvers(): Resolvers;
// (undocumented)
Expand Down Expand Up @@ -221,10 +229,16 @@ export class ApolloLink {
static execute(link: ApolloLink, operation: GraphQLRequest): Observable<FetchResult>;
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// (undocumented)
Expand Down Expand Up @@ -559,9 +573,16 @@ export class DocumentTransform {
concat(otherTransform: DocumentTransform): DocumentTransform;
// (undocumented)
static identity(): DocumentTransform;
// @internal
readonly left?: DocumentTransform;
resetCache(): void;
// @internal
readonly right?: DocumentTransform;
// (undocumented)
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform;
static split(predicate: (document: DocumentNode) => boolean, left: DocumentTransform, right?: DocumentTransform): DocumentTransform & {
left: DocumentTransform;
right: DocumentTransform;
};
// (undocumented)
transformDocument(document: DocumentNode): DocumentNode;
}
Expand Down Expand Up @@ -860,6 +881,68 @@ export function fromError<T>(errorValue: any): Observable<T>;
// @public (undocumented)
export function fromPromise<T>(promise: Promise<T>): Observable<T>;

// @internal
const getApolloCacheMemoryInternals: (() => {
cache: {
fragmentQueryDocuments: number | undefined;
};
}) | undefined;

// @internal
const getApolloClientMemoryInternals: (() => {
limits: {
[k: string]: number;
};
sizes: {
cache?: {
fragmentQueryDocuments: number | undefined;
} | undefined;
addTypenameDocumentTransform?: {
cache: number;
}[] | undefined;
inMemoryCache?: {
executeSelectionSet: number | undefined;
executeSubSelectedArray: number | undefined;
maybeBroadcastWatch: number | undefined;
} | undefined;
fragmentRegistry?: {
findFragmentSpreads: number | undefined;
lookup: number | undefined;
transform: number | undefined;
} | undefined;
print: number | undefined;
parser: number | undefined;
canonicalStringify: number | undefined;
links: unknown[];
queryManager: {
getDocumentInfo: number;
documentTransforms: {
cache: number;
}[];
};
};
}) | undefined;

// @internal
const getInMemoryCacheMemoryInternals: (() => {
addTypenameDocumentTransform: {
cache: number;
}[];
inMemoryCache: {
executeSelectionSet: number | undefined;
executeSubSelectedArray: number | undefined;
maybeBroadcastWatch: number | undefined;
};
fragmentRegistry: {
findFragmentSpreads: number | undefined;
lookup: number | undefined;
transform: number | undefined;
};
cache: {
fragmentQueryDocuments: number | undefined;
};
}) | undefined;

export { gql }

// @public (undocumented)
Expand Down Expand Up @@ -973,6 +1056,10 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
resetResultCache?: boolean;
resetResultIdentities?: boolean;
}): string[];
// Warning: (ae-forgotten-export) The symbol "getInMemoryCacheMemoryInternals" needs to be exported by the entry point index.d.ts
//
// @internal
getMemoryInternals?: typeof getInMemoryCacheMemoryInternals;
// (undocumented)
identify(object: StoreObject | Reference): string | undefined;
// (undocumented)
Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report-link_batch-http.md
Expand Up @@ -29,12 +29,18 @@ class ApolloLink {
//
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// Warning: (ae-forgotten-export) The symbol "NextLink" needs to be exported by the entry point index.d.ts
//
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// Warning: (ae-forgotten-export) The symbol "Operation" needs to be exported by the entry point index.d.ts
Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report-link_batch.md
Expand Up @@ -28,12 +28,18 @@ class ApolloLink {
//
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// Warning: (ae-forgotten-export) The symbol "NextLink" needs to be exported by the entry point index.d.ts
//
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// Warning: (ae-forgotten-export) The symbol "Operation" needs to be exported by the entry point index.d.ts
Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report-link_context.md
Expand Up @@ -28,12 +28,18 @@ class ApolloLink {
//
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// Warning: (ae-forgotten-export) The symbol "NextLink" needs to be exported by the entry point index.d.ts
//
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// Warning: (ae-forgotten-export) The symbol "Operation" needs to be exported by the entry point index.d.ts
Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report-link_core.md
Expand Up @@ -23,10 +23,16 @@ export class ApolloLink {
static execute(link: ApolloLink, operation: GraphQLRequest): Observable<FetchResult>;
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// (undocumented)
Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report-link_error.md
Expand Up @@ -28,12 +28,18 @@ class ApolloLink {
//
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// Warning: (ae-forgotten-export) The symbol "NextLink" needs to be exported by the entry point index.d.ts
//
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// Warning: (ae-forgotten-export) The symbol "Operation" needs to be exported by the entry point index.d.ts
Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report-link_http.md
Expand Up @@ -30,12 +30,18 @@ class ApolloLink {
//
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// Warning: (ae-forgotten-export) The symbol "NextLink" needs to be exported by the entry point index.d.ts
//
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// Warning: (ae-forgotten-export) The symbol "Operation" needs to be exported by the entry point index.d.ts
Expand Down
16 changes: 15 additions & 1 deletion .api-reports/api-report-link_persisted-queries.md
Expand Up @@ -28,12 +28,18 @@ class ApolloLink {
//
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// Warning: (ae-forgotten-export) The symbol "NextLink" needs to be exported by the entry point index.d.ts
//
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// Warning: (ae-forgotten-export) The symbol "Operation" needs to be exported by the entry point index.d.ts
Expand All @@ -59,7 +65,15 @@ interface BaseOptions {
// @public (undocumented)
export const createPersistedQueryLink: (options: PersistedQueryLink.Options) => ApolloLink & {
resetHashCache: () => void;
};
} & ({
getMemoryInternals(): {
PersistedQueryLink: {
persistedQueryHashes: number;
};
};
} | {
getMemoryInternals?: undefined;
});

// @public (undocumented)
interface DefaultContext extends Record<string, any> {
Expand Down
16 changes: 15 additions & 1 deletion .api-reports/api-report-link_remove-typename.md
Expand Up @@ -28,12 +28,18 @@ class ApolloLink {
//
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// Warning: (ae-forgotten-export) The symbol "NextLink" needs to be exported by the entry point index.d.ts
//
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// Warning: (ae-forgotten-export) The symbol "Operation" needs to be exported by the entry point index.d.ts
Expand Down Expand Up @@ -160,7 +166,15 @@ type Path = ReadonlyArray<string | number>;
// Warning: (ae-forgotten-export) The symbol "ApolloLink" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
export function removeTypenameFromVariables(options?: RemoveTypenameFromVariablesOptions): ApolloLink;
export function removeTypenameFromVariables(options?: RemoveTypenameFromVariablesOptions): ApolloLink & ({
getMemoryInternals(): {
removeTypenameFromVariables: {
getVariableDefinitions: number;
};
};
} | {
getMemoryInternals?: undefined;
});

// @public (undocumented)
export interface RemoveTypenameFromVariablesOptions {
Expand Down
6 changes: 6 additions & 0 deletions .api-reports/api-report-link_retry.md
Expand Up @@ -28,12 +28,18 @@ class ApolloLink {
//
// (undocumented)
static from(links: (ApolloLink | RequestHandler)[]): ApolloLink;
// @internal
getMemoryInternals?: () => unknown;
// @internal
readonly left?: ApolloLink;
// (undocumented)
protected onError(error: any, observer?: Observer<FetchResult>): false | void;
// Warning: (ae-forgotten-export) The symbol "NextLink" needs to be exported by the entry point index.d.ts
//
// (undocumented)
request(operation: Operation, forward?: NextLink): Observable<FetchResult> | null;
// @internal
readonly right?: ApolloLink;
// (undocumented)
setOnError(fn: ApolloLink["onError"]): this;
// Warning: (ae-forgotten-export) The symbol "Operation" needs to be exported by the entry point index.d.ts
Expand Down

0 comments on commit 2e7203b

Please sign in to comment.