diff --git a/packages/graph/src/-private/operations/replace-related-records.ts b/packages/graph/src/-private/operations/replace-related-records.ts index 86188fbd6cd..6bb3d12a44f 100644 --- a/packages/graph/src/-private/operations/replace-related-records.ts +++ b/packages/graph/src/-private/operations/replace-related-records.ts @@ -78,6 +78,13 @@ function replaceRelatedRecordsLocal(graph: Graph, op: ReplaceRelatedRecordsOpera const identifiers = op.value; const relationship = graph.get(op.record, op.field); assert(`expected hasMany relationship`, isHasMany(relationship)); + + // relationships for newly created records begin in the dirty state, so if updated + // before flushed we would fail to notify. This check helps us avoid that. + const isMaybeFirstUpdate = + relationship.remoteState.length === 0 && + relationship.localState === null && + relationship.state.hasReceivedData === false; relationship.state.hasReceivedData = true; const { additions, removals } = relationship; const { inverseKey, type } = relationship.definition; @@ -151,7 +158,10 @@ function replaceRelatedRecordsLocal(graph: Graph, op: ReplaceRelatedRecordsOpera relationship.localState = diff.finalState; relationship.isDirty = wasDirty; - if (!wasDirty /*&& becameDirty // TODO to guard like this we need to detect reorder when diffing local */) { + if ( + isMaybeFirstUpdate || + !wasDirty /*&& becameDirty // TODO to guard like this we need to detect reorder when diffing local */ + ) { notifyChange(graph, op.record, op.field); } } diff --git a/packages/model/src/-private/many-array.ts b/packages/model/src/-private/many-array.ts index 17ff9e2dc31..0b51dae3f7a 100644 --- a/packages/model/src/-private/many-array.ts +++ b/packages/model/src/-private/many-array.ts @@ -300,7 +300,7 @@ export default class RelatedCollection extends RecordArray { const [start, deleteCount, ...adds] = args as [number, number, ...OpaqueRecordInstance[]]; // detect a full replace - if (start === 0 && deleteCount === this[SOURCE].length && this[SOURCE].length > 0) { + if (start === 0 && deleteCount === this[SOURCE].length) { const newValues = extractIdentifiersFromRecords(adds); assertNoDuplicates(