gql-cache-patch
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -0,0 +0,0 @@ # Change Log |
import * as CachePatch from "./cache-patch"; | ||
import * as GraphQLEntityCache from "gql-cache"; | ||
export declare function apply(patches: ReadonlyArray<CachePatch.CachePatch>, cache: GraphQLEntityCache.EntityCache, staleEntities: GraphQLEntityCache.StaleEntities): [GraphQLEntityCache.EntityCache, GraphQLEntityCache.StaleEntities]; | ||
export declare type Mutable<T> = { | ||
-readonly [P in keyof T]: T[P]; | ||
}; |
@@ -25,2 +25,6 @@ "use strict"; | ||
switch (patch.type) { | ||
case "InvalidateEntity": { | ||
applyInvalidateEntity(patch, cacheCopy, staleEntitiesCopy); | ||
break; | ||
} | ||
case "InvalidateField": { | ||
@@ -63,13 +67,75 @@ applyInvalidateField(patch, cacheCopy, staleEntitiesCopy); | ||
exports.apply = apply; | ||
function applyInvalidateEntity(patch, cache, staleEntities) { | ||
var entity = cache[patch.id]; | ||
if (entity !== undefined) { | ||
for (var _i = 0, _a = Object.keys(entity); _i < _a.length; _i++) { | ||
var entityKey = _a[_i]; | ||
console.log(entityKey); | ||
invalidateRecursive(cache, staleEntities, entity[entityKey]); | ||
} | ||
} | ||
} | ||
function applyInvalidateField(patch, cache, staleEntities) { | ||
var _a; | ||
if (entityAndFieldExists(cache, patch)) { | ||
if (cache[patch.id] !== undefined) { | ||
// We want to invalidate all fields that start with the specified | ||
// field name in order to invlidate fields with arguments | ||
// For example the fields "products" and "products(ids: [1, 2])" should | ||
// both be invalidated if the field name "products" is specified | ||
var entityFieldKeys = Object.keys(cache[patch.id]).filter(function (k) { return k.indexOf(patch.fieldName) !== -1; }); | ||
if (entityFieldKeys.length === 0) { | ||
return; | ||
} | ||
for (var _i = 0, entityFieldKeys_1 = entityFieldKeys; _i < entityFieldKeys_1.length; _i++) { | ||
var fieldKey = entityFieldKeys_1[_i]; | ||
staleEntities[patch.id] = __assign({}, staleEntities[patch.id], (_a = {}, _a[fieldKey] = true, _a)); | ||
// Shallow mutation of stale entities OK as we have a shallow copy | ||
staleEntities[patch.id] = __assign({}, staleEntities[patch.id], (_a = {}, _a[fieldKey] = true, _a)); | ||
invalidateRecursive(cache, staleEntities, cache[patch.id][fieldKey]); | ||
} | ||
} | ||
} | ||
function invalidateRecursive(cache, staleEntities, startingEntity) { | ||
if (typeof startingEntity === "number" || | ||
typeof startingEntity === "boolean" || | ||
startingEntity === "undefined" || | ||
startingEntity === "null") { | ||
return; | ||
} | ||
var stack = []; | ||
if (typeof startingEntity === "string" && cache[startingEntity]) { | ||
stack.push(startingEntity); | ||
} | ||
else if (isArrayOfEntityIds(cache, startingEntity)) { | ||
stack.push.apply(stack, startingEntity); | ||
} | ||
while (stack.length > 0) { | ||
var entityId = stack.shift(); | ||
var entity = cache[entityId]; | ||
if (entity === undefined) { | ||
continue; | ||
} | ||
var entityFieldKeys = Object.keys(entity); | ||
var newStaleEntity = __assign({}, staleEntities[entityId]); | ||
for (var _i = 0, entityFieldKeys_2 = entityFieldKeys; _i < entityFieldKeys_2.length; _i++) { | ||
var entityFieldKey = entityFieldKeys_2[_i]; | ||
var entityField = entity[entityFieldKey]; | ||
if (isArrayOfEntityIds(cache, entityField)) { | ||
stack.push.apply(stack, entityField.filter(function (id) { return !staleEntities[id]; })); | ||
} | ||
else if (typeof entityField === "string" && | ||
cache[entityField] && | ||
!staleEntities[entityField]) { | ||
stack.push(entityField); | ||
} | ||
newStaleEntity[entityFieldKey] = true; | ||
} | ||
staleEntities[entityId] = newStaleEntity; | ||
} | ||
} | ||
function isArrayOfEntityIds(cache, field) { | ||
if (Array.isArray(field) && field.some(function (x) { return !!cache[x]; })) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
function applyCreateEntity(patch, cache) { | ||
@@ -76,0 +142,0 @@ // Shallow mutation OK as we have a shallow copy |
import * as GraphQLCache from "gql-cache"; | ||
export declare type CachePatch = InvalidateField | CreateEntity | DeleteEntity | UpdateField | InsertElement | RemoveElement | RemoveEntityElement; | ||
export declare type CachePatch = InvalidateEntity | InvalidateField | CreateEntity | DeleteEntity | UpdateField | InsertElement | RemoveElement | RemoveEntityElement; | ||
export interface InvalidateEntity { | ||
readonly type: "InvalidateEntity"; | ||
readonly id: string; | ||
} | ||
export interface InvalidateField { | ||
@@ -43,2 +47,6 @@ readonly type: "InvalidateField"; | ||
/** | ||
* Makes an entity stale in the cache | ||
*/ | ||
export declare function invalidateEntity(id: GraphQLCache.EntityId): InvalidateEntity; | ||
/** | ||
* Makes a field stale in the cache | ||
@@ -45,0 +53,0 @@ */ |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Makes an entity stale in the cache | ||
*/ | ||
function invalidateEntity(id) { | ||
return { | ||
type: "InvalidateEntity", | ||
id: id | ||
}; | ||
} | ||
exports.invalidateEntity = invalidateEntity; | ||
/** | ||
* Makes a field stale in the cache | ||
@@ -5,0 +15,0 @@ */ |
{ | ||
"name": "gql-cache-patch", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "Declarative patching for gql-cache", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -0,0 +0,0 @@ # gql-cache-patch |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
29327
350
1