Skip to content

Commit

Permalink
Remove try/catch from cache.read calls as they won't throw a MissingF…
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Jan 20, 2021
1 parent 68eaa9d commit df8ce0a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 73 deletions.
51 changes: 21 additions & 30 deletions src/components/files/updateCachedVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};
78 changes: 35 additions & 43 deletions src/scenes/Projects/Files/useUploadProjectFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
},
});
};
Expand Down

0 comments on commit df8ce0a

Please sign in to comment.