# Introduction While importing records encountering missing expected fields when writting a fragment from apollo cache ## Updates ### 1/ `createdBy` Default value When inserting in cache in create single or many we will now make optimistic behavior on the createdBy value ### 2/ `createRecordInCache` dynamically create `recordGrqlFields` When creating an entry in cache, we will now dynamically generate fields to be written in the fragment instead of expecting all of them. As by nature record could be partial ### 3/ Strictly typed `RecordGqlFields` # Conclusion closes #9927
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { useRecoilValue } from 'recoil';
|
|
|
|
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
|
|
import { RecordGqlOperationFilter } from '@/object-record/graphql/types/RecordGqlOperationFilter';
|
|
import { useFindManyRecords } from '@/object-record/hooks/useFindManyRecords';
|
|
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
|
|
import { PREFETCH_CONFIG } from '@/prefetch/constants/PrefetchConfig';
|
|
import { prefetchIsLoadedFamilyState } from '@/prefetch/states/prefetchIsLoadedFamilyState';
|
|
import { PrefetchKey } from '@/prefetch/types/PrefetchKey';
|
|
|
|
export const usePrefetchedData = <T extends ObjectRecord>(
|
|
prefetchKey: PrefetchKey,
|
|
filter?: RecordGqlOperationFilter,
|
|
) => {
|
|
const isDataPrefetched = useRecoilValue(
|
|
prefetchIsLoadedFamilyState(prefetchKey),
|
|
);
|
|
|
|
const { operationSignatureFactory, objectNameSingular } =
|
|
PREFETCH_CONFIG[prefetchKey];
|
|
|
|
const { objectMetadataItem } = useObjectMetadataItem({
|
|
objectNameSingular,
|
|
});
|
|
|
|
const recordGqlFields =
|
|
operationSignatureFactory({ objectMetadataItem }).fields ?? filter;
|
|
const { records } = useFindManyRecords<T>({
|
|
skip: !isDataPrefetched,
|
|
objectNameSingular: objectNameSingular,
|
|
recordGqlFields,
|
|
});
|
|
|
|
return {
|
|
isDataPrefetched,
|
|
records,
|
|
};
|
|
};
|