Socket
Socket
Sign inDemoInstall

apollo-cache-inmemory

Package Overview
Dependencies
Maintainers
4
Versions
149
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-cache-inmemory - npm Package Compare versions

Comparing version 1.3.0-beta.14 to 1.3.0-beta.15

lib/queryKeyMaker.d.ts

272

lib/bundle.umd.js

@@ -114,3 +114,3 @@ (function (global, factory) {

var map = this.children || (this.children = new Map);
return map.get(value) || map.set(value, new CacheKeyNode).get(value);
return map.get(value) || map.set(value, new CacheKeyNode()).get(value);
};

@@ -191,11 +191,115 @@ return CacheKeyNode;

};
var CIRCULAR = Object.create(null);
var QueryKeyMaker = (function () {
function QueryKeyMaker(cacheKeyRoot) {
this.cacheKeyRoot = cacheKeyRoot;
this.perQueryKeyMakers = new Map();
}
QueryKeyMaker.prototype.forQuery = function (document) {
if (!this.perQueryKeyMakers.has(document)) {
this.perQueryKeyMakers.set(document, new PerQueryKeyMaker(this.cacheKeyRoot, document));
}
return this.perQueryKeyMakers.get(document);
};
return QueryKeyMaker;
}());
var PerQueryKeyMaker = (function () {
function PerQueryKeyMaker(cacheKeyRoot, query) {
this.cacheKeyRoot = cacheKeyRoot;
this.query = query;
this.cache = new Map;
this.lookupArray = this.cacheMethod(this.lookupArray);
this.lookupObject = this.cacheMethod(this.lookupObject);
this.lookupFragmentSpread = this.cacheMethod(this.lookupFragmentSpread);
}
PerQueryKeyMaker.prototype.cacheMethod = function (method) {
var _this = this;
return function (value) {
if (_this.cache.has(value)) {
var cached = _this.cache.get(value);
if (cached === CIRCULAR) {
throw new Error("QueryKeyMaker cannot handle circular query structures");
}
return cached;
}
_this.cache.set(value, CIRCULAR);
try {
var result = method.call(_this, value);
_this.cache.set(value, result);
return result;
}
catch (e) {
_this.cache.delete(value);
throw e;
}
};
};
PerQueryKeyMaker.prototype.lookupQuery = function (document) {
return this.lookupObject(document);
};
PerQueryKeyMaker.prototype.lookupSelectionSet = function (selectionSet) {
return this.lookupObject(selectionSet);
};
PerQueryKeyMaker.prototype.lookupFragmentSpread = function (fragmentSpread) {
var name = fragmentSpread.name.value;
var fragment = null;
this.query.definitions.some(function (definition) {
if (definition.kind === "FragmentDefinition" &&
definition.name.value === name) {
fragment = definition;
return true;
}
});
return this.lookupObject(__assign({}, fragmentSpread, { fragment: fragment }));
};
PerQueryKeyMaker.prototype.lookupAny = function (value) {
if (Array.isArray(value)) {
return this.lookupArray(value);
}
if (typeof value === "object" && value !== null) {
if (value.kind === "FragmentSpread") {
return this.lookupFragmentSpread(value);
}
return this.lookupObject(value);
}
return value;
};
PerQueryKeyMaker.prototype.lookupArray = function (array) {
var elements = array.map(this.lookupAny, this);
return this.cacheKeyRoot.lookup(Array.prototype, this.cacheKeyRoot.lookupArray(elements));
};
PerQueryKeyMaker.prototype.lookupObject = function (object) {
var _this = this;
var keys = Object.keys(object);
var locIndex = keys.indexOf("loc");
if (locIndex >= 0)
keys.splice(locIndex, 1);
keys.sort();
var values = keys.map(function (key) { return _this.lookupAny(object[key]); });
return this.cacheKeyRoot.lookup(Object.getPrototypeOf(object), this.cacheKeyRoot.lookupArray(keys), this.cacheKeyRoot.lookupArray(values));
};
return PerQueryKeyMaker;
}());
var __assign$1 = (undefined && undefined.__assign) || function () {
__assign$1 = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign$1.apply(this, arguments);
};
var StoreReader = (function () {
function StoreReader(rootCacheKeyNode) {
if (rootCacheKeyNode === void 0) { rootCacheKeyNode = new CacheKeyNode; }
var _this = this;
this.rootCacheKeyNode = rootCacheKeyNode;
function StoreReader(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.addTypename, addTypename = _c === void 0 ? false : _c, _d = _b.cacheKeyRoot, cacheKeyRoot = _d === void 0 ? new CacheKeyNode : _d;
var reader = this;
var executeStoreQuery = reader.executeStoreQuery, executeSelectionSet = reader.executeSelectionSet;
reader.addTypename = addTypename;
reader.cacheKeyRoot = cacheKeyRoot;
reader.keyMaker = new QueryKeyMaker(cacheKeyRoot);
this.executeStoreQuery = wrap(function (options) {
return executeStoreQuery.call(_this, options);
return executeStoreQuery.call(reader, options);
}, {

@@ -205,3 +309,3 @@ makeCacheKey: function (_a) {

if (contextValue.store instanceof DepTrackingCache) {
return reader.rootCacheKeyNode.lookup(query, contextValue.store, JSON.stringify(variableValues));
return reader.cacheKeyRoot.lookup(reader.keyMaker.forQuery(query).lookupQuery(query), contextValue.store, JSON.stringify(variableValues));
}

@@ -211,3 +315,3 @@ }

this.executeSelectionSet = wrap(function (options) {
return executeSelectionSet.call(_this, options);
return executeSelectionSet.call(reader, options);
}, {

@@ -217,3 +321,3 @@ makeCacheKey: function (_a) {

if (execContext.contextValue.store instanceof DepTrackingCache) {
return reader.rootCacheKeyNode.lookup(selectionSet, execContext.contextValue.store, JSON.stringify(execContext.variableValues), rootValue.id);
return reader.cacheKeyRoot.lookup(reader.keyMaker.forQuery(execContext.query).lookupSelectionSet(selectionSet), execContext.contextValue.store, JSON.stringify(execContext.variableValues), rootValue.id);
}

@@ -225,3 +329,3 @@ }

var optsPatch = { returnPartialData: false };
return this.diffQueryAgainstStore(__assign({}, options, optsPatch)).result;
return this.diffQueryAgainstStore(__assign$1({}, options, optsPatch)).result;
};

@@ -273,2 +377,3 @@ StoreReader.prototype.diffQueryAgainstStore = function (_a) {

var execContext = {
query: query,
fragmentMap: fragmentMap,

@@ -283,2 +388,3 @@ contextValue: contextValue,

execContext: execContext,
parentKind: mainDefinition.kind,
});

@@ -288,3 +394,3 @@ };

var _this = this;
var selectionSet = _a.selectionSet, rootValue = _a.rootValue, execContext = _a.execContext;
var selectionSet = _a.selectionSet, rootValue = _a.rootValue, execContext = _a.execContext, parentKind = _a.parentKind;
var fragmentMap = execContext.fragmentMap, contextValue = execContext.contextValue, variables = execContext.variableValues;

@@ -294,2 +400,3 @@ var finalResult = {

};
var didReadTypename = false;
function handleMissing(result) {

@@ -303,3 +410,3 @@ var _a;

}
selectionSet.selections.forEach(function (selection) {
var handleSelection = function (selection) {
var _a;

@@ -311,5 +418,9 @@ if (!apolloUtilities.shouldInclude(selection, variables)) {

var fieldResult = handleMissing(_this.executeField(selection, rootValue, execContext));
var keyName = apolloUtilities.resultKeyNameFromField(selection);
if (keyName === "__typename") {
didReadTypename = true;
}
if (typeof fieldResult !== 'undefined') {
merge(finalResult.result, (_a = {},
_a[apolloUtilities.resultKeyNameFromField(selection)] = fieldResult,
_a[keyName] = fieldResult,
_a));

@@ -336,6 +447,7 @@ }

execContext: execContext,
parentKind: fragment.kind,
});
if (match === 'heuristic' && fragmentExecResult.missing) {
fragmentExecResult = __assign({}, fragmentExecResult, { missing: fragmentExecResult.missing.map(function (info) {
return __assign({}, info, { tolerable: true });
fragmentExecResult = __assign$1({}, fragmentExecResult, { missing: fragmentExecResult.missing.map(function (info) {
return __assign$1({}, info, { tolerable: true });
}) });

@@ -346,3 +458,15 @@ }

}
});
};
selectionSet.selections.forEach(handleSelection);
if (!didReadTypename &&
this.addTypename &&
parentKind !== "OperationDefinition") {
handleSelection({
kind: "Field",
name: {
kind: "Name",
value: "__typename",
},
});
}
return finalResult;

@@ -387,2 +511,3 @@ };

execContext: execContext,
parentKind: field.kind,
}));

@@ -411,2 +536,3 @@ };

execContext: execContext,
parentKind: field.kind,
}));

