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

Make it possible to extend ApolloCache<NormalizedCacheObject> in a InMemoryCache compatible way #12040

Closed
Closed
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
3 changes: 2 additions & 1 deletion scripts/memory/tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const assert = require("assert");
const {
$,
ApolloClient,
InMemoryCache,
gql,
Expand Down Expand Up @@ -182,7 +183,7 @@ describe("garbage collection", () => {
});

function register(suffix) {
const reader = cache["storeReader"];
const reader = $(cache)["storeReader"];
registry.register(reader, "StoreReader" + suffix);
registry.register(reader.canon, "ObjectCanon" + suffix);
}
Expand Down
5 changes: 3 additions & 2 deletions src/__tests__/ApolloClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import gql from "graphql-tag";

import {
$,
ApolloClient,
ApolloError,
DefaultOptions,
Expand Down Expand Up @@ -2745,15 +2746,15 @@ describe("ApolloClient", () => {
`,
});

expect((client.cache as any).data.data).toEqual({
expect($(client.cache).data["data"]).toEqual({
ROOT_QUERY: {
__typename: "Query",
a: 1,
},
});

await client.clearStore();
expect((client.cache as any).data.data).toEqual({});
expect($(client.cache).data["data"]).toEqual({});
});
});

Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

exports[`exports of public entry points @apollo/client 1`] = `
Array [
"$",
"ApolloCache",
"ApolloClient",
"ApolloConsumer",
Expand Down Expand Up @@ -73,6 +74,7 @@ Array [

exports[`exports of public entry points @apollo/client/cache 1`] = `
Array [
"$",
"ApolloCache",
"Cache",
"EntityStore",
Expand All @@ -92,6 +94,7 @@ Array [

exports[`exports of public entry points @apollo/client/core 1`] = `
Array [
"$",
"ApolloCache",
"ApolloClient",
"ApolloError",
Expand Down
21 changes: 13 additions & 8 deletions src/__tests__/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import { ApolloLink } from "../link/core";
import {
createFragmentRegistry,
EntityStore,
InMemoryCache,
makeVar,
PossibleTypesMap,
Expand All @@ -45,6 +46,8 @@ import {
} from "../testing";
import { spyOnConsole } from "../testing/internal";
import { waitFor } from "@testing-library/react";
import { $ } from "../cache/inmemory/privates";
import { LayerType } from "../cache/inmemory/entityStore";

describe("client", () => {
it("can be loaded via require", () => {
Expand Down Expand Up @@ -2434,19 +2437,20 @@ describe("client", () => {
});

{
const { data, optimisticData } = client.cache as any;
const { data, optimisticData } = $(client.cache);
expect(optimisticData).not.toBe(data);
expect(optimisticData.parent).toBe(data.stump);
expect(optimisticData.parent.parent).toBe(data);
const layer = optimisticData as unknown as LayerType;
expect(layer.parent).toBe((data as EntityStore.Root).stump);
expect((layer.parent as LayerType).parent).toBe(data);
}

mutatePromise
.then((_) => {
reject(new Error("Returned a result when it should not have."));
})
.catch((_: ApolloError) => {
const { data, optimisticData } = client.cache as any;
expect(optimisticData).toBe(data.stump);
const { data, optimisticData } = $(client.cache);
expect(optimisticData).toBe((data as EntityStore.Root).stump);
resolve();
});
}
Expand Down Expand Up @@ -3525,7 +3529,8 @@ describe("@connection", () => {
const aResults = watch(aQuery);
const bResults = watch(bQuery);

expect(cache["watches"].size).toBe(2);
const watches = $(cache)["watches"];
expect(watches.size).toBe(2);

expect(aResults).toEqual([]);
expect(bResults).toEqual([]);
Expand All @@ -3543,10 +3548,10 @@ describe("@connection", () => {
expect(aResults).toEqual([]);
expect(bResults).toEqual([]);

expect(cache["watches"].size).toBe(0);
expect(watches.size).toBe(0);
const abResults = watch(abQuery);
expect(abResults).toEqual([]);
expect(cache["watches"].size).toBe(1);
expect(watches.size).toBe(1);

await wait();

Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/resultCacheCleaning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { makeExecutableSchema } from "@graphql-tools/schema";
import { ApolloClient, Resolvers, gql } from "../core";
import { InMemoryCache, NormalizedCacheObject } from "../cache";
import { SchemaLink } from "../link/schema";
import { $ } from "../cache/inmemory/privates";

describe("resultCache cleaning", () => {
const fragments = gql`
Expand Down Expand Up @@ -150,7 +151,7 @@ describe("resultCache cleaning", () => {
});

afterEach(() => {
const storeReader = (client.cache as InMemoryCache)["storeReader"];
const { storeReader } = $(client.cache);
expect(storeReader["executeSubSelectedArray"].size).toBeGreaterThan(0);
expect(storeReader["executeSelectionSet"].size).toBeGreaterThan(0);
client.cache.evict({
Expand Down
2 changes: 1 addition & 1 deletion src/cache/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ export abstract class ApolloCache<TSerialized> implements DataProxy {

// Make sure we compute the same (===) fragment query document every
// time we receive the same fragment in readFragment.
private getFragmentDoc = wrap(getFragmentQueryDocument, {
getFragmentDoc = wrap(getFragmentQueryDocument, {
max:
cacheSizes["cache.fragmentQueryDocuments"] ||
defaultCacheSizes["cache.fragmentQueryDocuments"],
Expand Down
39 changes: 27 additions & 12 deletions src/cache/core/types/Cache.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { DataProxy } from "./DataProxy.js";
import type { DataProxy } from "./DataProxy.js";
import type { AllFieldsModifier, Modifiers } from "./common.js";
import type { ApolloCache } from "../cache.js";

export namespace Cache {
// @ts-ignore
const _ = ""; // Make typescript export something
export type WatchCallback<TData = any> = (
diff: Cache.DiffResult<TData>,
lastDiff?: Cache.DiffResult<TData>
) => void;

export interface ReadOptions<TVariables = any, TData = any>
extends DataProxy.Query<TVariables, TData> {
export type ReadOptions<TVariables = any, TData = any> = DataProxy.Query<
TVariables,
TData
> & {
rootId?: string;
previousResult?: any;
optimistic: boolean;
Expand All @@ -22,7 +26,7 @@ export namespace Cache {
* the risk of memory leaks.
*/
canonizeResults?: boolean;
}
};

export interface WriteOptions<TResult = any, TVariables = any>
extends Omit<DataProxy.Query<TVariables, TResult>, "id">,
Expand Down Expand Up @@ -104,12 +108,23 @@ export namespace Cache {
) => any;
}

export import DiffResult = DataProxy.DiffResult;
export import ReadQueryOptions = DataProxy.ReadQueryOptions;
export import ReadFragmentOptions = DataProxy.ReadFragmentOptions;
export import WriteQueryOptions = DataProxy.WriteQueryOptions;
export import WriteFragmentOptions = DataProxy.WriteFragmentOptions;
export import UpdateQueryOptions = DataProxy.UpdateQueryOptions;
export import UpdateFragmentOptions = DataProxy.UpdateFragmentOptions;
export import Fragment = DataProxy.Fragment;
export type DiffResult<T> = DataProxy.DiffResult<T>;
export type ReadQueryOptions<TData, TVariables> = DataProxy.ReadQueryOptions<
TData,
TVariables
>;
export type ReadFragmentOptions<TData, TVariables> =
DataProxy.ReadFragmentOptions<TData, TVariables>;
export type WriteQueryOptions<TData, TVariables> =
DataProxy.WriteQueryOptions<TData, TVariables>;
export type WriteFragmentOptions<TData, TVariables> =
DataProxy.WriteFragmentOptions<TData, TVariables>;
export type UpdateQueryOptions<TData, TVariables> =
DataProxy.UpdateQueryOptions<TData, TVariables>;
export type UpdateFragmentOptions<TData, TVariables> =
DataProxy.UpdateFragmentOptions<TData, TVariables>;
export type Fragment<TVariables, TData> = DataProxy.Fragment<
TVariables,
TData
>;
}
2 changes: 2 additions & 0 deletions src/cache/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type {
WatchFragmentResult,
} from "./core/cache.js";
export { ApolloCache } from "./core/cache.js";
// eslint-disable-next-line @typescript-eslint/consistent-type-exports
export { Cache } from "./core/types/Cache.js";
export type { DataProxy } from "./core/types/DataProxy.js";
export type {
Expand All @@ -31,6 +32,7 @@ export {
} from "./inmemory/helpers.js";

export { InMemoryCache } from "./inmemory/inMemoryCache.js";
export { $ } from "./inmemory/privates.js";

export type { ReactiveVar } from "./inmemory/reactiveVars.js";
export { makeVar, cacheSlot } from "./inmemory/reactiveVars.js";
Expand Down
Loading