Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
}
if (storeFieldName === void 0) {
storeFieldName = storeKeyNameFromField(field, variables);
}
// Make sure custom field names start with the actual field.name.value
// of the field, so we can always figure out which properties of a
// StoreObject correspond to which original field names.
return fieldName === fieldNameFromStoreName(storeFieldName)
? storeFieldName
: fieldName + ":" + storeFieldName;
}
private storageTrie = new KeyTrie(true);
private fieldDep = dep();
public readField(
objectOrReference: StoreObject | Reference,
nameOrField: string | FieldNode,
getFieldValue: FieldValueGetter,
variables?: Record,
typename = getFieldValue(objectOrReference, "__typename"),
): Readonly {
const policies = this;
const storeFieldName = typeof nameOrField === "string" ? nameOrField
: policies.getStoreFieldName(typename, nameOrField, variables);
const fieldName = fieldNameFromStoreName(storeFieldName);
const existing = getFieldValue(objectOrReference, storeFieldName);
const policy = policies.getFieldPolicy(typename, fieldName, false);
const read = policy && policy.read;
? this.data[dataId]
: this.parent.get(dataId);
}
}
export class InMemoryCache extends ApolloCache {
private data: NormalizedCache;
private optimisticData: NormalizedCache;
protected config: InMemoryCacheConfig;
private watches = new Set();
private addTypename: boolean;
private typenameDocumentCache = new Map();
private storeReader: StoreReader;
private storeWriter: StoreWriter;
private cacheKeyRoot = new KeyTrie(canUseWeakMap);
// Set this while in a transaction to prevent broadcasts...
// don't forget to turn it back on!
private silenceBroadcast: boolean = false;
constructor(config: InMemoryCacheConfig = {}) {
super();
this.config = { ...defaultConfig, ...config };
// backwards compat
if ((this.config as any).customResolvers) {
invariant.warn(
'customResolvers have been renamed to cacheRedirects. Please update your config as we will be deprecating customResolvers in the next major version.',
);
this.config.cacheRedirects = (this.config as any).customResolvers;
}
function keyFieldsFnFromSpecifier(
specifier: KeySpecifier,
): KeyFieldsFunction {
const trie = new KeyTrie<{
aliasMap?: AliasMap;
}>(canUseWeakMap);
return (object, context) => {
let aliasMap: AliasMap;
if (context.selectionSet && context.fragmentMap) {
const info = trie.lookupArray([
context.selectionSet,
context.fragmentMap,
]);
aliasMap = info.aliasMap || (
info.aliasMap = makeAliasMap(context.selectionSet, context.fragmentMap)
);
}
return `${context.typename}:${
JSON.stringify(computeKeyObject(object, specifier, aliasMap))
constructor({
cacheKeyRoot = new KeyTrie(canUseWeakMap),
freezeResults = false,
}: StoreReaderConfig = {}) {
const {
executeStoreQuery,
executeSelectionSet,
executeSubSelectedArray,
} = this;
this.freezeResults = freezeResults;
this.executeStoreQuery = wrap((options: ExecStoreQueryOptions) => {
return executeStoreQuery.call(this, options);
}, {
makeCacheKey({
query,
rootValue,
constructor(config?: StoreReaderConfig) {
const cacheKeyRoot =
config && config.cacheKeyRoot || new KeyTrie(canUseWeakMap);
this.config = {
addTypename: true,
cacheKeyRoot,
...config,
};
const {
executeStoreQuery,
executeSelectionSet,
executeSubSelectedArray,
} = this;
this.executeStoreQuery = wrap((options: ExecStoreQueryOptions) => {
return executeStoreQuery.call(this, options);
}, {