···11+---
22+'@urql/exchange-graphcache': patch
33+---
44+55+Only record dependencies that are changing data, this will reduce the amount of operations we re-invoke due to network-only/cache-and-network queries and mutations
···9898 InMemoryData.gc();
9999100100 expect(InMemoryData.readRecord('Todo:1', 'id')).toBe('1');
101101+ // TODO: is it a problem that this fails, we are reading from Todo
102102+ // but we are not updating anything
101103 expect(InMemoryData.getCurrentDependencies()).toEqual(
102102- new Set(['Query.todo', 'Todo:1'])
104104+ new Set(['Query.todo'])
103105 );
104106 });
105107
+36-9
exchanges/graphcache/src/store/data.ts
···458458 entityKey: string,
459459 fieldKey: string
460460): EntityField => {
461461- updateDependencies(entityKey, fieldKey);
461461+ if (currentOperation === 'read') {
462462+ updateDependencies(entityKey, fieldKey);
463463+ }
462464 return getNode(currentData!.records, entityKey, fieldKey);
463465};
464466···467469 entityKey: string,
468470 fieldKey: string
469471): Link | undefined => {
470470- updateDependencies(entityKey, fieldKey);
472472+ if (currentOperation === 'read') {
473473+ updateDependencies(entityKey, fieldKey);
474474+ }
471475 return getNode(currentData!.links, entityKey, fieldKey);
472476};
473477···508512 fieldKey: string,
509513 value?: EntityField
510514) => {
511511- updateDependencies(entityKey, fieldKey);
512512- updatePersist(entityKey, fieldKey);
515515+ const existing = getNode(currentData!.records, entityKey, fieldKey);
516516+ if (!isEqualLinkOrScalar(existing, value)) {
517517+ updateDependencies(entityKey, fieldKey);
518518+ updatePersist(entityKey, fieldKey);
519519+ }
520520+513521 setNode(currentData!.records, entityKey, fieldKey, value);
514522};
515523···533541 updateRCForLink(entityLinks && entityLinks[fieldKey], -1);
534542 updateRCForLink(link, 1);
535543 }
536536- // Update persistence batch and dependencies
537537- updateDependencies(entityKey, fieldKey);
538538- updatePersist(entityKey, fieldKey);
544544+ const existing = getNode(currentData!.links, entityKey, fieldKey);
545545+ if (!isEqualLinkOrScalar(existing, link)) {
546546+ updateDependencies(entityKey, fieldKey);
547547+ updatePersist(entityKey, fieldKey);
548548+ }
549549+539550 // Update the link
540551 setNode(currentData!.links, entityKey, fieldKey, link);
541552};
···629640 for (const entry of links.entries()) {
630641 const entityKey = entry[0];
631642 const keyMap = entry[1];
632632- for (const fieldKey in keyMap)
643643+ for (const fieldKey in keyMap) {
633644 writeLink(entityKey, fieldKey, keyMap[fieldKey]);
645645+ }
634646 }
635647 }
636648···639651 for (const entry of records.entries()) {
640652 const entityKey = entry[0];
641653 const keyMap = entry[1];
642642- for (const fieldKey in keyMap)
654654+ for (const fieldKey in keyMap) {
643655 writeRecord(entityKey, fieldKey, keyMap[fieldKey]);
656656+ }
644657 }
645658 }
646659···710723 data.hydrating = false;
711724 clearDataState();
712725};
726726+727727+function isEqualLinkOrScalar(
728728+ a: Link | EntityField | undefined,
729729+ b: Link | EntityField | undefined
730730+) {
731731+ if (typeof a !== typeof b) return false;
732732+ if (a !== b) return false;
733733+ if (Array.isArray(a) && Array.isArray(b)) {
734734+ if (a.length !== b.length) return false;
735735+ return !a.some((el, index) => el !== b[index]);
736736+ }
737737+738738+ return true;
739739+}