Skip to content

Commit 0510eab

Browse files
committed
Make it possible to extend ApolloCache<NormalizedCacheObject> in a InMemoryCache compatible way
1 parent 82d8cb4 commit 0510eab

File tree

12 files changed

+94
-57
lines changed

12 files changed

+94
-57
lines changed

src/__tests__/__snapshots__/exports.ts.snap

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Array [
88
"ApolloError",
99
"ApolloLink",
1010
"ApolloProvider",
11-
"Cache",
1211
"DocumentTransform",
1312
"DocumentType",
1413
"HttpLink",
@@ -74,7 +73,6 @@ Array [
7473
exports[`exports of public entry points @apollo/client/cache 1`] = `
7574
Array [
7675
"ApolloCache",
77-
"Cache",
7876
"EntityStore",
7977
"InMemoryCache",
8078
"MissingFieldError",
@@ -96,7 +94,6 @@ Array [
9694
"ApolloClient",
9795
"ApolloError",
9896
"ApolloLink",
99-
"Cache",
10097
"DocumentTransform",
10198
"HttpLink",
10299
"InMemoryCache",

src/cache/core/types/Cache.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { DataProxy } from "./DataProxy.js";
1+
import type { DataProxy } from "./DataProxy.js";
22
import type { AllFieldsModifier, Modifiers } from "./common.js";
33
import type { ApolloCache } from "../cache.js";
44

@@ -8,8 +8,10 @@ export namespace Cache {
88
lastDiff?: Cache.DiffResult<TData>
99
) => void;
1010

11-
export interface ReadOptions<TVariables = any, TData = any>
12-
extends DataProxy.Query<TVariables, TData> {
11+
export type ReadOptions<TVariables = any, TData = any> = DataProxy.Query<
12+
TVariables,
13+
TData
14+
> & {
1315
rootId?: string;
1416
previousResult?: any;
1517
optimistic: boolean;
@@ -22,7 +24,7 @@ export namespace Cache {
2224
* the risk of memory leaks.
2325
*/
2426
canonizeResults?: boolean;
25-
}
27+
};
2628

2729
export interface WriteOptions<TResult = any, TVariables = any>
2830
extends Omit<DataProxy.Query<TVariables, TResult>, "id">,
@@ -104,12 +106,23 @@ export namespace Cache {
104106
) => any;
105107
}
106108

107-
export import DiffResult = DataProxy.DiffResult;
108-
export import ReadQueryOptions = DataProxy.ReadQueryOptions;
109-
export import ReadFragmentOptions = DataProxy.ReadFragmentOptions;
110-
export import WriteQueryOptions = DataProxy.WriteQueryOptions;
111-
export import WriteFragmentOptions = DataProxy.WriteFragmentOptions;
112-
export import UpdateQueryOptions = DataProxy.UpdateQueryOptions;
113-
export import UpdateFragmentOptions = DataProxy.UpdateFragmentOptions;
114-
export import Fragment = DataProxy.Fragment;
109+
export type DiffResult<T> = DataProxy.DiffResult<T>;
110+
export type ReadQueryOptions<TData, TVariables> = DataProxy.ReadQueryOptions<
111+
TData,
112+
TVariables
113+
>;
114+
export type ReadFragmentOptions<TData, TVariables> =
115+
DataProxy.ReadFragmentOptions<TData, TVariables>;
116+
export type WriteQueryOptions<TData, TVariables> =
117+
DataProxy.WriteQueryOptions<TData, TVariables>;
118+
export type WriteFragmentOptions<TData, TVariables> =
119+
DataProxy.WriteFragmentOptions<TData, TVariables>;
120+
export type UpdateQueryOptions<TData, TVariables> =
121+
DataProxy.UpdateQueryOptions<TData, TVariables>;
122+
export type UpdateFragmentOptions<TData, TVariables> =
123+
DataProxy.UpdateFragmentOptions<TData, TVariables>;
124+
export type Fragment<TVariables, TData> = DataProxy.Fragment<
125+
TVariables,
126+
TData
127+
>;
115128
}

src/cache/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type {
66
WatchFragmentResult,
77
} from "./core/cache.js";
88
export { ApolloCache } from "./core/cache.js";
9-
export { Cache } from "./core/types/Cache.js";
9+
export type { Cache } from "./core/types/Cache.js";
1010
export type { DataProxy } from "./core/types/DataProxy.js";
1111
export type {
1212
MissingTree,

src/cache/inmemory/__tests__/optimistic.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import gql from "graphql-tag";
22

33
import { InMemoryCache } from "../inMemoryCache";
4+
import { ApolloCache } from "../../core/cache";
5+
import type { NormalizedCacheObject } from "../types";
46

57
describe("optimistic cache layers", () => {
68
it("return === results for repeated reads", () => {
@@ -28,7 +30,7 @@ describe("optimistic cache layers", () => {
2830
}
2931
`;
3032

31-
function readOptimistic(cache: InMemoryCache) {
33+
function readOptimistic(cache: ApolloCache<NormalizedCacheObject>) {
3234
return cache.readQuery<{ book: any }>({ query }, true);
3335
}
3436

src/cache/inmemory/entityStore.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ import type {
3434
} from "../core/types/common.js";
3535
import type { DocumentNode, FieldNode, SelectionSetNode } from "graphql";
3636

37-
const DELETE: DeleteModifier = Object.create(null);
38-
const delModifier: Modifier<any> = () => DELETE;
39-
const INVALIDATE: InvalidateModifier = Object.create(null);
37+
export const DELETE: DeleteModifier = Object.create(null);
38+
export const delModifier: Modifier<any> = () => DELETE;
39+
export const INVALIDATE: InvalidateModifier = Object.create(null);
4040

4141
export abstract class EntityStore implements NormalizedCache {
4242
protected data: NormalizedCacheObject = Object.create(null);

src/cache/inmemory/inMemoryCache.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,25 @@ import type { OperationVariables } from "../../core/index.js";
3232
import { getInMemoryCacheMemoryInternals } from "../../utilities/caching/getMemoryInternals.js";
3333

3434
type BroadcastOptions = Pick<
35-
Cache.BatchOptions<InMemoryCache>,
35+
Cache.BatchOptions<ApolloCache<NormalizedCacheObject>>,
3636
"optimistic" | "onWatchUpdated"
3737
>;
3838

3939
export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
40-
private data!: EntityStore;
41-
private optimisticData!: EntityStore;
40+
public data!: EntityStore;
41+
public optimisticData!: EntityStore;
4242

43-
protected config: InMemoryCacheConfig;
44-
private watches = new Set<Cache.WatchOptions>();
45-
private addTypename: boolean;
43+
public readonly config: InMemoryCacheConfig;
44+
public readonly watches = new Set<Cache.WatchOptions>();
45+
public readonly addTypename: boolean;
4646

47-
private storeReader!: StoreReader;
48-
private storeWriter!: StoreWriter;
49-
private addTypenameTransform = new DocumentTransform(addTypenameToDocument);
47+
public storeReader!: StoreReader;
48+
public storeWriter!: StoreWriter;
49+
public readonly addTypenameTransform = new DocumentTransform(
50+
addTypenameToDocument
51+
);
5052

51-
private maybeBroadcastWatch!: OptimisticWrapperFunction<
53+
public maybeBroadcastWatch!: OptimisticWrapperFunction<
5254
[Cache.WatchOptions, BroadcastOptions?],
5355
any,
5456
[Cache.WatchOptions]
@@ -80,7 +82,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
8082
this.init();
8183
}
8284

83-
private init() {
85+
init() {
8486
// Passing { resultCaching: false } in the InMemoryCache constructor options
8587
// will completely disable dependency tracking, which will improve memory
8688
// usage but worsen the performance of repeated reads.
@@ -99,7 +101,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
99101
this.resetResultCache();
100102
}
101103

102-
private resetResultCache(resetResultIdentities?: boolean) {
104+
resetResultCache(resetResultIdentities?: boolean) {
103105
const previousReader = this.storeReader;
104106
const { fragments } = this.config;
105107

@@ -411,10 +413,13 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
411413
}
412414
}
413415

414-
private txCount = 0;
416+
public txCount = 0;
415417

416418
public batch<TUpdateResult>(
417-
options: Cache.BatchOptions<InMemoryCache, TUpdateResult>
419+
options: Cache.BatchOptions<
420+
ApolloCache<NormalizedCacheObject>,
421+
TUpdateResult
422+
>
418423
): TUpdateResult {
419424
const {
420425
update,
@@ -515,7 +520,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
515520
}
516521

517522
public performTransaction(
518-
update: (cache: InMemoryCache) => any,
523+
update: (cache: ApolloCache<NormalizedCacheObject>) => any,
519524
optimisticId?: string | null
520525
) {
521526
return this.batch({
@@ -528,18 +533,18 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
528533
return this.addTypenameToDocument(this.addFragmentsToDocument(document));
529534
}
530535

531-
protected broadcastWatches(options?: BroadcastOptions) {
536+
public broadcastWatches(options?: BroadcastOptions) {
532537
if (!this.txCount) {
533538
this.watches.forEach((c) => this.maybeBroadcastWatch(c, options));
534539
}
535540
}
536541

537-
private addFragmentsToDocument(document: DocumentNode) {
542+
addFragmentsToDocument(document: DocumentNode) {
538543
const { fragments } = this.config;
539544
return fragments ? fragments.transform(document) : document;
540545
}
541546

542-
private addTypenameToDocument(document: DocumentNode) {
547+
addTypenameToDocument(document: DocumentNode) {
543548
if (this.addTypename) {
544549
return this.addTypenameTransform.transformDocument(document);
545550
}
@@ -552,7 +557,7 @@ export class InMemoryCache extends ApolloCache<NormalizedCacheObject> {
552557
// simpler to check for changes after recomputing a result but before
553558
// broadcasting it, but this wrapping approach allows us to skip both
554559
// the recomputation and the broadcast, in most cases.
555-
private broadcastWatch(c: Cache.WatchOptions, options?: BroadcastOptions) {
560+
broadcastWatch(c: Cache.WatchOptions, options?: BroadcastOptions) {
556561
const { lastDiff } = c;
557562

558563
// Both WatchOptions and DiffOptions extend ReadOptions, and DiffOptions

src/core/QueryManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,9 +1705,9 @@ interface FetchConcastInfo {
17051705
// Metadata properties that can be returned in addition to the Concast.
17061706
fromLink: boolean;
17071707
}
1708-
interface SourcesAndInfo<TData> extends FetchConcastInfo {
1708+
export interface SourcesAndInfo<TData> extends FetchConcastInfo {
17091709
sources: ConcastSourcesArray<ApolloQueryResult<TData>>;
17101710
}
1711-
interface ConcastAndInfo<TData> extends FetchConcastInfo {
1711+
export interface ConcastAndInfo<TData> extends FetchConcastInfo {
17121712
concast: Concast<ApolloQueryResult<TData>>;
17131713
}

src/core/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export { isApolloError, ApolloError } from "../errors/index.js";
2929
export type {
3030
// All the exports (types) from ../cache, minus cacheSlot,
3131
// which we want to keep semi-private.
32+
Cache,
3233
Transaction,
3334
DataProxy,
3435
InMemoryCacheConfig,
@@ -44,7 +45,6 @@ export type {
4445
WatchFragmentResult,
4546
} from "../cache/index.js";
4647
export {
47-
Cache,
4848
ApolloCache,
4949
InMemoryCache,
5050
MissingFieldError,

src/link/persisted-queries/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ export const createPersistedQueryLink = (
206206
(networkError.result.errors as GraphQLFormattedError[]);
207207
}
208208
if (isNonEmptyArray(networkErrors)) {
209-
graphQLErrors.push(...networkErrors);
209+
graphQLErrors.push(...(networkErrors as GraphQLFormattedError[]));
210210
}
211211

212212
const disablePayload: ErrorResponse = {

src/react/hoc/__tests__/queries/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ describe("queries", () => {
550550
});
551551
const Container = graphql<Vars, Data>(query)(() => null);
552552

553-
let errorCaught = null;
553+
let errorCaught: unknown = null;
554554
try {
555555
const { unmount } = render(
556556
<ApolloProvider client={client}>

0 commit comments

Comments
 (0)