diff --git a/src/cache/inmemory/entityStore.ts b/src/cache/inmemory/entityStore.ts index 60f38a8b98c..db2d93f9fa1 100644 --- a/src/cache/inmemory/entityStore.ts +++ b/src/cache/inmemory/entityStore.ts @@ -36,19 +36,24 @@ export abstract class EntityStore implements NormalizedCache { return { ...this.data }; } - public has(dataId: string): boolean { - return this.get(dataId) !== void 0; + public has(dataId: string, fieldName?: string): boolean { + return this.get(dataId, fieldName) !== void 0; } - public get(dataId: string): StoreObject { - this.group.depend(dataId); - return this.data[dataId]; - } - - public getFieldValue(dataId: string, storeFieldName: string): StoreValue { - this.group.depend(dataId, storeFieldName); - const storeObject = this.data[dataId]; - return storeObject && storeObject[storeFieldName]; + public get(dataId: string): StoreObject; + public get(dataId: string, fieldName: string): StoreValue; + public get(dataId: string, fieldName?: string): StoreValue { + this.group.depend(dataId, fieldName); + if (hasOwn.call(this.data, dataId)) { + const storeObject = this.data[dataId]; + if (!fieldName) return storeObject; + if (hasOwn.call(storeObject, fieldName)) { + return storeObject[fieldName]; + } + } + if (this instanceof Layer) { + return this.parent.get(dataId, fieldName); + } } public merge(dataId: string, incoming: StoreObject): void { @@ -59,8 +64,8 @@ export abstract class EntityStore implements NormalizedCache { this.data[dataId] = merged; delete this.refs[dataId]; if (this.group.caching) { - // First, invalidate any dependents that called get rather than - // getFieldValue. + // First, invalidate any dependents that called store.get(id) + // rather than store.get(id, fieldName). this.group.dirty(dataId); // Now invalidate dependents who called getFieldValue for any // fields that are changing as a result of this merge. @@ -284,11 +289,7 @@ class CacheGroup { public dirty(dataId: string, storeFieldName?: string) { if (this.d) { - this.d.dirty( - typeof storeFieldName === "string" - ? makeDepKey(dataId, storeFieldName) - : makeDepKey(dataId), - ); + this.d.dirty(makeDepKey(dataId, storeFieldName)); } } @@ -390,40 +391,6 @@ class Layer extends EntityStore { }; } - public get(dataId: string): StoreObject { - if (hasOwn.call(this.data, dataId)) { - return super.get(dataId); - } - - // If this layer has a this.depend function and it's not the one - // this.parent is using, we need to depend on the given dataId using - // this.depend before delegating to the parent. This check saves us - // from calling this.depend for every optimistic layer we examine, but - // ensures we call this.depend in the last optimistic layer before we - // reach the root layer. - - if (this.group.caching && this.group !== this.parent.group) { - this.group.depend(dataId); - } - - return this.parent.get(dataId); - } - - public getFieldValue(dataId: string, storeFieldName: string): StoreValue { - if (hasOwn.call(this.data, dataId)) { - const storeObject = this.data[dataId]; - if (storeObject && hasOwn.call(storeObject, storeFieldName)) { - return super.getFieldValue(dataId, storeFieldName); - } - } - - if (this.group.caching && this.group !== this.parent.group) { - this.group.depend(dataId, storeFieldName); - } - - return this.parent.getFieldValue(dataId, storeFieldName); - } - // Return a Set of all the ID strings that have been retained by this // Layer *and* any layers/roots beneath it. public getRootIdSet(): Set { diff --git a/src/cache/inmemory/helpers.ts b/src/cache/inmemory/helpers.ts index 3a6b2f29a30..232e4c050a2 100644 --- a/src/cache/inmemory/helpers.ts +++ b/src/cache/inmemory/helpers.ts @@ -8,7 +8,7 @@ export function getTypenameFromStoreObject( objectOrReference: StoreObject | Reference, ): string | undefined { return isReference(objectOrReference) - ? store.getFieldValue(objectOrReference.__ref, "__typename") as string + ? store.get(objectOrReference.__ref, "__typename") as string : objectOrReference && objectOrReference.__typename; } diff --git a/src/cache/inmemory/policies.ts b/src/cache/inmemory/policies.ts index 3be80f7943c..2062c0e12fc 100644 --- a/src/cache/inmemory/policies.ts +++ b/src/cache/inmemory/policies.ts @@ -432,7 +432,7 @@ export class Policies { let fieldValue: StoreValue; if (isReference(objectOrReference)) { const dataId = objectOrReference.__ref; - fieldValue = store.getFieldValue(dataId, storeFieldName); + fieldValue = store.get(dataId, storeFieldName); if (fieldValue === void 0 && storeFieldName === "__typename") { // We can infer the __typename of singleton root objects like // ROOT_QUERY ("Query") and ROOT_MUTATION ("Mutation"), even if diff --git a/src/cache/inmemory/types.ts b/src/cache/inmemory/types.ts index 9d0255dbdde..e7b6f34bbd8 100644 --- a/src/cache/inmemory/types.ts +++ b/src/cache/inmemory/types.ts @@ -18,9 +18,9 @@ export declare type IdGetter = ( * StoreObjects from the cache */ export interface NormalizedCache { - has(dataId: string): boolean; + has(dataId: string, fieldName?: string): boolean; get(dataId: string): StoreObject; - getFieldValue(dataId: string, storeFieldName: string): StoreValue; + get(dataId: string, fieldName: string): StoreValue; merge(dataId: string, incoming: StoreObject): void; delete(dataId: string, fieldName?: string): void; clear(): void;