diff --git a/src/components/files/updateCachedVersions.ts b/src/components/files/updateCachedVersions.ts index 23d2d98278..71a6c77ebe 100644 --- a/src/components/files/updateCachedVersions.ts +++ b/src/components/files/updateCachedVersions.ts @@ -7,36 +7,27 @@ export const updateCachedVersions = ( existingVersions: readonly FileNodeInfoFragment[], parentId: string ) => { - try { - const response = cache.readQuery({ - query: FileVersionsDocument, - variables: { id: parentId }, - }); - if (response) { - const updatedData = { - ...response, - file: { - ...response.file, - children: { - ...response.file.children, - items: existingVersions, - total: existingVersions.length, - }, - }, - }; - cache.writeQuery({ - query: FileVersionsDocument, - variables: { id: parentId }, - data: updatedData, - }); - } - } catch { - /** - * We need this try/catch because if this data has never been fetched - * before, `cache.readQuery` will throw an error instead of returning - * anything, which is apparently a behavior the Apollo team finds - * acceptable. - */ + const cachedFile = cache.readQuery({ + query: FileVersionsDocument, + variables: { id: parentId }, + }); + if (!cachedFile) { return; } + const updatedData = { + ...cachedFile, + file: { + ...cachedFile.file, + children: { + ...cachedFile.file.children, + items: existingVersions, + total: existingVersions.length, + }, + }, + }; + cache.writeQuery({ + query: FileVersionsDocument, + variables: { id: parentId }, + data: updatedData, + }); }; diff --git a/src/scenes/Projects/Files/useUploadProjectFiles.ts b/src/scenes/Projects/Files/useUploadProjectFiles.ts index 95efdd6696..b9ed3a5ee7 100644 --- a/src/scenes/Projects/Files/useUploadProjectFiles.ts +++ b/src/scenes/Projects/Files/useUploadProjectFiles.ts @@ -31,50 +31,42 @@ export const useUploadProjectFiles = (): UploadFilesConsumerFunction => { refetchQueries: action === 'file' ? [GQLOperations.Query.ProjectDirectory] : undefined, update: (cache, { data }) => { - if (data?.createFileVersion) { - if (action === 'version') { - updateCachedVersions( - cache, - data.createFileVersion.children.items, - parentId - ); - } else { - try { - const id = `Directory:${parentId}`; - const response = cache.readFragment({ - id, - fragment: ProjectDirectoryContentsFragmentDoc, - fragmentName: 'ProjectDirectoryContents', - }); - if (response) { - const newFile = data.createFileVersion; - const currentItems = response.children.items; - const updatedData = { - ...response, - children: { - ...response.children, - items: currentItems.concat(newFile), - total: currentItems.length + 1, - }, - }; - cache.writeFragment({ - id, - fragment: ProjectDirectoryContentsFragmentDoc, - fragmentName: 'ProjectDirectoryContents', - data: updatedData, - }); - } - } catch { - /** - * We need this try/catch because if this data has never been fetched - * before, `cache.readFragment` will throw an error instead of returning - * anything, which is apparently a behavior the Apollo team finds - * acceptable. - */ - return; - } - } + if (!data?.createFileVersion) { + return; + } + if (action === 'version') { + updateCachedVersions( + cache, + data.createFileVersion.children.items, + parentId + ); + return; + } + const id = `Directory:${parentId}`; + const cachedDir = cache.readFragment({ + id, + fragment: ProjectDirectoryContentsFragmentDoc, + fragmentName: 'ProjectDirectoryContents', + }); + if (!cachedDir) { + return; } + const newFile = data.createFileVersion; + const currentItems = cachedDir.children.items; + const updatedData = { + ...cachedDir, + children: { + ...cachedDir.children, + items: currentItems.concat(newFile), + total: currentItems.length + 1, + }, + }; + cache.writeFragment({ + id, + fragment: ProjectDirectoryContentsFragmentDoc, + fragmentName: 'ProjectDirectoryContents', + data: updatedData, + }); }, }); };