## Query depth deprecation I'm deprecating depth parameter in our graphql query / cache tooling. They were obsolete since we introduce the possibility to provide RecordGqlFields ## Refactor combinedFindManyRecordHook The hook can now take an array of operationSignatures ## Fix tasks issues Fix optimistic rendering issue. Note that we still haven't handle optimisticEffect on creation properly
72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import { ApolloCache, StoreObject } from '@apollo/client';
|
|
|
|
import { RecordGqlRefEdge } from '@/object-record/cache/types/RecordGqlRefEdge';
|
|
import { isObjectRecordConnectionWithRefs } from '@/object-record/cache/utils/isObjectRecordConnectionWithRefs';
|
|
import { isDefined } from '~/utils/isDefined';
|
|
import { capitalize } from '~/utils/string/capitalize';
|
|
|
|
export const triggerAttachRelationOptimisticEffect = ({
|
|
cache,
|
|
sourceObjectNameSingular,
|
|
sourceRecordId,
|
|
targetObjectNameSingular,
|
|
fieldNameOnTargetRecord,
|
|
targetRecordId,
|
|
}: {
|
|
cache: ApolloCache<unknown>;
|
|
sourceObjectNameSingular: string;
|
|
sourceRecordId: string;
|
|
targetObjectNameSingular: string;
|
|
fieldNameOnTargetRecord: string;
|
|
targetRecordId: string;
|
|
}) => {
|
|
const sourceRecordTypeName = capitalize(sourceObjectNameSingular);
|
|
const targetRecordTypeName = capitalize(targetObjectNameSingular);
|
|
|
|
const targetRecordCacheId = cache.identify({
|
|
id: targetRecordId,
|
|
__typename: targetRecordTypeName,
|
|
});
|
|
|
|
cache.modify<StoreObject>({
|
|
id: targetRecordCacheId,
|
|
fields: {
|
|
[fieldNameOnTargetRecord]: (targetRecordFieldValue, { toReference }) => {
|
|
const fieldValueisObjectRecordConnectionWithRefs =
|
|
isObjectRecordConnectionWithRefs(
|
|
sourceObjectNameSingular,
|
|
targetRecordFieldValue,
|
|
);
|
|
|
|
const sourceRecordReference = toReference({
|
|
id: sourceRecordId,
|
|
__typename: sourceRecordTypeName,
|
|
});
|
|
|
|
if (!isDefined(sourceRecordReference)) {
|
|
return targetRecordFieldValue;
|
|
}
|
|
|
|
if (fieldValueisObjectRecordConnectionWithRefs) {
|
|
const nextEdges: RecordGqlRefEdge[] = [
|
|
...targetRecordFieldValue.edges,
|
|
{
|
|
__typename: `${sourceRecordTypeName}Edge`,
|
|
node: sourceRecordReference,
|
|
cursor: '',
|
|
},
|
|
];
|
|
|
|
return {
|
|
...targetRecordFieldValue,
|
|
edges: nextEdges,
|
|
};
|
|
} else {
|
|
// To one object => attach next relation record
|
|
return sourceRecordReference;
|
|
}
|
|
},
|
|
},
|
|
});
|
|
};
|