apollo-invalidation-policies
Advanced tools
Comparing version
@@ -0,2 +1,6 @@ | ||
1.0.0-beta13 (Dan Reynolds) | ||
- Short-term fix that adds support for running the lib in environments that cannot handle import statements by removing imports from non-public Apollo APIs. | ||
The required imports will be made available in an upcoming version of Apollo Client and we'll switch to that better fix at that time. | ||
1.0.0-beta12 (Dan Reynolds) | ||
@@ -3,0 +7,0 @@ |
@@ -8,7 +8,4 @@ "use strict"; | ||
const client_1 = require("@apollo/client"); | ||
const maybeDeepFreeze_1 = require("@apollo/client/utilities/common/maybeDeepFreeze"); | ||
const getFromAST_1 = require("@apollo/client/utilities/graphql/getFromAST"); | ||
const utilities_1 = require("@apollo/client/utilities"); | ||
const helpers_1 = require("../helpers"); | ||
const storeUtils_1 = require("@apollo/client/utilities/graphql/storeUtils"); | ||
const fragments_1 = require("@apollo/client/utilities/graphql/fragments"); | ||
const types_1 = require("../policies/types"); | ||
@@ -29,11 +26,11 @@ var ReadResultStatus; | ||
getFieldsForQuery(options) { | ||
const operationDefinition = getFromAST_1.getOperationDefinition(options.query); | ||
const fragmentMap = fragments_1.createFragmentMap(getFromAST_1.getFragmentDefinitions(options.query)); | ||
const operationDefinition = utilities_1.getOperationDefinition(options.query); | ||
const fragmentMap = utilities_1.createFragmentMap(utilities_1.getFragmentDefinitions(options.query)); | ||
return operationDefinition.selectionSet.selections.reduce((acc, selection) => { | ||
var _a, _b; | ||
if (storeUtils_1.isField(selection)) { | ||
if (utilities_1.isField(selection)) { | ||
acc.push(selection); | ||
return acc; | ||
} | ||
const selections = (_b = (_a = fragments_1.getFragmentFromSelection(selection, fragmentMap)) === null || _a === void 0 ? void 0 : _a.selectionSet) === null || _b === void 0 ? void 0 : _b.selections; | ||
const selections = (_b = (_a = utilities_1.getFragmentFromSelection(selection, fragmentMap)) === null || _a === void 0 ? void 0 : _a.selectionSet) === null || _b === void 0 ? void 0 : _b.selections; | ||
if (selections) { | ||
@@ -105,3 +102,3 @@ acc.push(...selections); | ||
var _a; | ||
const fieldName = storeUtils_1.resultKeyNameFromField(field); | ||
const fieldName = utilities_1.resultKeyNameFromField(field); | ||
const subResultStatus = this.processReadSubResult(result, fieldName); | ||
@@ -134,3 +131,3 @@ const typename = (_a = entityTypeMap.readEntityById(helpers_1.makeEntityId(dataId, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
}, true); | ||
maybeDeepFreeze_1.maybeDeepFreeze(result); | ||
utilities_1.maybeDeepFreeze(result); | ||
return aggregateResultComplete | ||
@@ -140,3 +137,3 @@ ? ReadResultStatus.Complete | ||
} | ||
maybeDeepFreeze_1.maybeDeepFreeze(result); | ||
utilities_1.maybeDeepFreeze(result); | ||
return this.processReadSubResult(result); | ||
@@ -182,3 +179,3 @@ } | ||
var _a, _b, _c; | ||
const fieldName = storeUtils_1.resultKeyNameFromField(field); | ||
const fieldName = utilities_1.resultKeyNameFromField(field); | ||
const typename = (_a = entityTypeMap.readEntityById(helpers_1.makeEntityId(dataId, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
@@ -185,0 +182,0 @@ if (typename) { |
import { InMemoryCache, Cache, NormalizedCacheObject, Reference, StoreObject } from "@apollo/client"; | ||
import { ReadFieldOptions } from "@apollo/client/cache/core/types/common"; | ||
import { EntityStore } from "@apollo/client/cache/inmemory/entityStore"; | ||
import InvalidationPolicyManager from "../policies/InvalidationPolicyManager"; | ||
@@ -8,3 +6,3 @@ import { EntityTypeMap } from "../entity-store"; | ||
import { CacheResultProcessor } from "./CacheResultProcessor"; | ||
import { InvalidationPolicyEvent } from "../policies/types"; | ||
import { InvalidationPolicyEvent, ReadFieldOptions } from "../policies/types"; | ||
/** | ||
@@ -17,3 +15,3 @@ * Extension of Apollo in-memory cache which adds support for invalidation policies. | ||
protected cacheResultProcessor: CacheResultProcessor; | ||
protected entityStoreRoot: EntityStore.Root; | ||
protected entityStoreRoot: any; | ||
protected isBroadcasting: boolean; | ||
@@ -20,0 +18,0 @@ constructor(config?: InvalidationPolicyCacheConfig); |
@@ -19,6 +19,5 @@ "use strict"; | ||
const client_1 = require("@apollo/client"); | ||
const helpers_1 = require("@apollo/client/cache/inmemory/helpers"); | ||
const InvalidationPolicyManager_1 = __importDefault(require("../policies/InvalidationPolicyManager")); | ||
const entity_store_1 = require("../entity-store"); | ||
const helpers_2 = require("../helpers"); | ||
const helpers_1 = require("../helpers"); | ||
const CacheResultProcessor_1 = require("./CacheResultProcessor"); | ||
@@ -93,7 +92,7 @@ const types_1 = require("../policies/types"); | ||
const { id = "ROOT_QUERY", fields } = options; | ||
if (helpers_2.isQuery(id)) { | ||
if (helpers_1.isQuery(id)) { | ||
Object.keys(fields).forEach((storeFieldName) => { | ||
var _a; | ||
const fieldName = helpers_1.fieldNameFromStoreName(storeFieldName); | ||
const typename = (_a = this.entityTypeMap.readEntityById(helpers_2.makeEntityId(id, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
const typename = (_a = this.entityTypeMap.readEntityById(helpers_1.makeEntityId(id, fieldName))) === null || _a === void 0 ? void 0 : _a.typename; | ||
if (!typename) { | ||
@@ -155,5 +154,5 @@ return; | ||
if (this.invalidationPolicyManager.isPolicyEventActive(types_1.InvalidationPolicyEvent.Evict)) { | ||
const { typename } = (_a = this.entityTypeMap.readEntityById(helpers_2.makeEntityId(id, fieldName))) !== null && _a !== void 0 ? _a : {}; | ||
const { typename } = (_a = this.entityTypeMap.readEntityById(helpers_1.makeEntityId(id, fieldName))) !== null && _a !== void 0 ? _a : {}; | ||
if (typename) { | ||
const storeFieldName = helpers_2.isQuery(id) && fieldName | ||
const storeFieldName = helpers_1.isQuery(id) && fieldName | ||
? this.policies.getStoreFieldName({ | ||
@@ -187,3 +186,3 @@ typename, | ||
const { storeFieldNames, dataId, fieldName, typename } = entity; | ||
if (helpers_2.isQuery(dataId) && storeFieldNames) { | ||
if (helpers_1.isQuery(dataId) && storeFieldNames) { | ||
Object.keys(storeFieldNames.entries).forEach((storeFieldName) => { | ||
@@ -198,3 +197,3 @@ const isExpired = this.invalidationPolicyManager.runReadPolicy({ | ||
if (isExpired) { | ||
expiredEntityIds.push(helpers_2.makeEntityId(dataId, storeFieldName)); | ||
expiredEntityIds.push(helpers_1.makeEntityId(dataId, storeFieldName)); | ||
} | ||
@@ -211,3 +210,3 @@ }); | ||
if (isExpired) { | ||
expiredEntityIds.push(helpers_2.makeEntityId(dataId)); | ||
expiredEntityIds.push(helpers_1.makeEntityId(dataId)); | ||
} | ||
@@ -261,3 +260,3 @@ } | ||
} | ||
const processedResult = helpers_2.maybeDeepClone(result); | ||
const processedResult = helpers_1.maybeDeepClone(result); | ||
const processedResultStatus = this.cacheResultProcessor.processReadResult(processedResult, options); | ||
@@ -283,3 +282,3 @@ if (processedResultStatus === CacheResultProcessor_1.ReadResultStatus.Complete) { | ||
const { result } = cacheDiff; | ||
const processedResult = helpers_2.maybeDeepClone(result); | ||
const processedResult = helpers_1.maybeDeepClone(result); | ||
const processedResultStatus = this.cacheResultProcessor.processReadResult(processedResult, options); | ||
@@ -286,0 +285,0 @@ if (processedResultStatus === CacheResultProcessor_1.ReadResultStatus.Complete) { |
import EntityTypeMap from "./EntityTypeMap"; | ||
import { EntityStore } from "@apollo/client/cache/inmemory/entityStore"; | ||
import { Policies } from "@apollo/client/cache/inmemory/policies"; | ||
interface EntityStoreWatcherConfig { | ||
entityStore: EntityStore; | ||
entityStore: any; | ||
entityTypeMap: EntityTypeMap; | ||
policies: Policies; | ||
policies: any; | ||
} | ||
@@ -9,0 +7,0 @@ /** |
@@ -7,4 +7,3 @@ "use strict"; | ||
const lodash_1 = __importDefault(require("lodash")); | ||
const helpers_1 = require("@apollo/client/cache/inmemory/helpers"); | ||
const helpers_2 = require("../helpers"); | ||
const helpers_1 = require("../helpers"); | ||
/** | ||
@@ -108,6 +107,6 @@ * Map which stores a relationship between entities in the cache and their type | ||
: undefined; | ||
const entityId = helpers_2.makeEntityId(dataId, fieldName); | ||
const entityId = helpers_1.makeEntityId(dataId, fieldName); | ||
const existingTypeMapEntity = this.readEntityById(entityId); | ||
if (existingTypeMapEntity) { | ||
if (helpers_2.isQuery(dataId) && storeFieldName) { | ||
if (helpers_1.isQuery(dataId) && storeFieldName) { | ||
const storeFieldNameEntry = existingTypeMapEntity.storeFieldNames | ||
@@ -129,3 +128,3 @@ .entries[storeFieldName]; | ||
const cacheTime = Date.now(); | ||
if (helpers_2.isQuery(dataId) && storeFieldName) { | ||
if (helpers_1.isQuery(dataId) && storeFieldName) { | ||
newEntity = { | ||
@@ -159,3 +158,3 @@ dataId, | ||
: null; | ||
const entityId = helpers_2.makeEntityId(dataId, fieldName); | ||
const entityId = helpers_1.makeEntityId(dataId, fieldName); | ||
const entity = this.readEntityById(entityId); | ||
@@ -195,6 +194,6 @@ if (!entity) { | ||
: undefined; | ||
const entity = this.entitiesById[helpers_2.makeEntityId(dataId, fieldName)]; | ||
const entity = this.entitiesById[helpers_1.makeEntityId(dataId, fieldName)]; | ||
if (entity) { | ||
const cacheTime = Date.now(); | ||
if (helpers_2.isQuery(dataId) && storeFieldName) { | ||
if (helpers_1.isQuery(dataId) && storeFieldName) { | ||
entity.storeFieldNames.entries[storeFieldName].cacheTime = cacheTime; | ||
@@ -201,0 +200,0 @@ } |
@@ -11,2 +11,4 @@ export declare function isQuery(dataId: string): boolean; | ||
export declare const maybeDeepClone: (obj: any) => any; | ||
export declare var TypeOrFieldNameRegExp: RegExp; | ||
export declare function fieldNameFromStoreName(storeFieldName: string): string; | ||
//# sourceMappingURL=helpers.d.ts.map |
@@ -29,2 +29,8 @@ "use strict"; | ||
exports.maybeDeepClone = (obj) => lodash_1.default.isPlainObject(obj) && Object.isFrozen(obj) ? lodash_1.default.cloneDeep(obj) : obj; | ||
exports.TypeOrFieldNameRegExp = /^[_a-z][_0-9a-z]*/i; | ||
function fieldNameFromStoreName(storeFieldName) { | ||
var match = storeFieldName.match(exports.TypeOrFieldNameRegExp); | ||
return match ? match[0] : storeFieldName; | ||
} | ||
exports.fieldNameFromStoreName = fieldNameFromStoreName; | ||
//# sourceMappingURL=helpers.js.map |
export { InvalidationPolicyCache } from "./cache"; | ||
export { InvalidationPolicyCacheAuditor } from "./audit"; | ||
export { InvalidationPolicies, PolicyActionEntity, PolicyActionFields, RenewalPolicy, } from "./policies/types"; | ||
export { DefaultPolicyAction, InvalidationPolicies, InvalidationPolicy, InvalidationPolicyEvent, PolicyAction, PolicyActionEntity, PolicyActionFields, RenewalPolicy, } from "./policies/types"; | ||
export { InvalidationPolicyCacheConfig } from "./cache/types"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -8,3 +8,4 @@ "use strict"; | ||
var types_1 = require("./policies/types"); | ||
exports.InvalidationPolicyEvent = types_1.InvalidationPolicyEvent; | ||
exports.RenewalPolicy = types_1.RenewalPolicy; | ||
//# sourceMappingURL=index.js.map |
@@ -0,4 +1,14 @@ | ||
import { FieldNode } from 'graphql'; | ||
import { Cache, Reference, StoreObject, StoreValue } from "@apollo/client"; | ||
import { ReadFieldOptions } from "@apollo/client/cache/core/types/common"; | ||
import EntityTypeMap from "../entity-store/EntityTypeMap"; | ||
export interface FieldSpecifier { | ||
typename?: string; | ||
fieldName: string; | ||
field?: FieldNode; | ||
args?: Record<string, any>; | ||
variables?: Record<string, any>; | ||
} | ||
export interface ReadFieldOptions extends FieldSpecifier { | ||
from?: StoreObject | Reference; | ||
} | ||
export declare enum InvalidationPolicyEvent { | ||
@@ -5,0 +15,0 @@ Write = "Write", |
{ | ||
"name": "apollo-invalidation-policies", | ||
"version": "1.0.0-beta12", | ||
"version": "1.0.0-beta13", | ||
"description": "An extension to the InMemoryCache from Apollo for type-based invalidation policies.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
1660
0.61%569300
-0.26%