Skip to content

Commit

Permalink
fix bug where non-typed responses are not evicted by query TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
danReynolds committed Aug 6, 2024
1 parent 29add9e commit f48ad9f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
3.3.2 (Dan Reynolds)

Fixes bug where query responses without a typename are never evicted.

Bugfix: https://github.com/NerdWalletOSS/apollo-cache-policies/issues/81
Reporter: https://github.com/qwertypomy

3.3.1 (Dan Reynolds)

Bugfix for aliased queries with variables: https://github.com/NerdWalletOSS/apollo-cache-policies/issues/83
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nerdwallet/apollo-cache-policies",
"version": "3.3.1",
"version": "3.3.2",
"description": "An extension to the InMemoryCache from Apollo that adds additional cache policies.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
20 changes: 13 additions & 7 deletions src/entity-store/EntityStoreWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,24 @@ export default class EntityStoreWatcher {
// If there is a valid response, it will contain the type Query and then the nested response types for each requested field. We want
// to record a map of the types for those fields to their field store names. If there is no incoming data it is because that cache entry for storeFieldName
// is being deleted so do nothing
storeFieldName !== "__typename" &&
(incomingStoreObject[storeFieldName] as StoreObject)?.__typename
storeFieldName !== "__typename"
)
.forEach((storeFieldName) => {
const entityStoreObject = incomingStoreObject[
storeFieldName
] as StoreObject;
entityTypeMap.write(
entityStoreObject.__typename!,
dataId,
storeFieldName
);
const typename = entityStoreObject?.__typename;

if (typename) {
entityTypeMap.write(
typename,
dataId,
storeFieldName
);
} else {
const queryTypename = this.config.policies.rootTypenamesById[dataId];
entityTypeMap.write(queryTypename, dataId, storeFieldName);
}
});
} else {
const typename = incomingStoreObject.__typename;
Expand Down
7 changes: 1 addition & 6 deletions tests/InvalidationPolicyCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1915,15 +1915,10 @@ describe("InvalidationPolicyCache", () => {
},
});

expect(queryResult).toEqual({
bosses: [],
});
expect(queryResult).toEqual({});
expect(cache.extract(true, false)).toEqual({
ROOT_QUERY: {
__typename: "Query",
// The employees field remains in the cache since it's cached value has no __typename field and is not evicted
// itself when read.
"employees({\"name\":\"Tester McBoss\"})": [{ __ref: employee3.toRef() }],
},
});
});
Expand Down

0 comments on commit f48ad9f

Please sign in to comment.