Skip to content

Commit

Permalink
Merge pull request #10 from conpagoaus/fix/removal-of-cache-in-cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim-Filimonov authored Mar 10, 2023
2 parents 19238a8 + 0bf92c4 commit 031302d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 40 deletions.
37 changes: 27 additions & 10 deletions lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,25 @@ type GetClientListWithEdgesQuery = {
clients: {
__typename?: "ClientRoot";
edges: {
__typename?: "Client";
id: string;
name: string;
__typename?: "ClientEdge";
cursor: string;
node: {
__typename?: "Client";
id: string;
name: string;
};
}[];
};
};
const TEST_LIST_QUERY_WITH_EDGES = gql`
query GetClientList {
clients {
edges {
id
name
node {
id
name
}
cursor
}
}
}
Expand Down Expand Up @@ -229,8 +236,11 @@ describe("cache addition", () => {
clients: {
edges: [
{
id: "1",
name: "John",
cursor: "1",
node: {
id: "1",
name: "John",
},
},
],
},
Expand All @@ -241,8 +251,11 @@ describe("cache addition", () => {
cache: client.cache,
query: TEST_LIST_QUERY_WITH_EDGES,
data: {
id: "2",
name: "Jane",
node: {
id: "2",
name: "Jane",
},
cursor: "2",
},
selectionName: "clients.edges",
});
Expand Down Expand Up @@ -320,7 +333,10 @@ describe("cache removal", () => {
query: TEST_LIST_QUERY_WITH_EDGES,
data: {
clients: {
edges: testClients,
edges: testClients.map((x) => ({
cursor: x.id,
node: x,
})),
},
},
});
Expand All @@ -331,6 +347,7 @@ describe("cache removal", () => {
data: {
id: testClients[0].id,
},
objectToRemovePath: "node",
selectionName: "clients.edges",
});

Expand Down
25 changes: 21 additions & 4 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import { isArray } from "@apollo/client/cache/inmemory/helpers";
import { OperationDefinitionNode, FieldNode } from "graphql";

import produce, { Draft } from "immer";
import getNestedProperty from "./prop_getter";
import get from "lodash.get";

const getNestedProperty = (obj: any, path: string | undefined) => {
// when path is not provided defalt to object
if (!path) {
return obj;
}
return get(obj, path);
};
interface ICacheData {
[key: string]: unknown;
}
Expand Down Expand Up @@ -67,6 +74,7 @@ type removeCacheArgs<
data: I;
removeNormalized?: boolean | undefined;
selectionName?: string;
objectToRemovePath?: string;
};

function removeArrayCacheElement<
Expand All @@ -79,13 +87,22 @@ function removeArrayCacheElement<
};
const options: removeCacheArgs<T, V, I> = { ...defaultOptions, ...args };

const { data, cache, removeNormalized } = options;
const { data, cache, removeNormalized, objectToRemovePath } = options;

const index = candidate.findIndex((x) => x.id === data.id);
const idToFindPath = objectToRemovePath ? objectToRemovePath + ".id" : "id";

const index = candidate.findIndex(
(x) => getNestedProperty(x, idToFindPath) === data.id
);
if (index > -1) {
const removedEntry = candidate.splice(index, 1)[0];
if (removeNormalized) {
const idToRemove = cache.identify(removedEntry);
const objectToSearchFor = getNestedProperty(
removedEntry,
objectToRemovePath
);

const idToRemove = cache.identify(objectToSearchFor);
cache.evict({ id: idToRemove });
}
}
Expand Down
20 changes: 0 additions & 20 deletions lib/prop_getter.ts

This file was deleted.

48 changes: 45 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
"generate": "graphql-codegen --config codegen.yml"
},
"devDependencies": {
"@apollo/client": "^3.7.1",
"@graphql-codegen/cli": "^2.13.12",
"@graphql-codegen/typed-document-node": "^2.3.7",
"@graphql-codegen/typescript": "^2.8.2",
"@graphql-codegen/typescript-operations": "^2.5.7",
"@graphql-typed-document-node/core": "^3.1.1",
"@apollo/client": "^3.7.1",
"@types/lodash.get": "^4.4.7",
"typescript": "^4.6.4",
"vitest": "^0.25.0"
},
"peerDependencies": {
"@apollo/client": "^3.0.0"
},
"dependencies": {
"immer": "^9.0.16"
"immer": "^9.0.16",
"lodash.get": "^4.4.2"
}
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true
"strict": true,
"esModuleInterop": true
},
"exclude": ["**/*.test.ts", "dist"]
}

0 comments on commit 031302d

Please sign in to comment.