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.2 to 1.3.3

168

lib/bundle.umd.js
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('apollo-utilities'), require('graphql/language/printer'), require('apollo-cache')) :
typeof define === 'function' && define.amd ? define(['exports', 'apollo-utilities', 'graphql/language/printer', 'apollo-cache'], factory) :
(factory((global.apollo = global.apollo || {}, global.apollo.cache = global.apollo.cache || {}, global.apollo.cache.inmemory = {}),global.apollo.utilities,global.print,global.apolloCache.core));
}(this, (function (exports,apolloUtilities,printer,apolloCache) { 'use strict';
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('apollo-utilities'), require('graphql/language/visitor'), require('graphql/language/printer'), require('apollo-cache')) :
typeof define === 'function' && define.amd ? define(['exports', 'apollo-utilities', 'graphql/language/visitor', 'graphql/language/printer', 'apollo-cache'], factory) :
(factory((global.apollo = global.apollo || {}, global.apollo.cache = global.apollo.cache || {}, global.apollo.cache.inmemory = {}),global.apollo.utilities,global.visitor,global.print,global.apolloCache.core));
}(this, (function (exports,apolloUtilities,visitor,printer,apolloCache) { 'use strict';

@@ -271,7 +271,3 @@ var haveWarned = false;

var _this = this;
var keys = Object.keys(object);
var locIndex = keys.indexOf("loc");
if (locIndex >= 0)
keys.splice(locIndex, 1);
keys.sort();
var keys = safeSortedKeys(object);
var values = keys.map(function (key) { return _this.lookupAny(object[key]); });

@@ -282,2 +278,28 @@ return this.cacheKeyRoot.lookup(Object.getPrototypeOf(object), this.cacheKeyRoot.lookupArray(keys), this.cacheKeyRoot.lookupArray(values));

}());
var queryKeyMap = Object.create(null);
Object.keys(visitor.QueryDocumentKeys).forEach(function (parentKind) {
var childKeys = queryKeyMap[parentKind] = Object.create(null);
visitor.QueryDocumentKeys[parentKind].forEach(function (childKey) {
childKeys[childKey] = true;
});
if (parentKind === "FragmentSpread") {
childKeys["fragment"] = true;
}
});
function safeSortedKeys(object) {
var keys = Object.keys(object);
var keyCount = keys.length;
var knownKeys = typeof object.kind === "string" && queryKeyMap[object.kind];
var target = 0;
for (var source = target; source < keyCount; ++source) {
var key = keys[source];
var value = object[key];
var isObjectOrArray = value !== null && typeof value === "object";
if (!isObjectOrArray || !knownKeys || knownKeys[key] === true) {
keys[target++] = key;
}
}
keys.length = target;
return keys.sort();
}

@@ -296,11 +318,11 @@ var __assign$1 = (undefined && undefined.__assign) || function () {

var StoreReader = (function () {
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;
function StoreReader(cacheKeyRoot) {
if (cacheKeyRoot === void 0) { cacheKeyRoot = new CacheKeyNode; }
var _this = this;
this.cacheKeyRoot = cacheKeyRoot;
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(reader, options);
return executeStoreQuery.call(_this, options);
}, {

@@ -315,3 +337,3 @@ makeCacheKey: function (_a) {

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

@@ -356,3 +378,3 @@ makeCacheKey: function (_a) {

return;
throw new Error("Can't find field " + info.fieldName + " on object (" + info.objectId + ") " + JSON.stringify(store.get(info.objectId), null, 2) + ".");
throw new Error("Can't find field " + info.fieldName + " on object " + JSON.stringify(info.object, null, 2) + ".");
});

@@ -386,3 +408,2 @@ }

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

@@ -392,3 +413,3 @@ };

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

@@ -398,3 +419,6 @@ var finalResult = {

};
var didReadTypename = false;
var object = contextValue.store.get(rootValue.id);
var typename = (object && object.__typename) ||
(rootValue.id === 'ROOT_QUERY' && 'Query') ||
void 0;
function handleMissing(result) {

@@ -408,3 +432,3 @@ var _a;

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

@@ -415,10 +439,6 @@ if (!apolloUtilities.shouldInclude(selection, variables)) {

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

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

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

@@ -456,18 +475,6 @@ 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;
};
StoreReader.prototype.executeField = function (field, rootValue, execContext) {
StoreReader.prototype.executeField = function (object, typename, field, execContext) {
var variables = execContext.variableValues, contextValue = execContext.contextValue;

@@ -480,3 +487,3 @@ var fieldName = field.name.value;

};
var readStoreResult = readStoreResolver(fieldName, rootValue, args, contextValue, info);
var readStoreResult = readStoreResolver(object, typename, fieldName, args, contextValue, info);
if (!field.selectionSet) {

@@ -510,3 +517,2 @@ return readStoreResult;

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

@@ -535,3 +541,2 @@ };

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

@@ -551,7 +556,4 @@ });

}
function readStoreResolver(fieldName, idValue, args, context, _a) {
function readStoreResolver(object, typename, fieldName, args, context, _a) {
var resultKey = _a.resultKey, directives = _a.directives;
assertIdValue(idValue);
var objId = idValue.id;
var obj = context.store.get(objId);
var storeKeyName = fieldName;

@@ -562,8 +564,7 @@ if (args || directives) {

var fieldValue = void 0;
if (obj) {
fieldValue = obj[storeKeyName];
if (object) {
fieldValue = object[storeKeyName];
if (typeof fieldValue === 'undefined' &&
context.cacheRedirects &&
(obj.__typename || objId === 'ROOT_QUERY')) {
var typename = obj.__typename || 'Query';
typeof typename === 'string') {
var type = context.cacheRedirects[typename];

@@ -573,3 +574,3 @@ if (type) {

if (resolver) {
fieldValue = resolver(obj, args, {
fieldValue = resolver(object, args, {
getCacheKey: function (storeObj) {

@@ -590,3 +591,3 @@ return apolloUtilities.toIdValue({

missing: [{
objectId: objId,
object: object,
fieldName: storeKeyName,

@@ -692,13 +693,4 @@ tolerable: false,

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

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

},
parentKind: operationDefinition.kind,
});

@@ -743,5 +734,4 @@ }

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

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

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

@@ -807,3 +794,2 @@ _this.writeFieldToStore({

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

@@ -813,13 +799,2 @@ }

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

@@ -844,3 +819,3 @@ };

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

@@ -870,3 +845,2 @@ else {

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

@@ -911,3 +885,3 @@ }

};
StoreWriter.prototype.processArrayValue = function (value, generatedId, selectionSet, context, parentKind) {
StoreWriter.prototype.processArrayValue = function (value, generatedId, selectionSet, context) {
var _this = this;

@@ -920,3 +894,3 @@ return value.map(function (item, index) {

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

@@ -937,3 +911,2 @@ var generated = true;

context: context,
parentKind: parentKind,
});

@@ -1110,9 +1083,4 @@ }

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

@@ -1159,3 +1127,3 @@ var maybeBroadcastWatch = cache.maybeBroadcastWatch;

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

@@ -1173,3 +1141,3 @@ rootId: query.rootId,

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

@@ -1187,3 +1155,3 @@ dataIdFromObject: this.config.dataIdFromObject,

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

@@ -1267,3 +1235,3 @@ returnPartialData: query.returnPartialData,

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

@@ -1278,3 +1246,3 @@ rootId: options.id,

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

@@ -1287,3 +1255,3 @@ });

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

@@ -1290,0 +1258,0 @@ });

@@ -70,9 +70,4 @@ var __extends = (this && this.__extends) || (function () {

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

@@ -119,3 +114,3 @@ var maybeBroadcastWatch = cache.maybeBroadcastWatch;

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

@@ -133,3 +128,3 @@ rootId: query.rootId,

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

@@ -147,3 +142,3 @@ dataIdFromObject: this.config.dataIdFromObject,

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

@@ -227,3 +222,3 @@ returnPartialData: query.returnPartialData,

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

@@ -238,3 +233,3 @@ rootId: options.id,

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

@@ -247,3 +242,3 @@ });

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

@@ -250,0 +245,0 @@ });

@@ -12,2 +12,3 @@ var __assign = (this && this.__assign) || function () {

};
import { QueryDocumentKeys } from "graphql/language/visitor";
var CIRCULAR = Object.create(null);

@@ -95,7 +96,3 @@ var QueryKeyMaker = (function () {

var _this = this;
var keys = Object.keys(object);
var locIndex = keys.indexOf("loc");
if (locIndex >= 0)
keys.splice(locIndex, 1);
keys.sort();
var keys = safeSortedKeys(object);
var values = keys.map(function (key) { return _this.lookupAny(object[key]); });

@@ -106,2 +103,28 @@ return this.cacheKeyRoot.lookup(Object.getPrototypeOf(object), this.cacheKeyRoot.lookupArray(keys), this.cacheKeyRoot.lookupArray(values));

}());
var queryKeyMap = Object.create(null);
Object.keys(QueryDocumentKeys).forEach(function (parentKind) {
var childKeys = queryKeyMap[parentKind] = Object.create(null);
QueryDocumentKeys[parentKind].forEach(function (childKey) {
childKeys[childKey] = true;
});
if (parentKind === "FragmentSpread") {
childKeys["fragment"] = true;
}
});
function safeSortedKeys(object) {
var keys = Object.keys(object);
var keyCount = keys.length;
var knownKeys = typeof object.kind === "string" && queryKeyMap[object.kind];
var target = 0;
for (var source = target; source < keyCount; ++source) {
var key = keys[source];
var value = object[key];
var isObjectOrArray = value !== null && typeof value === "object";
if (!isObjectOrArray || !knownKeys || knownKeys[key] === true) {
keys[target++] = key;
}
}
keys.length = target;
return keys.sort();
}
//# sourceMappingURL=queryKeyMaker.js.map
import { IdValue } from 'apollo-utilities';
import { Cache } from 'apollo-cache';
import { ReadStoreContext, DiffQueryAgainstStoreOptions, ReadQueryOptions } from './types';
import { ReadStoreContext, DiffQueryAgainstStoreOptions, ReadQueryOptions, StoreObject } from './types';
import { CacheKeyNode } from './optimism';

@@ -11,3 +11,3 @@ export { OptimisticWrapperFunction } from './optimism';

export declare type ExecResultMissingField = {
objectId: string;
object: StoreObject;
fieldName: string;

@@ -20,11 +20,6 @@ tolerable: boolean;

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

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

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

var StoreReader = (function () {
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;
function StoreReader(cacheKeyRoot) {
if (cacheKeyRoot === void 0) { cacheKeyRoot = new CacheKeyNode; }
var _this = this;
this.cacheKeyRoot = cacheKeyRoot;
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(reader, options);
return executeStoreQuery.call(_this, options);
}, {

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

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

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

return;
throw new Error("Can't find field " + info.fieldName + " on object (" + info.objectId + ") " + JSON.stringify(store.get(info.objectId), null, 2) + ".");
throw new Error("Can't find field " + info.fieldName + " on object " + JSON.stringify(info.object, null, 2) + ".");
});

@@ -107,3 +107,2 @@ }

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

@@ -113,3 +112,3 @@ };

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

@@ -119,3 +118,6 @@ var finalResult = {

};
var didReadTypename = false;
var object = contextValue.store.get(rootValue.id);
var typename = (object && object.__typename) ||
(rootValue.id === 'ROOT_QUERY' && 'Query') ||
void 0;
function handleMissing(result) {

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

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

@@ -136,10 +138,6 @@ if (!shouldInclude(selection, variables)) {

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

@@ -166,3 +164,2 @@ }

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

@@ -177,18 +174,6 @@ 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;
};
StoreReader.prototype.executeField = function (field, rootValue, execContext) {
StoreReader.prototype.executeField = function (object, typename, field, execContext) {
var variables = execContext.variableValues, contextValue = execContext.contextValue;

@@ -201,3 +186,3 @@ var fieldName = field.name.value;

};
var readStoreResult = readStoreResolver(fieldName, rootValue, args, contextValue, info);
var readStoreResult = readStoreResolver(object, typename, fieldName, args, contextValue, info);
if (!field.selectionSet) {

@@ -231,3 +216,2 @@ return readStoreResult;

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

@@ -256,3 +240,2 @@ };

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

@@ -273,7 +256,4 @@ });

}
function readStoreResolver(fieldName, idValue, args, context, _a) {
function readStoreResolver(object, typename, fieldName, args, context, _a) {
var resultKey = _a.resultKey, directives = _a.directives;
assertIdValue(idValue);
var objId = idValue.id;
var obj = context.store.get(objId);
var storeKeyName = fieldName;

@@ -284,8 +264,7 @@ if (args || directives) {

var fieldValue = void 0;
if (obj) {
fieldValue = obj[storeKeyName];
if (object) {
fieldValue = object[storeKeyName];
if (typeof fieldValue === 'undefined' &&
context.cacheRedirects &&
(obj.__typename || objId === 'ROOT_QUERY')) {
var typename = obj.__typename || 'Query';
typeof typename === 'string') {
var type = context.cacheRedirects[typename];

@@ -295,3 +274,3 @@ if (type) {

if (resolver) {
fieldValue = resolver(obj, args, {
fieldValue = resolver(object, args, {
getCacheKey: function (storeObj) {

@@ -312,3 +291,3 @@ return toIdValue({

missing: [{
objectId: objId,
object: object,
fieldName: storeKeyName,

@@ -315,0 +294,0 @@ tolerable: false,

@@ -19,8 +19,3 @@ 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, }: {

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

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

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

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

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

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

@@ -45,13 +45,4 @@ var __extends = (this && this.__extends) || (function () {

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

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

},
parentKind: operationDefinition.kind,
});

@@ -96,5 +86,4 @@ }

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

@@ -107,5 +96,2 @@ if (!shouldInclude(selection, variables)) {

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

@@ -160,3 +146,2 @@ _this.writeFieldToStore({

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

@@ -166,13 +151,2 @@ }

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

@@ -197,3 +171,3 @@ };

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

@@ -223,3 +197,2 @@ else {

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

@@ -264,3 +237,3 @@ }

};
StoreWriter.prototype.processArrayValue = function (value, generatedId, selectionSet, context, parentKind) {
StoreWriter.prototype.processArrayValue = function (value, generatedId, selectionSet, context) {
var _this = this;

@@ -273,3 +246,3 @@ return value.map(function (item, index) {

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

@@ -290,3 +263,2 @@ var generated = true;

context: context,
parentKind: parentKind,
});

@@ -293,0 +265,0 @@ }

{
"name": "apollo-cache-inmemory",
"version": "1.3.2",
"version": "1.3.3",
"description": "Core abstract of Caching layer for Apollo Client",

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

@@ -80,11 +80,5 @@ import { DocumentNode } from 'graphql';

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

@@ -147,3 +141,3 @@ const { maybeBroadcastWatch } = cache;

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

@@ -162,3 +156,3 @@ rootId: query.rootId,

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

@@ -179,3 +173,3 @@ dataIdFromObject: this.config.dataIdFromObject,

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

@@ -295,5 +289,4 @@ returnPartialData: query.returnPartialData,

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

@@ -312,3 +305,3 @@ variables: options.variables,

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

@@ -324,5 +317,4 @@ });

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

@@ -329,0 +321,0 @@ variables: options.variables,

import { CacheKeyNode } from "./optimism";
import { DocumentNode, SelectionSetNode, FragmentSpreadNode, FragmentDefinitionNode } from "graphql";
import { QueryDocumentKeys } from "graphql/language/visitor";

@@ -107,6 +108,3 @@ const CIRCULAR = Object.create(null);

private lookupObject(object: { [key: string]: any }): object {
const keys = Object.keys(object);
const locIndex = keys.indexOf("loc");
if (locIndex >= 0) keys.splice(locIndex, 1);
keys.sort();
const keys = safeSortedKeys(object);
const values = keys.map(key => this.lookupAny(object[key]));

@@ -120,1 +118,41 @@ return this.cacheKeyRoot.lookup(

}
const queryKeyMap: {
[key: string]: { [key: string]: boolean }
} = Object.create(null);
Object.keys(QueryDocumentKeys).forEach(parentKind => {
const childKeys = queryKeyMap[parentKind] = Object.create(null);
(QueryDocumentKeys as {
[key: string]: any[]
})[parentKind].forEach(childKey => {
childKeys[childKey] = true;
});
if (parentKind === "FragmentSpread") {
// A custom key that we include when looking up FragmentSpread nodes.
childKeys["fragment"] = true;
}
});
function safeSortedKeys(object: { [key: string]: any }): string[] {
const keys = Object.keys(object);
const keyCount = keys.length;
const knownKeys = typeof object.kind === "string" && queryKeyMap[object.kind];
// Remove unknown object-valued keys from the array, but leave keys with
// non-object values untouched.
let target = 0;
for (let source = target; source < keyCount; ++source) {
const key = keys[source];
const value = object[key];
const isObjectOrArray = value !== null && typeof value === "object";
if (! isObjectOrArray || ! knownKeys || knownKeys[key] === true) {
keys[target++] = key;
}
}
keys.length = target;
return keys.sort();
}

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

SelectionSetNode,
SelectionNode,
} from 'graphql';

@@ -72,3 +71,3 @@

export type ExecResultMissingField = {
objectId: string;
object: StoreObject;
fieldName: string;

@@ -97,19 +96,10 @@ tolerable: boolean;

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

@@ -121,8 +111,6 @@ const {

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

@@ -152,3 +140,3 @@ makeCacheKey({

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

@@ -249,5 +237,7 @@ makeCacheKey({

throw new Error(
`Can't find field ${info.fieldName} on object (${info.objectId}) ${
JSON.stringify(store.get(info.objectId), null, 2)
}.`
`Can't find field ${info.fieldName} on object ${JSON.stringify(
info.object,
null,
2,
)}.`,
);

@@ -310,3 +300,2 @@ });

execContext,
parentKind: mainDefinition.kind,
});

@@ -319,3 +308,2 @@ }

execContext,
parentKind,
}: ExecSelectionSetOptions): ExecResult {

@@ -327,4 +315,9 @@ const { fragmentMap, contextValue, variableValues: variables } = execContext;

let didReadTypename = false;
const object: StoreObject = contextValue.store.get(rootValue.id);
const typename =
(object && object.__typename) ||
(rootValue.id === 'ROOT_QUERY' && 'Query') ||
void 0;
function handleMissing<T>(result: ExecResult<T>): T {

@@ -338,3 +331,3 @@ if (result.missing) {

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

@@ -347,13 +340,8 @@ // Skip this entirely

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

@@ -384,3 +372,2 @@ }

execContext,
parentKind: fragment.kind,
});

@@ -400,21 +387,4 @@

}
};
});
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;

@@ -424,4 +394,5 @@ }

private executeField(
object: StoreObject,
typename: string | void,
field: FieldNode,
rootValue: any,
execContext: ExecContext,

@@ -439,4 +410,5 @@ ): ExecResult {

const readStoreResult = readStoreResolver(
object,
typename,
fieldName,
rootValue,
args,

@@ -491,3 +463,2 @@ contextValue,

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

@@ -528,3 +499,2 @@ }

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

@@ -550,4 +520,5 @@ });

function readStoreResolver(
object: StoreObject,
typename: string | void,
fieldName: string,
idValue: IdValue,
args: any,

@@ -557,7 +528,2 @@ context: ReadStoreContext,

): ExecResult<StoreValue> {
assertIdValue(idValue);
const objId = idValue.id;
const obj = context.store.get(objId);
let storeKeyName = fieldName;

@@ -574,4 +540,4 @@ if (args || directives) {

if (obj) {
fieldValue = obj[storeKeyName];
if (object) {
fieldValue = object[storeKeyName];

@@ -581,6 +547,4 @@ if (

context.cacheRedirects &&
(obj.__typename || objId === 'ROOT_QUERY')
typeof typename === 'string'
) {
const typename = obj.__typename || 'Query';
// Look for the type in the custom resolver map

@@ -592,3 +556,3 @@ const type = context.cacheRedirects[typename];

if (resolver) {
fieldValue = resolver(obj, args, {
fieldValue = resolver(object, args, {
getCacheKey(storeObj: StoreObject) {

@@ -610,3 +574,3 @@ return toIdValue({

missing: [{
objectId: objId,
object,
fieldName: storeKeyName,

@@ -613,0 +577,0 @@ tolerable: false,

@@ -64,23 +64,3 @@ import {

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;
}
/**

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

},
parentKind: operationDefinition.kind,
});

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

context,
parentKind,
}: {

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

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

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

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

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

context,
parentKind: fragment.kind,
});

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

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;

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

context,
field.kind,
);

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

context,
parentKind: field.kind,
});

@@ -464,3 +418,2 @@ }

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

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

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

@@ -502,3 +449,2 @@

context,
parentKind,
});

@@ -505,0 +451,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