Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code 961c5f985d Apollo v4 upgrade breaks record creation cache update
https://sonarly.com/issue/17030?type=bug

Apollo Client v3→v4 upgrade causes `toReference()` to return null in the post-mutation cache update callback when creating opportunity records, because `__typename` is no longer automatically present on mutation response data passed to `update` callbacks.

Fix: In `useCreateOneRecord.ts`, the post-mutation `update` callback (line 146) was passing the raw mutation response `data?.[mutationResponseField]` directly to `triggerCreateRecordsOptimisticEffect`. After the Apollo v3→v4 upgrade, this raw response may lack `__typename`, causing `toReference()` inside `cache.modify()` to return null and throw.

The fix transforms the raw mutation response through `getRecordNodeFromRecord()` (with `computeReferences: false`) before passing it to `triggerCreateRecordsOptimisticEffect`. This ensures `__typename` is always present on the record node, matching the pattern already used in the optimistic path at lines 116-122 of the same file. Added a null check on the result to avoid passing null into the optimistic effect.
2026-03-20 18:19:34 +00:00
@@ -146,16 +146,26 @@ export const useCreateOneRecord = <
update: (cache, { data }) => {
const record = data?.[mutationResponseField];
if (skipPostOptimisticEffect === false && isDefined(record)) {
triggerCreateRecordsOptimisticEffect({
cache,
const recordNode = getRecordNodeFromRecord({
objectMetadataItem,
recordsToCreate: [record],
objectMetadataItems,
shouldMatchRootQueryFilter,
checkForRecordInCache: true,
objectPermissionsByObjectMetadataId,
upsertRecordsInStore,
record: record as CreatedObjectRecord,
recordGqlFields: computedRecordGqlFields,
computeReferences: false,
});
if (isDefined(recordNode)) {
triggerCreateRecordsOptimisticEffect({
cache,
objectMetadataItem,
recordsToCreate: [recordNode],
objectMetadataItems,
shouldMatchRootQueryFilter,
checkForRecordInCache: true,
objectPermissionsByObjectMetadataId,
upsertRecordsInStore,
});
}
}
setLoading(false);