@@ -477,3 +603,3 @@ });

if (Object.isExtensible && !Object.isExtensible(target)) {
target = __assign({}, target);
target = __assign$1({}, target);
}

@@ -535,4 +661,4 @@ Object.keys(source).forEach(function (sourceKey) {

})();
var __assign$1 = (undefined && undefined.__assign) || function () {
__assign$1 = Object.assign || function(t) {
var __assign$2 = (undefined && undefined.__assign) || function () {
__assign$2 = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {

@@ -545,3 +671,3 @@ s = arguments[i];

};
return __assign$1.apply(this, arguments);
return __assign$2.apply(this, arguments);
};

@@ -563,4 +689,13 @@ var WriteError = (function (_super) {

}
var TYPENAME_FIELD = {
kind: 'Field',
name: {
kind: 'Name',
value: '__typename',
},
};
var StoreWriter = (function () {
function StoreWriter() {
function StoreWriter(_a) {
var _b = (_a === void 0 ? {} : _a).addTypename, addTypename = _b === void 0 ? false : _b;
this.addTypename = addTypename;
}

@@ -595,2 +730,3 @@ StoreWriter.prototype.writeQueryToStore = function (_a) {

},
parentKind: operationDefinition.kind,
});

@@ -604,4 +740,5 @@ }

var _this = this;
var result = _a.result, dataId = _a.dataId, selectionSet = _a.selectionSet, context = _a.context;
var result = _a.result, dataId = _a.dataId, selectionSet = _a.selectionSet, context = _a.context, parentKind = _a.parentKind;
var variables = context.variables, store = context.store, fragmentMap = context.fragmentMap;
var didWriteTypename = false;
selectionSet.selections.forEach(function (selection) {

@@ -614,2 +751,5 @@ if (!apolloUtilities.shouldInclude(selection, variables)) {

var value = result[resultFieldKey];
if (resultFieldKey === "__typename") {
didWriteTypename = true;
}
if (typeof value !== 'undefined') {

@@ -664,2 +804,3 @@ _this.writeFieldToStore({

context: context,
parentKind: fragment.kind,
});

@@ -669,2 +810,13 @@ }

});
if (!didWriteTypename &&
this.addTypename &&
parentKind !== "OperationDefinition" &&
typeof result.__typename === "string") {
this.writeFieldToStore({
dataId: dataId,
value: result.__typename,
field: TYPENAME_FIELD,
context: context,
});
}
return store;

@@ -689,3 +841,3 @@ };

var generatedId = dataId + "." + storeFieldName;
storeValue = this.processArrayValue(value, generatedId, field.selectionSet, context);
storeValue = this.processArrayValue(value, generatedId, field.selectionSet, context, field.kind);
}

@@ -715,2 +867,3 @@ else {

context: context,
parentKind: field.kind,
});

@@ -751,7 +904,7 @@ }

