@mearie/core 0.5.0 → 0.5.1

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 CHANGED
@@ -518,22 +518,34 @@ const makeFieldKeyFromArgs = (field, args) => {
518
518
 
519
519
  //#endregion
520
520
  //#region src/cache/normalize.ts
521
+ const resolveTypename = (selections, data) => {
522
+ for (const s of selections) if (s.kind === "Field" && s.name === "__typename") return data[s.alias ?? "__typename"];
523
+ return data.__typename;
524
+ };
521
525
  const normalize = (schemaMeta, selections, storage, data, variables, accessor) => {
526
+ const resolveEntityKey = (typename, data) => {
527
+ if (!typename) return null;
528
+ const entityMeta = schemaMeta.entities[typename];
529
+ if (!entityMeta) return null;
530
+ const keys = entityMeta.keyFields.map((field) => data[field]);
531
+ if (keys.every((k) => k !== void 0 && k !== null)) return makeEntityKey(typename, keys);
532
+ return null;
533
+ };
522
534
  const normalizeField = (storageKey, selections, value) => {
523
535
  if (isNullish(value)) return value;
524
536
  if (Array.isArray(value)) return value.map((item) => normalizeField(storageKey, selections, item));
525
537
  const data = value;
526
- const typename = data.__typename;
527
- let entityMeta = schemaMeta.entities[typename];
528
- if (entityMeta) {
529
- const keys = entityMeta.keyFields.map((field) => data[field]);
530
- if (keys.every((k) => k !== void 0 && k !== null)) storageKey = makeEntityKey(typename, keys);
531
- else entityMeta = void 0;
532
- }
538
+ const typename = resolveTypename(selections, data);
539
+ const entityKey = resolveEntityKey(typename, data);
540
+ if (entityKey) storageKey = entityKey;
533
541
  const fields = {};
534
542
  for (const selection of selections) if (selection.kind === "Field") {
535
543
  const fieldKey = makeFieldKey(selection, variables);
536
544
  const fieldValue = data[selection.alias ?? selection.name];
545
+ if (storageKey !== null && selection.selections && typeof fieldValue === "object" && fieldValue !== null && !Array.isArray(fieldValue)) {
546
+ const fieldTypename = resolveTypename(selection.selections, fieldValue);
547
+ if (fieldTypename && schemaMeta.entities[fieldTypename] && !resolveEntityKey(fieldTypename, fieldValue) && isEntityLink(storage[storageKey]?.[fieldKey])) continue;
548
+ }
537
549
  const oldValue = storageKey === null ? void 0 : storage[storageKey]?.[fieldKey];
538
550
  if (storageKey !== null && (!selection.selections || isNullish(oldValue) || isNullish(fieldValue))) accessor?.(storageKey, fieldKey, oldValue, fieldValue);
539
551
  fields[fieldKey] = selection.selections ? normalizeField(null, selection.selections, fieldValue) : fieldValue;
@@ -542,11 +554,11 @@ const normalize = (schemaMeta, selections, storage, data, variables, accessor) =
542
554
  const inner = normalizeField(storageKey, selection.selections, value);
543
555
  if (!isEntityLink(inner)) mergeFields(fields, inner);
544
556
  }
545
- if (entityMeta && storageKey !== null) {
546
- const existing = storage[storageKey];
557
+ if (entityKey) {
558
+ const existing = storage[entityKey];
547
559
  if (existing) mergeFields(existing, fields);
548
- else storage[storageKey] = fields;
549
- return { [EntityLinkKey]: storageKey };
560
+ else storage[entityKey] = fields;
561
+ return { [EntityLinkKey]: entityKey };
550
562
  }
551
563
  return fields;
552
564
  };
package/dist/index.mjs CHANGED
@@ -517,22 +517,34 @@ const makeFieldKeyFromArgs = (field, args) => {
517
517
 
518
518
  //#endregion
519
519
  //#region src/cache/normalize.ts
520
+ const resolveTypename = (selections, data) => {
521
+ for (const s of selections) if (s.kind === "Field" && s.name === "__typename") return data[s.alias ?? "__typename"];
522
+ return data.__typename;
523
+ };
520
524
  const normalize = (schemaMeta, selections, storage, data, variables, accessor) => {
525
+ const resolveEntityKey = (typename, data) => {
526
+ if (!typename) return null;
527
+ const entityMeta = schemaMeta.entities[typename];
528
+ if (!entityMeta) return null;
529
+ const keys = entityMeta.keyFields.map((field) => data[field]);
530
+ if (keys.every((k) => k !== void 0 && k !== null)) return makeEntityKey(typename, keys);
531
+ return null;
532
+ };
521
533
  const normalizeField = (storageKey, selections, value) => {
522
534
  if (isNullish(value)) return value;
523
535
  if (Array.isArray(value)) return value.map((item) => normalizeField(storageKey, selections, item));
524
536
  const data = value;
525
- const typename = data.__typename;
526
- let entityMeta = schemaMeta.entities[typename];
527
- if (entityMeta) {
528
- const keys = entityMeta.keyFields.map((field) => data[field]);
529
- if (keys.every((k) => k !== void 0 && k !== null)) storageKey = makeEntityKey(typename, keys);
530
- else entityMeta = void 0;
531
- }
537
+ const typename = resolveTypename(selections, data);
538
+ const entityKey = resolveEntityKey(typename, data);
539
+ if (entityKey) storageKey = entityKey;
532
540
  const fields = {};
533
541
  for (const selection of selections) if (selection.kind === "Field") {
534
542
  const fieldKey = makeFieldKey(selection, variables);
535
543
  const fieldValue = data[selection.alias ?? selection.name];
544
+ if (storageKey !== null && selection.selections && typeof fieldValue === "object" && fieldValue !== null && !Array.isArray(fieldValue)) {
545
+ const fieldTypename = resolveTypename(selection.selections, fieldValue);
546
+ if (fieldTypename && schemaMeta.entities[fieldTypename] && !resolveEntityKey(fieldTypename, fieldValue) && isEntityLink(storage[storageKey]?.[fieldKey])) continue;
547
+ }
536
548
  const oldValue = storageKey === null ? void 0 : storage[storageKey]?.[fieldKey];
537
549
  if (storageKey !== null && (!selection.selections || isNullish(oldValue) || isNullish(fieldValue))) accessor?.(storageKey, fieldKey, oldValue, fieldValue);
538
550
  fields[fieldKey] = selection.selections ? normalizeField(null, selection.selections, fieldValue) : fieldValue;
@@ -541,11 +553,11 @@ const normalize = (schemaMeta, selections, storage, data, variables, accessor) =
541
553
  const inner = normalizeField(storageKey, selection.selections, value);
542
554
  if (!isEntityLink(inner)) mergeFields(fields, inner);
543
555
  }
544
- if (entityMeta && storageKey !== null) {
545
- const existing = storage[storageKey];
556
+ if (entityKey) {
557
+ const existing = storage[entityKey];
546
558
  if (existing) mergeFields(existing, fields);
547
- else storage[storageKey] = fields;
548
- return { [EntityLinkKey]: storageKey };
559
+ else storage[entityKey] = fields;
560
+ return { [EntityLinkKey]: entityKey };
549
561
  }
550
562
  return fields;
551
563
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mearie/core",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Type-safe, zero-overhead GraphQL client",
5
5
  "keywords": [
6
6
  "graphql",