From c5f943d571649462167905b8957933f6def01a71 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 5 Nov 2023 22:40:35 +0100 Subject: [PATCH] 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 --- src/lib/requests/RequestManager.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/lib/requests/RequestManager.ts b/src/lib/requests/RequestManager.ts index 07242509..fdba9c71 100644 --- a/src/lib/requests/RequestManager.ts +++ b/src/lib/requests/RequestManager.ts @@ -206,7 +206,7 @@ import { DOWNLOAD_STATUS_SUBSCRIPTION } from '@/lib/graphql/subscriptions/Downlo import { UPDATER_SUBSCRIPTION } from '@/lib/graphql/subscriptions/UpdaterSubscription.ts'; import { GET_SERVER_SETTINGS } from '@/lib/graphql/queries/SettingsQuery.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 { QUERY = 'QUERY', @@ -1098,9 +1098,34 @@ export class RequestManager { 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({ + id: this.graphQLClient.client.cache.identify(manga), + fragment: BASE_MANGA_FIELDS, + fragmentName: 'BASE_MANGA_FIELDS', + }) ?? manga, + ), + }, + }, + }; + }); + return this.returnPaginatedMutationResult( areInitialPagesFetched, - cachedResults, + normalizedCachedResults, getVariablesFor, paginatedResult, wrappedMutate,