storeObject = store.get(dataId);
if (!storeObject || storeValue !== storeObject[storeFieldName]) {
store.set(dataId, __assign$1({}, storeObject, (_b = {}, _b[storeFieldName] = storeValue, _b)));
if (!storeObject || !apolloUtilities.isEqual(storeValue, storeObject[storeFieldName])) {
store.set(dataId, __assign$2({}, storeObject, (_b = {}, _b[storeFieldName] = storeValue, _b)));
}
};
StoreWriter.prototype.processArrayValue = function (value, generatedId, selectionSet, context) {
StoreWriter.prototype.processArrayValue = function (value, generatedId, selectionSet, context, parentKind) {
var _this = this;

@@ -764,3 +917,3 @@ return value.map(function (item, index) {

if (Array.isArray(item)) {
return _this.processArrayValue(item, itemDataId, selectionSet, context);
return _this.processArrayValue(item, itemDataId, selectionSet, context, parentKind);
}

@@ -781,2 +934,3 @@ var generated = true;

context: context,
parentKind: parentKind,
});

@@ -793,13 +947,26 @@ }

function mergeWithGenerated(generatedKey, realKey, cache) {
if (generatedKey === realKey) {
return false;
}
var generated = cache.get(generatedKey);
var real = cache.get(realKey);
var madeChanges = false;
Object.keys(generated).forEach(function (key) {
var value = generated[key];
var realValue = real[key];
if (apolloUtilities.isIdValue(value) && isGeneratedId(value.id) && apolloUtilities.isIdValue(realValue)) {
mergeWithGenerated(value.id, realValue.id, cache);
if (apolloUtilities.isIdValue(value) &&
isGeneratedId(value.id) &&
apolloUtilities.isIdValue(realValue) &&
!apolloUtilities.isEqual(value, realValue) &&
mergeWithGenerated(value.id, realValue.id, cache)) {
madeChanges = true;
}
cache.delete(generatedKey);
cache.set(realKey, __assign$1({}, generated, real));
});
cache.delete(generatedKey);
var newRealValue = __assign$2({}, generated, real);
if (apolloUtilities.isEqual(newRealValue, real)) {
return madeChanges;
}
cache.set(realKey, newRealValue);
return true;
}

@@ -824,4 +991,4 @@ function isDataProcessed(dataId, field, processedData) {

var __assign$2 = (undefined && undefined.__assign) || function () {
__assign$2 = Object.assign || function(t) {
var __assign$3 = (undefined && undefined.__assign) || function () {
__assign$3 = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {

@@ -834,3 +1001,3 @@ s = arguments[i];

};
return __assign$2.apply(this, arguments);
return __assign$3.apply(this, arguments);
};

@@ -850,3 +1017,3 @@ var RecordingCache = (function () {

RecordingCache.prototype.toObject = function () {
return __assign$2({}, this.data, this.recordedData);
return __assign$3({}, this.data, this.recordedData);
};

@@ -874,3 +1041,3 @@ RecordingCache.prototype.get = function (dataId) {

this.clear();
this.recordedData = __assign$2({}, newData);
this.recordedData = __assign$3({}, newData);
};

@@ -897,4 +1064,4 @@ return RecordingCache;

})();
var __assign$3 = (undefined && undefined.__assign) || function () {
__assign$3 = Object.assign || function(t) {
var __assign$4 = (undefined && undefined.__assign) || function () {
__assign$4 = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {

@@ -907,3 +1074,3 @@ s = arguments[i];

};
return __assign$3.apply(this, arguments);
return __assign$4.apply(this, arguments);
};

@@ -934,5 +1101,5 @@ var defaultConfig = {

_this.typenameDocumentCache = new Map();
_this.rootCacheKeyNode = new CacheKeyNode();
_this.cacheKeyRoot = new CacheKeyNode();
_this.silenceBroadcast = false;
_this.config = __assign$3({}, defaultConfig, config);
_this.config = __assign$4({}, defaultConfig, config);
if (_this.config.customResolvers) {

@@ -948,4 +1115,9 @@ console.warn('customResolvers have been renamed to cacheRedirects. Please update your config as we will be deprecating customResolvers in the next major version.');

_this.data = defaultNormalizedCacheFactory();
_this.storeReader = new StoreReader(_this.rootCacheKeyNode);
_this.storeWriter = new StoreWriter();
_this.storeReader = new StoreReader({
addTypename: _this.config.addTypename,
cacheKeyRoot: _this.cacheKeyRoot,
});
_this.storeWriter = new StoreWriter({
addTypename: _this.config.addTypename,
});
var cache = _this;

@@ -964,3 +1136,3 @@ var maybeBroadcastWatch = cache.maybeBroadcastWatch;

if (cache.data instanceof DepTrackingCache) {
return cache.rootCacheKeyNode.lookup(c.query, JSON.stringify(c.variables));
return cache.cacheKeyRoot.lookup(c.query, JSON.stringify(c.variables));
}

@@ -993,3 +1165,3 @@ }

store: store,
query: this.transformDocument(query.query),
query: query.query,
variables: query.variables,

@@ -1007,3 +1179,3 @@ rootId: query.rootId,

variables: write.variables,
document: this.transformDocument(write.query),
document: write.query,
store: this.data,

@@ -1021,3 +1193,3 @@ dataIdFromObject: this.config.dataIdFromObject,

store: store,
query: this.transformDocument(query.query),
query: query.query,
variables: query.variables,

@@ -1101,3 +1273,3 @@ returnPartialData: query.returnPartialData,

return this.read({
query: this.transformDocument(apolloUtilities.getFragmentQueryDocument(options.fragment, options.fragmentName)),
query: apolloUtilities.getFragmentQueryDocument(options.fragment, options.fragmentName),
variables: options.variables,

@@ -1112,3 +1284,3 @@ rootId: options.id,

result: options.data,
query: this.transformDocument(options.query),
query: options.query,
variables: options.variables,

@@ -1121,3 +1293,3 @@ });

result: options.data,
query: this.transformDocument(apolloUtilities.getFragmentQueryDocument(options.fragment, options.fragmentName)),
query: apolloUtilities.getFragmentQueryDocument(options.fragment, options.fragmentName),
variables: options.variables,

@@ -1124,0 +1296,0 @@ });

2

lib/inMemoryCache.d.ts

@@ -14,3 +14,3 @@ import { DocumentNode } from 'graphql';

private storeWriter;
private rootCacheKeyNode;
private cacheKeyRoot;
private silenceBroadcast;

@@ -17,0 +17,0 @@ constructor(config?: ApolloReducerConfig);

@@ -57,3 +57,3 @@ var __extends = (this && this.__extends) || (function () {

_this.typenameDocumentCache = new Map();
_this.rootCacheKeyNode = new CacheKeyNode();
_this.cacheKeyRoot = new CacheKeyNode();
_this.silenceBroadcast = false;

@@ -71,4 +71,9 @@ _this.config = __assign({}, defaultConfig, config);

_this.data = defaultNormalizedCacheFactory();
_this.storeReader = new StoreReader(_this.rootCacheKeyNode);
_this.storeWriter = new StoreWriter();
_this.storeReader = new StoreReader({
addTypename: _this.config.addTypename,
cacheKeyRoot: _this.cacheKeyRoot,
});
_this.storeWriter = new StoreWriter({
addTypename: _this.config.addTypename,
});
var cache = _this;

@@ -87,3 +92,3 @@ var maybeBroadcastWatch = cache.maybeBroadcastWatch;

if (cache.data instanceof DepTrackingCache) {
return cache.rootCacheKeyNode.lookup(c.query, JSON.stringify(c.variables));
return cache.cacheKeyRoot.lookup(c.query, JSON.stringify(c.variables));
}

@@ -116,3 +121,3 @@ }

store: store,
query: this.transformDocument(query.query),
query: query.query,
variables: query.variables,

@@ -130,3 +135,3 @@ rootId: query.rootId,

variables: write.variables,
document: this.transformDocument(write.query),
document: write.query,
store: this.data,

@@ -144,3 +149,3 @@ dataIdFromObject: this.config.dataIdFromObject,

store: store,
query: this.transformDocument(query.query),
query: query.query,
variables: query.variables,

@@ -224,3 +229,3 @@ returnPartialData: query.returnPartialData,

return this.read({
query: this.transformDocument(getFragmentQueryDocument(options.fragment, options.fragmentName)),
query: getFragmentQueryDocument(options.fragment, options.fragmentName),
variables: options.variables,

@@ -235,3 +240,3 @@ rootId: options.id,

result: options.data,
query: this.transformDocument(options.query),
query: options.query,
variables: options.variables,

@@ -244,3 +249,3 @@ });

result: options.data,
query: this.transformDocument(getFragmentQueryDocument(options.fragment, options.fragmentName)),
query: getFragmentQueryDocument(options.fragment, options.fragmentName),
variables: options.variables,

@@ -247,0 +252,0 @@ });

@@ -11,9 +11,9 @@ export declare type OptimisticWrapperFunction<T = (...args: any[]) => any> = T & {

export { wrap };
export declare class CacheKeyNode {
export declare class CacheKeyNode<KeyType = object> {
private children;
private key;
lookup(...args: any[]): any;
lookupArray(array: any[]): any;
getOrCreate(value: any): CacheKeyNode;
lookup(...args: any[]): KeyType;
lookupArray(array: any[]): KeyType;
getOrCreate(value: any): CacheKeyNode<KeyType>;
}
//# sourceMappingURL=optimism.d.ts.map

@@ -24,3 +24,3 @@ var wrap = require('optimism').wrap;

var map = this.children || (this.children = new Map);
return map.get(value) || map.set(value, new CacheKeyNode).get(value);
return map.get(value) || map.set(value, new CacheKeyNode()).get(value);
};

@@ -27,0 +27,0 @@ return CacheKeyNode;

@@ -19,5 +19,11 @@ import { IdValue } from 'apollo-utilities';

};
declare type StoreReaderOptions = {
addTypename?: boolean;
cacheKeyRoot?: CacheKeyNode;
};
export declare class StoreReader {
private rootCacheKeyNode;
constructor(rootCacheKeyNode?: CacheKeyNode);
private addTypename;
private cacheKeyRoot;
private keyMaker;
constructor({ addTypename, cacheKeyRoot, }?: StoreReaderOptions);
readQueryFromStore<QueryType>(options: ReadQueryOptions): QueryType;

@@ -24,0 +30,0 @@ diffQueryAgainstStore<T>({ store, query, variables, previousResult, returnPartialData, rootId, fragmentMatcherFunction, config, }: DiffQueryAgainstStoreOptions): Cache.DiffResult<T>;

@@ -15,11 +15,13 @@ var __assign = (this && this.__assign) || function () {

import { DepTrackingCache } from './depTrackingCache';
import { QueryKeyMaker } from './queryKeyMaker';
var StoreReader = (function () {
function StoreReader(rootCacheKeyNode) {
if (rootCacheKeyNode === void 0) { rootCacheKeyNode = new CacheKeyNode; }
var _this = this;
this.rootCacheKeyNode = rootCacheKeyNode;
function StoreReader(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.addTypename, addTypename = _c === void 0 ? false : _c, _d = _b.cacheKeyRoot, cacheKeyRoot = _d === void 0 ? new CacheKeyNode : _d;
var reader = this;
var executeStoreQuery = reader.executeStoreQuery, executeSelectionSet = reader.executeSelectionSet;
reader.addTypename = addTypename;
reader.cacheKeyRoot = cacheKeyRoot;
reader.keyMaker = new QueryKeyMaker(cacheKeyRoot);
this.executeStoreQuery = wrap(function (options) {
return executeStoreQuery.call(_this, options);
return executeStoreQuery.call(reader, options);
}, {

@@ -29,3 +31,3 @@ makeCacheKey: function (_a) {

if (contextValue.store instanceof DepTrackingCache) {
return reader.rootCacheKeyNode.lookup(query, contextValue.store, JSON.stringify(variableValues));
return reader.cacheKeyRoot.lookup(reader.keyMaker.forQuery(query).lookupQuery(query), contextValue.store, JSON.stringify(variableValues));
}

@@ -35,3 +37,3 @@ }

this.executeSelectionSet = wrap(function (options) {
return executeSelectionSet.call(_this, options);
return executeSelectionSet.call(reader, options);
}, {

@@ -41,3 +43,3 @@ makeCacheKey: function (_a) {

if (execContext.contextValue.store instanceof DepTrackingCache) {
return reader.rootCacheKeyNode.lookup(selectionSet, execContext.contextValue.store, JSON.stringify(execContext.variableValues), rootValue.id);
return reader.cacheKeyRoot.lookup(reader.keyMaker.forQuery(execContext.query).lookupSelectionSet(selectionSet), execContext.contextValue.store, JSON.stringify(execContext.variableValues), rootValue.id);
}

@@ -96,2 +98,3 @@ }

var execContext = {
query: query,
fragmentMap: fragmentMap,

@@ -106,2 +109,3 @@ contextValue: contextValue,

execContext: execContext,
parentKind: mainDefinition.kind,
});

@@ -111,3 +115,3 @@ };

var _this = this;
var selectionSet = _a.selectionSet, rootValue = _a.rootValue, execContext = _a.execContext;
var selectionSet = _a.selectionSet, rootValue = _a.rootValue, execContext = _a.execContext, parentKind = _a.parentKind;
var fragmentMap = execContext.fragmentMap, contextValue = execContext.contextValue, variables = execContext.variableValues;

@@ -117,2 +121,3 @@ var finalResult = {

};
var didReadTypename = false;
function handleMissing(result) {

@@ -126,3 +131,3 @@ var _a;

}
selectionSet.selections.forEach(function (selection) {
var handleSelection = function (selection) {
var _a;

@@ -134,5 +139,9 @@ if (!shouldInclude(selection, variables)) {

var fieldResult = handleMissing(_this.executeField(selection, rootValue, execContext));
var keyName = resultKeyNameFromField(selection);
if (keyName === "__typename") {
didReadTypename = true;
}
if (typeof fieldResult !== 'undefined') {
merge(finalResult.result, (_a = {},
_a[resultKeyNameFromField(selection)] = fieldResult,
_a[keyName] = fieldResult,
_a));

@@ -159,2 +168,3 @@ }

execContext: execContext,
parentKind: fragment.kind,
});

@@ -169,3 +179,15 @@ if (match === 'heuristic' && fragmentExecResult.missing) {

}
});
};
selectionSet.selections.forEach(handleSelection);
if (!didReadTypename &&
this.addTypename &&
parentKind !== "OperationDefinition") {
handleSelection({
kind: "Field",
name: {
kind: "Name",
value: "__typename",
},
});
}
return finalResult;

@@ -210,2 +232,3 @@ };

execContext: execContext,
parentKind: field.kind,
}));

@@ -234,2 +257,3 @@ };

execContext: execContext,
parentKind: field.kind,
}));

@@ -236,0 +260,0 @@ });

@@ -19,3 +19,8 @@ import { SelectionSetNode, FieldNode, DocumentNode } from 'graphql';

};
declare type StoreWriterOptions = {
addTypename?: boolean;
};
export declare class StoreWriter {
private addTypename;
constructor({ addTypename, }?: StoreWriterOptions);
writeQueryToStore({ query, result, store, variables, dataIdFromObject, fragmentMatcherFunction, }: {

@@ -38,3 +43,3 @@ query: DocumentNode;

}): NormalizedCache;
writeSelectionSetToStore({ result, dataId, selectionSet, context, }: {
writeSelectionSetToStore({ result, dataId, selectionSet, context, parentKind, }: {
dataId: string;

@@ -44,2 +49,3 @@ result: any;

context: WriteContext;
parentKind: string;
}): NormalizedCache;

@@ -49,2 +55,3 @@ private writeFieldToStore;

}
export {};
//# sourceMappingURL=writeToStore.d.ts.map

@@ -26,3 +26,3 @@ var __extends = (this && this.__extends) || (function () {

import { print } from 'graphql/language/printer';
import { assign, createFragmentMap, getDefaultValues, getFragmentDefinitions, getOperationDefinition, isField, isIdValue, isInlineFragment, isProduction, resultKeyNameFromField, shouldInclude, storeKeyNameFromField, toIdValue, } from 'apollo-utilities';
import { assign, createFragmentMap, getDefaultValues, getFragmentDefinitions, getOperationDefinition, isField, isIdValue, isInlineFragment, isProduction, resultKeyNameFromField, shouldInclude, storeKeyNameFromField, toIdValue, isEqual, } from 'apollo-utilities';
import { ObjectCache } from './objectCache';

@@ -46,4 +46,13 @@ import { defaultNormalizedCacheFactory } from './depTrackingCache';

}
var TYPENAME_FIELD = {
kind: 'Field',
name: {
kind: 'Name',
value: '__typename',
},
};
var StoreWriter = (function () {
function StoreWriter() {
function StoreWriter(_a) {
var _b = (_a === void 0 ? {} : _a).addTypename, addTypename = _b === void 0 ? false : _b;
this.addTypename = addTypename;
}

@@ -78,2 +87,3 @@ StoreWriter.prototype.writeQueryToStore = function (_a) {

},
parentKind: operationDefinition.kind,
});

@@ -87,4 +97,5 @@ }

var _this = this;
var result = _a.result, dataId = _a.dataId, selectionSet = _a.selectionSet, context = _a.context;
var result = _a.result, dataId = _a.dataId, selectionSet = _a.selectionSet, context = _a.context, parentKind = _a.parentKind;
var variables = context.variables, store = context.store, fragmentMap = context.fragmentMap;
var didWriteTypename = false;
selectionSet.selections.forEach(function (selection) {

@@ -97,2 +108,5 @@ if (!shouldInclude(selection, variables)) {

var value = result[resultFieldKey];
if (resultFieldKey === "__typename") {
didWriteTypename = true;
}
if (typeof value !== 'undefined') {

@@ -147,2 +161,3 @@ _this.writeFieldToStore({

context: context,
parentKind: fragment.kind,
});

@@ -152,2 +167,13 @@ }

});
if (!didWriteTypename &&
this.addTypename &&
parentKind !== "OperationDefinition" &&
typeof result.__typename === "string") {
this.writeFieldToStore({
dataId: dataId,
value: result.__typename,
field: TYPENAME_FIELD,
context: context,
});
}
return store;

@@ -172,3 +198,3 @@ };

var generatedId = dataId + "." + storeFieldName;
storeValue = this.processArrayValue(value, generatedId, field.selectionSet, context);
storeValue = this.processArrayValue(value, generatedId, field.selectionSet, context, field.kind);
}

@@ -198,2 +224,3 @@ else {

context: context,
parentKind: field.kind,
});

@@ -234,7 +261,7 @@ }

storeObject = store.get(dataId);
if (!storeObject || storeValue !== storeObject[storeFieldName]) {
if (!storeObject || !isEqual(storeValue, storeObject[storeFieldName])) {
store.set(dataId, __assign({}, storeObject, (_b = {}, _b[storeFieldName] = storeValue, _b)));
}
};
StoreWriter.prototype.processArrayValue = function (value, generatedId, selectionSet, context) {
StoreWriter.prototype.processArrayValue = function (value, generatedId, selectionSet, context, parentKind) {
var _this = this;

@@ -247,3 +274,3 @@ return value.map(function (item, index) {

if (Array.isArray(item)) {
return _this.processArrayValue(item, itemDataId, selectionSet, context);
return _this.processArrayValue(item, itemDataId, selectionSet, context, parentKind);
}

@@ -264,2 +291,3 @@ var generated = true;

context: context,
parentKind: parentKind,
});

@@ -277,13 +305,26 @@ }

function mergeWithGenerated(generatedKey, realKey, cache) {
if (generatedKey === realKey) {
return false;
}
var generated = cache.get(generatedKey);
var real = cache.get(realKey);
var madeChanges = false;
Object.keys(generated).forEach(function (key) {
var value = generated[key];
var realValue = real[key];
if (isIdValue(value) && isGeneratedId(value.id) && isIdValue(realValue)) {
mergeWithGenerated(value.id, realValue.id, cache);
if (isIdValue(value) &&
isGeneratedId(value.id) &&
isIdValue(realValue) &&
!isEqual(value, realValue) &&
mergeWithGenerated(value.id, realValue.id, cache)) {
madeChanges = true;
}
cache.delete(generatedKey);
cache.set(realKey, __assign({}, generated, real));
});
cache.delete(generatedKey);
var newRealValue = __assign({}, generated, real);
if (isEqual(newRealValue, real)) {
return madeChanges;
}
cache.set(realKey, newRealValue);
return true;
}

@@ -290,0 +331,0 @@ function isDataProcessed(dataId, field, processedData) {

{
"name": "apollo-cache-inmemory",
"version": "1.3.0-beta.14",
"version": "1.3.0-beta.15",
"description": "Core abstract of Caching layer for Apollo Client",

@@ -5,0 +5,0 @@ "author": "James Baxley <james@meteor.com>",

@@ -52,3 +52,3 @@ import { DocumentNode } from 'graphql';

private storeWriter: StoreWriter;
private rootCacheKeyNode = new CacheKeyNode();
private cacheKeyRoot = new CacheKeyNode();

@@ -81,5 +81,11 @@ // Set this while in a transaction to prevent broadcasts...

this.storeReader = new StoreReader(this.rootCacheKeyNode);
this.storeWriter = new StoreWriter();
this.storeReader = new StoreReader({
addTypename: this.config.addTypename,
cacheKeyRoot: this.cacheKeyRoot,
});
this.storeWriter = new StoreWriter({
addTypename: this.config.addTypename,
});
const cache = this;

@@ -108,3 +114,3 @@ const { maybeBroadcastWatch } = cache;

// using a data store that can track cache dependencies.
return cache.rootCacheKeyNode.lookup(
return cache.cacheKeyRoot.lookup(
c.query,

@@ -143,3 +149,3 @@ JSON.stringify(c.variables),

store,
query: this.transformDocument(query.query),
query: query.query,
variables: query.variables,

@@ -158,3 +164,3 @@ rootId: query.rootId,

variables: write.variables,
document: this.transformDocument(write.query),
document: write.query,
store: this.data,

@@ -175,3 +181,3 @@ dataIdFromObject: this.config.dataIdFromObject,

store: store,
query: this.transformDocument(query.query),
query: query.query,
variables: query.variables,

@@ -291,4 +297,5 @@ returnPartialData: query.returnPartialData,

return this.read({
query: this.transformDocument(
getFragmentQueryDocument(options.fragment, options.fragmentName),
query: getFragmentQueryDocument(
options.fragment,
options.fragmentName,
),

@@ -307,3 +314,3 @@ variables: options.variables,

result: options.data,
query: this.transformDocument(options.query),
query: options.query,
variables: options.variables,

@@ -319,4 +326,5 @@ });

result: options.data,
query: this.transformDocument(
getFragmentQueryDocument(options.fragment, options.fragmentName),
query: getFragmentQueryDocument(
options.fragment,
options.fragmentName,
),

@@ -323,0 +331,0 @@ variables: options.variables,

@@ -26,12 +26,12 @@ declare function require(id: string): any;

export class CacheKeyNode {
private children: Map<any, CacheKeyNode> | null = null;
private key: object | null = null;
export class CacheKeyNode<KeyType = object> {
private children: Map<any, CacheKeyNode<KeyType>> | null = null;
private key: KeyType | null = null;
lookup(...args: any[]) {
lookup(...args: any[]): KeyType {
return this.lookupArray(args);
}
lookupArray(array: any[]) {
let node: CacheKeyNode = this;
lookupArray(array: any[]): KeyType {
let node: CacheKeyNode<KeyType> = this;
array.forEach(value => {

@@ -43,6 +43,6 @@ node = node.getOrCreate(value);

getOrCreate(value: any) {
getOrCreate(value: any): CacheKeyNode<KeyType> {
const map = this.children || (this.children = new Map);
return map.get(value) || map.set(value, new CacheKeyNode).get(value);
return map.get(value) || map.set(value, new CacheKeyNode<KeyType>()).get(value);
}
}

@@ -40,2 +40,3 @@ import {

SelectionSetNode,
SelectionNode,
} from 'graphql';

@@ -47,2 +48,3 @@

import { DepTrackingCache } from './depTrackingCache';
import { QueryKeyMaker } from './queryKeyMaker';

@@ -58,2 +60,3 @@ export type VariableMap = { [name: string]: any };

type ExecContext = {
query: DocumentNode;
fragmentMap: FragmentMap;

@@ -95,8 +98,19 @@ contextValue: ReadStoreContext;

execContext: ExecContext;
parentKind: string;
};
type StoreReaderOptions = {
addTypename?: boolean;
cacheKeyRoot?: CacheKeyNode;
};
export class StoreReader {
constructor(
private rootCacheKeyNode = new CacheKeyNode,
) {
private addTypename: boolean;
private cacheKeyRoot: CacheKeyNode;
private keyMaker: QueryKeyMaker;
constructor({
addTypename = false,
cacheKeyRoot = new CacheKeyNode,
}: StoreReaderOptions = {}) {
const reader = this;

@@ -108,4 +122,8 @@ const {

reader.addTypename = addTypename;
reader.cacheKeyRoot = cacheKeyRoot;
reader.keyMaker = new QueryKeyMaker(cacheKeyRoot);
this.executeStoreQuery = wrap((options: ExecStoreQueryOptions) => {
return executeStoreQuery.call(this, options);
return executeStoreQuery.call(reader, options);
}, {

@@ -122,4 +140,4 @@ makeCacheKey({

if (contextValue.store instanceof DepTrackingCache) {
return reader.rootCacheKeyNode.lookup(
query,
return reader.cacheKeyRoot.lookup(
reader.keyMaker.forQuery(query).lookupQuery(query),
contextValue.store,

@@ -133,3 +151,3 @@ JSON.stringify(variableValues),

this.executeSelectionSet = wrap((options: ExecSelectionSetOptions) => {
return executeSelectionSet.call(this, options);
return executeSelectionSet.call(reader, options);
}, {

@@ -142,4 +160,4 @@ makeCacheKey({

if (execContext.contextValue.store instanceof DepTrackingCache) {
return reader.rootCacheKeyNode.lookup(
selectionSet,
return reader.cacheKeyRoot.lookup(
reader.keyMaker.forQuery(execContext.query).lookupSelectionSet(selectionSet),
execContext.contextValue.store,

@@ -156,2 +174,3 @@ JSON.stringify(execContext.variableValues),

}
/**

@@ -282,2 +301,3 @@ * Resolves the result of a query solely from the store (i.e. never hits the server).

const execContext: ExecContext = {
query,
fragmentMap,

@@ -293,2 +313,3 @@ contextValue,

execContext,
parentKind: mainDefinition.kind,
});

@@ -301,2 +322,3 @@ }

execContext,
parentKind,
}: ExecSelectionSetOptions): ExecResult {

@@ -308,2 +330,4 @@ const { fragmentMap, contextValue, variableValues: variables } = execContext;

let didReadTypename = false;
function handleMissing<T>(result: ExecResult<T>): T {

@@ -317,3 +341,3 @@ if (result.missing) {

selectionSet.selections.forEach(selection => {
const handleSelection = (selection: SelectionNode) => {
if (!shouldInclude(selection, variables)) {

@@ -329,5 +353,10 @@ // Skip this entirely

const keyName = resultKeyNameFromField(selection);
if (keyName === "__typename") {
didReadTypename = true;
}
if (typeof fieldResult !== 'undefined') {
merge(finalResult.result, {
[resultKeyNameFromField(selection)]: fieldResult,
[keyName]: fieldResult,
});

@@ -358,2 +387,3 @@ }

execContext,
parentKind: fragment.kind,
});

@@ -373,4 +403,21 @@

}
});
};
selectionSet.selections.forEach(handleSelection);
if (! didReadTypename &&
this.addTypename &&
// Analogous to the isRoot parameter that addTypenameToDocument passes
// to addTypenameToSelectionSet to avoid adding __typename to the root
// query operation's selection set.
parentKind !== "OperationDefinition") {
handleSelection({
kind: "Field",
name: {
kind: "Name",
value: "__typename",
},
});
}
return finalResult;

@@ -445,2 +492,3 @@ }

execContext,
parentKind: field.kind,
}));

@@ -481,2 +529,3 @@ }

execContext,
parentKind: field.kind,
}));

@@ -483,0 +532,0 @@ });

@@ -28,2 +28,3 @@ import {

toIdValue,
isEqual,
} from 'apollo-utilities';

@@ -64,3 +65,23 @@

type StoreWriterOptions = {
addTypename?: boolean;
};
const TYPENAME_FIELD: FieldNode = {
kind: 'Field',
name: {
kind: 'Name',
value: '__typename',
},
};
export class StoreWriter {
private addTypename: boolean;
constructor({
addTypename = false,
}: StoreWriterOptions = {}) {
this.addTypename = addTypename;
}
/**

@@ -146,2 +167,3 @@ * Writes the result of a query to the store.

},
parentKind: operationDefinition.kind,
});

@@ -158,2 +180,3 @@ } catch (e) {

context,
parentKind,
}: {

@@ -164,4 +187,6 @@ dataId: string;

context: WriteContext;
parentKind: string;
}): NormalizedCache {
const { variables, store, fragmentMap } = context;
let didWriteTypename = false;

@@ -177,2 +202,6 @@ selectionSet.selections.forEach(selection => {

if (resultFieldKey === "__typename") {
didWriteTypename = true;
}
if (typeof value !== 'undefined') {

@@ -253,2 +282,3 @@ this.writeFieldToStore({

context,
parentKind: fragment.kind,
});

@@ -259,2 +289,17 @@ }

if (! didWriteTypename &&
this.addTypename &&
// Analogous to the isRoot parameter that addTypenameToDocument passes
// to addTypenameToSelectionSet to avoid adding __typename to the root
// query operation's selection set.
parentKind !== "OperationDefinition" &&
typeof result.__typename === "string") {
this.writeFieldToStore({
dataId,
value: result.__typename,
field: TYPENAME_FIELD,
context,
});
}
return store;

@@ -298,2 +343,3 @@ }

context,
field.kind,
);

@@ -339,2 +385,3 @@ } else {

context,
parentKind: field.kind,
});

@@ -405,3 +452,3 @@ }

storeObject = store.get(dataId);
if (!storeObject || storeValue !== storeObject[storeFieldName]) {
if (!storeObject || !isEqual(storeValue, storeObject[storeFieldName])) {
store.set(dataId, {

@@ -419,2 +466,3 @@ ...storeObject,

context: WriteContext,
parentKind: string,
): any[] {

@@ -429,3 +477,9 @@ return value.map((item: any, index: any) => {

if (Array.isArray(item)) {
return this.processArrayValue(item, itemDataId, selectionSet, context);
return this.processArrayValue(
item,
itemDataId,
selectionSet,
context,
parentKind,
);
}

@@ -450,2 +504,3 @@

context,
parentKind,
});

@@ -472,5 +527,10 @@ }

cache: NormalizedCache,
) {
): boolean {
if (generatedKey === realKey) {
return false;
}
const generated = cache.get(generatedKey);
const real = cache.get(realKey);
let madeChanges = false;

@@ -480,8 +540,21 @@ Object.keys(generated).forEach(key => {

const realValue = real[key];
if (isIdValue(value) && isGeneratedId(value.id) && isIdValue(realValue)) {
mergeWithGenerated(value.id, realValue.id, cache);
if (isIdValue(value) &&
isGeneratedId(value.id) &&
isIdValue(realValue) &&
! isEqual(value, realValue) &&
mergeWithGenerated(value.id, realValue.id, cache)) {
madeChanges = true;
}
cache.delete(generatedKey);
cache.set(realKey, { ...generated, ...real } as StoreObject);
});
cache.delete(generatedKey);
const newRealValue = { ...generated, ...real };
if (isEqual(newRealValue, real)) {
return madeChanges;
}
cache.set(realKey, newRealValue);
return true;
}

@@ -488,0 +561,0 @@

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc