@mearie/core 0.6.3 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +28 -4
- package/dist/index.mjs +28 -4
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -347,6 +347,26 @@ const makeEntityKey = (typename, keyValues) => {
|
|
|
347
347
|
const resolveArguments = (args, variables) => {
|
|
348
348
|
return Object.fromEntries(Object.entries(args).map(([key, value]) => [key, value.kind === "literal" ? value.value : variables[value.name]]));
|
|
349
349
|
};
|
|
350
|
+
const resolveDirectiveValue = (value, variables) => {
|
|
351
|
+
if (value !== null && typeof value === "object" && "kind" in value) {
|
|
352
|
+
const v = value;
|
|
353
|
+
if (v.kind === "variable" && v.name) return variables[v.name];
|
|
354
|
+
if (v.kind === "literal") return v.value;
|
|
355
|
+
}
|
|
356
|
+
return value;
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Determines whether a field should be included based on @skip/@include directives.
|
|
360
|
+
* @internal
|
|
361
|
+
*/
|
|
362
|
+
const shouldIncludeField = (directives, variables) => {
|
|
363
|
+
if (!directives) return true;
|
|
364
|
+
for (const d of directives) {
|
|
365
|
+
if (d.name === "skip" && resolveDirectiveValue(d.args?.if, variables) === true) return false;
|
|
366
|
+
if (d.name === "include" && resolveDirectiveValue(d.args?.if, variables) === false) return false;
|
|
367
|
+
}
|
|
368
|
+
return true;
|
|
369
|
+
};
|
|
350
370
|
/**
|
|
351
371
|
* Generates a cache key for a GraphQL field selection.
|
|
352
372
|
* Always uses the actual field name (not alias) with a stringified representation of the arguments.
|
|
@@ -556,6 +576,7 @@ const normalize = (schemaMeta, selections, storage, data, variables, accessor) =
|
|
|
556
576
|
if (entityKey) storageKey = entityKey;
|
|
557
577
|
const fields = {};
|
|
558
578
|
for (const selection of selections) if (selection.kind === "Field") {
|
|
579
|
+
if (!shouldIncludeField(selection.directives, variables)) continue;
|
|
559
580
|
const fieldKey = makeFieldKey(selection, variables);
|
|
560
581
|
let fieldValue = data[selection.alias ?? selection.name];
|
|
561
582
|
if (selection.name === "__typename" && fieldValue === void 0 && typename) fieldValue = typename;
|
|
@@ -612,6 +633,7 @@ const denormalize = (selections, storage, value, variables, accessor, options) =
|
|
|
612
633
|
}
|
|
613
634
|
const fields = {};
|
|
614
635
|
for (const selection of selections) if (selection.kind === "Field") {
|
|
636
|
+
if (!shouldIncludeField(selection.directives, variables)) continue;
|
|
615
637
|
const fieldKey = makeFieldKey(selection, variables);
|
|
616
638
|
const fieldValue = data[fieldKey];
|
|
617
639
|
const fieldPath = [...path, selection.alias ?? selection.name];
|
|
@@ -755,6 +777,7 @@ const traceSelections = (selections, storage, value, variables, storageKey, base
|
|
|
755
777
|
}
|
|
756
778
|
const fields = {};
|
|
757
779
|
for (const selection of sels) if (selection.kind === "Field") {
|
|
780
|
+
if (!shouldIncludeField(selection.directives, variables)) continue;
|
|
758
781
|
const fieldKey = makeFieldKey(selection, variables);
|
|
759
782
|
const fieldValue = data[fieldKey];
|
|
760
783
|
const fieldPath = [...path, selection.alias ?? selection.name];
|
|
@@ -980,7 +1003,7 @@ const classifyChanges = (changes) => {
|
|
|
980
1003
|
/**
|
|
981
1004
|
* @internal
|
|
982
1005
|
*/
|
|
983
|
-
const processScalarChanges = (changes, registry, subscriptions) => {
|
|
1006
|
+
const processScalarChanges = (changes, registry, subscriptions, storage) => {
|
|
984
1007
|
const result = /* @__PURE__ */ new Map();
|
|
985
1008
|
for (const change of changes) {
|
|
986
1009
|
const entries = registry.get(change.depKey);
|
|
@@ -990,8 +1013,9 @@ const processScalarChanges = (changes, registry, subscriptions) => {
|
|
|
990
1013
|
const sub = subscriptions.get(entry.subscriptionId);
|
|
991
1014
|
if (!sub) continue;
|
|
992
1015
|
let patchValue = change.newValue;
|
|
993
|
-
if (entry.selections && isNormalizedRecord(change.newValue)) {
|
|
994
|
-
const
|
|
1016
|
+
if (entry.selections && (isNormalizedRecord(change.newValue) || Array.isArray(change.newValue) && change.newValue.some((v) => isNormalizedRecord(v)))) {
|
|
1017
|
+
const mergedValue = storage[change.storageKey]?.[change.fieldKey] ?? change.newValue;
|
|
1018
|
+
const { data } = denormalize(entry.selections, {}, mergedValue, sub.variables);
|
|
995
1019
|
patchValue = data;
|
|
996
1020
|
}
|
|
997
1021
|
const patches = result.get(entry.subscriptionId) ?? [];
|
|
@@ -1586,7 +1610,7 @@ var Cache = class {
|
|
|
1586
1610
|
if (changes.length === 0) return;
|
|
1587
1611
|
const unstalledPatches = this.#checkStalled(changes);
|
|
1588
1612
|
const { scalar, structural } = classifyChanges(changes);
|
|
1589
|
-
const scalarPatches = processScalarChanges(scalar, this.#registry, this.#subscriptions);
|
|
1613
|
+
const scalarPatches = processScalarChanges(scalar, this.#registry, this.#subscriptions, this.#storage);
|
|
1590
1614
|
const structuralPatches = processStructuralChanges(structural, this.#registry, this.#subscriptions, this.#storage, this.#stalled);
|
|
1591
1615
|
const allPatches = /* @__PURE__ */ new Map();
|
|
1592
1616
|
for (const [subId, patches] of unstalledPatches) allPatches.set(subId, patches);
|
package/dist/index.mjs
CHANGED
|
@@ -346,6 +346,26 @@ const makeEntityKey = (typename, keyValues) => {
|
|
|
346
346
|
const resolveArguments = (args, variables) => {
|
|
347
347
|
return Object.fromEntries(Object.entries(args).map(([key, value]) => [key, value.kind === "literal" ? value.value : variables[value.name]]));
|
|
348
348
|
};
|
|
349
|
+
const resolveDirectiveValue = (value, variables) => {
|
|
350
|
+
if (value !== null && typeof value === "object" && "kind" in value) {
|
|
351
|
+
const v = value;
|
|
352
|
+
if (v.kind === "variable" && v.name) return variables[v.name];
|
|
353
|
+
if (v.kind === "literal") return v.value;
|
|
354
|
+
}
|
|
355
|
+
return value;
|
|
356
|
+
};
|
|
357
|
+
/**
|
|
358
|
+
* Determines whether a field should be included based on @skip/@include directives.
|
|
359
|
+
* @internal
|
|
360
|
+
*/
|
|
361
|
+
const shouldIncludeField = (directives, variables) => {
|
|
362
|
+
if (!directives) return true;
|
|
363
|
+
for (const d of directives) {
|
|
364
|
+
if (d.name === "skip" && resolveDirectiveValue(d.args?.if, variables) === true) return false;
|
|
365
|
+
if (d.name === "include" && resolveDirectiveValue(d.args?.if, variables) === false) return false;
|
|
366
|
+
}
|
|
367
|
+
return true;
|
|
368
|
+
};
|
|
349
369
|
/**
|
|
350
370
|
* Generates a cache key for a GraphQL field selection.
|
|
351
371
|
* Always uses the actual field name (not alias) with a stringified representation of the arguments.
|
|
@@ -555,6 +575,7 @@ const normalize = (schemaMeta, selections, storage, data, variables, accessor) =
|
|
|
555
575
|
if (entityKey) storageKey = entityKey;
|
|
556
576
|
const fields = {};
|
|
557
577
|
for (const selection of selections) if (selection.kind === "Field") {
|
|
578
|
+
if (!shouldIncludeField(selection.directives, variables)) continue;
|
|
558
579
|
const fieldKey = makeFieldKey(selection, variables);
|
|
559
580
|
let fieldValue = data[selection.alias ?? selection.name];
|
|
560
581
|
if (selection.name === "__typename" && fieldValue === void 0 && typename) fieldValue = typename;
|
|
@@ -611,6 +632,7 @@ const denormalize = (selections, storage, value, variables, accessor, options) =
|
|
|
611
632
|
}
|
|
612
633
|
const fields = {};
|
|
613
634
|
for (const selection of selections) if (selection.kind === "Field") {
|
|
635
|
+
if (!shouldIncludeField(selection.directives, variables)) continue;
|
|
614
636
|
const fieldKey = makeFieldKey(selection, variables);
|
|
615
637
|
const fieldValue = data[fieldKey];
|
|
616
638
|
const fieldPath = [...path, selection.alias ?? selection.name];
|
|
@@ -754,6 +776,7 @@ const traceSelections = (selections, storage, value, variables, storageKey, base
|
|
|
754
776
|
}
|
|
755
777
|
const fields = {};
|
|
756
778
|
for (const selection of sels) if (selection.kind === "Field") {
|
|
779
|
+
if (!shouldIncludeField(selection.directives, variables)) continue;
|
|
757
780
|
const fieldKey = makeFieldKey(selection, variables);
|
|
758
781
|
const fieldValue = data[fieldKey];
|
|
759
782
|
const fieldPath = [...path, selection.alias ?? selection.name];
|
|
@@ -979,7 +1002,7 @@ const classifyChanges = (changes) => {
|
|
|
979
1002
|
/**
|
|
980
1003
|
* @internal
|
|
981
1004
|
*/
|
|
982
|
-
const processScalarChanges = (changes, registry, subscriptions) => {
|
|
1005
|
+
const processScalarChanges = (changes, registry, subscriptions, storage) => {
|
|
983
1006
|
const result = /* @__PURE__ */ new Map();
|
|
984
1007
|
for (const change of changes) {
|
|
985
1008
|
const entries = registry.get(change.depKey);
|
|
@@ -989,8 +1012,9 @@ const processScalarChanges = (changes, registry, subscriptions) => {
|
|
|
989
1012
|
const sub = subscriptions.get(entry.subscriptionId);
|
|
990
1013
|
if (!sub) continue;
|
|
991
1014
|
let patchValue = change.newValue;
|
|
992
|
-
if (entry.selections && isNormalizedRecord(change.newValue)) {
|
|
993
|
-
const
|
|
1015
|
+
if (entry.selections && (isNormalizedRecord(change.newValue) || Array.isArray(change.newValue) && change.newValue.some((v) => isNormalizedRecord(v)))) {
|
|
1016
|
+
const mergedValue = storage[change.storageKey]?.[change.fieldKey] ?? change.newValue;
|
|
1017
|
+
const { data } = denormalize(entry.selections, {}, mergedValue, sub.variables);
|
|
994
1018
|
patchValue = data;
|
|
995
1019
|
}
|
|
996
1020
|
const patches = result.get(entry.subscriptionId) ?? [];
|
|
@@ -1585,7 +1609,7 @@ var Cache = class {
|
|
|
1585
1609
|
if (changes.length === 0) return;
|
|
1586
1610
|
const unstalledPatches = this.#checkStalled(changes);
|
|
1587
1611
|
const { scalar, structural } = classifyChanges(changes);
|
|
1588
|
-
const scalarPatches = processScalarChanges(scalar, this.#registry, this.#subscriptions);
|
|
1612
|
+
const scalarPatches = processScalarChanges(scalar, this.#registry, this.#subscriptions, this.#storage);
|
|
1589
1613
|
const structuralPatches = processStructuralChanges(structural, this.#registry, this.#subscriptions, this.#storage, this.#stalled);
|
|
1590
1614
|
const allPatches = /* @__PURE__ */ new Map();
|
|
1591
1615
|
for (const [subId, patches] of unstalledPatches) allPatches.set(subId, patches);
|