Skip to content

Commit 236631d

Browse files
authored
Minor improvements (#13299)
Small improvements mentioned in #13270 (review). - Keeps `cache.extract()` cheap when no scalars are configured - Reuses an existing variable to remove unneeded duplicate code <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved cache extraction so scalar values are only normalized when scalar handling is enabled. * Preserved stored values unchanged when scalar handling is disabled. * Refined store field checks to avoid redundant lookups during validation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent aba0960 commit 236631d

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

src/cache/inmemory/entityStore.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -402,17 +402,21 @@ export abstract class EntityStore implements NormalizedCache {
402402
}
403403

404404
public extract(): NormalizedCacheObject {
405-
const obj = Object.fromEntries(
406-
Object.entries(this.toObject()).map(([dataId, storeObject]) => [
407-
dataId,
408-
storeObject &&
409-
this.coerceStoreObject(
410-
storeObject,
411-
(scalar, value) => scalar.coerceToSerialized(value),
412-
storeObject?.__typename || this.policies.rootTypenamesById[dataId]
413-
),
414-
])
415-
);
405+
let obj = this.toObject();
406+
407+
if (this.hasScalarConfig()) {
408+
obj = Object.fromEntries(
409+
Object.entries(obj).map(([dataId, storeObject]) => [
410+
dataId,
411+
storeObject &&
412+
this.coerceStoreObject(
413+
storeObject,
414+
(scalar, value) => scalar.coerceToSerialized(value),
415+
storeObject?.__typename || this.policies.rootTypenamesById[dataId]
416+
),
417+
])
418+
);
419+
}
416420

417421
const extraRootIds: string[] = [];
418422
this.getRootIdSet().forEach((id) => {
@@ -426,12 +430,16 @@ export abstract class EntityStore implements NormalizedCache {
426430
return obj;
427431
}
428432

433+
private hasScalarConfig() {
434+
return !!this.policies.cache["config"].scalars;
435+
}
436+
429437
private coerceStoreObject(
430438
obj: StoreObject,
431439
coerce: (scalar: Scalar<any, any>, value: unknown) => unknown,
432440
typename = obj.__typename
433441
): StoreObject {
434-
if (!typename || !this.policies.cache["config"].scalars) {
442+
if (!typename || !this.hasScalarConfig()) {
435443
return obj;
436444
}
437445

src/cache/inmemory/readFromStore.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,6 @@ export class StoreReader {
362362
// do nothing
363363
} else if (fieldValue != null) {
364364
if (__DEV__) {
365-
const typename = context.store.getFieldValue<string>(
366-
objectOrReference,
367-
"__typename"
368-
);
369365
const fieldName = selection.name.value;
370366

371367
if (typename) {

0 commit comments

Comments
 (0)