Get latest manga data from the apollo cache (#447)

When using the cached results, replace them with the latest cached data from apollo.
This will have the updated data, that was inserted from the manga update mutation
This commit is contained in:
schroda
2023-11-05 22:40:35 +01:00
committed by GitHub
parent 593acc7f89
commit c5f943d571

View File

@@ -206,7 +206,7 @@ import { DOWNLOAD_STATUS_SUBSCRIPTION } from '@/lib/graphql/subscriptions/Downlo
import { UPDATER_SUBSCRIPTION } from '@/lib/graphql/subscriptions/UpdaterSubscription.ts'; import { UPDATER_SUBSCRIPTION } from '@/lib/graphql/subscriptions/UpdaterSubscription.ts';
import { GET_SERVER_SETTINGS } from '@/lib/graphql/queries/SettingsQuery.ts'; import { GET_SERVER_SETTINGS } from '@/lib/graphql/queries/SettingsQuery.ts';
import { UPDATE_SERVER_SETTINGS } from '@/lib/graphql/mutations/SettingsMutation.ts'; import { UPDATE_SERVER_SETTINGS } from '@/lib/graphql/mutations/SettingsMutation.ts';
import { FULL_EXTENSION_FIELDS } from '@/lib/graphql/Fragments.ts'; import { BASE_MANGA_FIELDS, FULL_EXTENSION_FIELDS } from '@/lib/graphql/Fragments.ts';
enum GQLMethod { enum GQLMethod {
QUERY = 'QUERY', QUERY = 'QUERY',
@@ -1098,9 +1098,34 @@ export class RequestManager {
revalidate, revalidate,
); );
const normalizedCachedResults = cachedResults.map((cachedResult) => {
const hasResults = cachedResult.data?.fetchSourceManga.mangas;
if (!hasResults) {
return cachedResult;
}
return {
...cachedResult,
data: {
...cachedResult.data,
fetchSourceManga: {
...cachedResult.data?.fetchSourceManga,
mangas: cachedResult.data?.fetchSourceManga.mangas.map(
(manga) =>
this.graphQLClient.client.cache.readFragment<typeof manga>({
id: this.graphQLClient.client.cache.identify(manga),
fragment: BASE_MANGA_FIELDS,
fragmentName: 'BASE_MANGA_FIELDS',
}) ?? manga,
),
},
},
};
});
return this.returnPaginatedMutationResult( return this.returnPaginatedMutationResult(
areInitialPagesFetched, areInitialPagesFetched,
cachedResults, normalizedCachedResults,
getVariablesFor, getVariablesFor,
paginatedResult, paginatedResult,
wrappedMutate, wrappedMutate,