Handle cache update when creating new metadata

In case the metadata didn't exist yet, it was never added to the meta array because the objects meta field was never updated
This commit is contained in:
schroda
2024-09-22 02:46:01 +02:00
parent 7abfbd09ef
commit a06e170d78
10 changed files with 174 additions and 45 deletions

View File

@@ -25,8 +25,9 @@ import {
useQuery,
useSubscription,
} from '@apollo/client';
import { OperationVariables } from '@apollo/client/core';
import { OperationVariables, Reference } from '@apollo/client/core';
import { useEffect, useMemo, useRef, useState } from 'react';
import { ReadFieldFunction } from '@apollo/client/cache/core/types/common';
import { IRestClient, RestClient } from '@/lib/requests/client/RestClient.ts';
import { GraphQLClient } from '@/lib/requests/client/GraphQLClient.ts';
import {
@@ -301,8 +302,12 @@ import { ControlledPromise } from '@/lib/ControlledPromise.ts';
import { MetadataMigrationSettings } from '@/typings.ts';
import { DOWNLOAD_STATUS_FIELDS } from '@/lib/graphql/fragments/DownloadFragments.ts';
import { EXTENSION_LIST_FIELDS } from '@/lib/graphql/fragments/ExtensionFragments.ts';
import { MANGA_BASE_FIELDS } from '@/lib/graphql/fragments/MangaFragments.ts';
import { MANGA_BASE_FIELDS, MANGA_META_FIELDS } from '@/lib/graphql/fragments/MangaFragments.ts';
import { MangaIdInfo } from '@/lib/data/Mangas.ts';
import { GLOBAL_METADATA } from '@/lib/graphql/fragments/Fragments';
import { CATEGORY_META_FIELDS } from '@/lib/graphql/fragments/CategoryFragments.ts';
import { SOURCE_META_FIELDS } from '@/lib/graphql/fragments/SourceFragments.ts';
import { CHAPTER_META_FIELDS } from '@/lib/graphql/fragments/ChapterFragments.ts';
enum GQLMethod {
QUERY = 'QUERY',
@@ -403,6 +408,24 @@ export const SPECIAL_ED_SOURCES = {
],
};
const updateMetadata = (
key: string,
existingMetas: Reference[] | undefined,
readField: ReadFieldFunction,
createMetaRef: () => Reference | undefined,
): (Reference | undefined)[] | undefined => {
if (!existingMetas) {
return existingMetas;
}
const exists = existingMetas.some((metaRef: Reference) => readField('key', metaRef) === key);
if (exists) {
return existingMetas;
}
return [...existingMetas, createMetaRef()];
};
// TODO - extract logic to reduce the size of this file... grew waaaaaaaaaaaaay too big peepoFat
// TODO - correctly update cache after all mutations instead of refetching queries
export class RequestManager {
@@ -1083,6 +1106,21 @@ export class RequestManager {
},
},
},
update(cache, { data }) {
cache.modify({
id: cache.identify({ __typename: 'GlobalMetaType', key }),
fields: {
meta(existingMetas, { readField }) {
return updateMetadata(key, existingMetas, readField, () =>
cache.writeFragment({
data: data!.setGlobalMeta!.meta,
fragment: GLOBAL_METADATA,
}),
);
},
},
});
},
...options,
},
);
@@ -1364,6 +1402,21 @@ export class RequestManager {
},
},
},
update(cache, { data }) {
cache.modify({
id: cache.identify({ __typename: 'SourceType', id: sourceId }),
fields: {
meta(existingMetas, { readField }) {
return updateMetadata(key, existingMetas, readField, () =>
cache.writeFragment({
data: data!.setSourceMeta!.meta,
fragment: SOURCE_META_FIELDS,
}),
);
},
},
});
},
...options,
},
);
@@ -1843,6 +1896,21 @@ export class RequestManager {
},
},
},
update(cache, { data }) {
cache.modify({
id: cache.identify({ __typename: 'MangaType', id: mangaId }),
fields: {
meta(existingMetas, { readField }) {
return updateMetadata(key, existingMetas, readField, () =>
cache.writeFragment({
data: data!.setMangaMeta!.meta,
fragment: MANGA_META_FIELDS,
}),
);
},
},
});
},
...options,
},
);
@@ -2026,6 +2094,21 @@ export class RequestManager {
},
},
},
update(cache, { data }) {
cache.modify({
id: cache.identify({ __typename: 'ChapterType', id: chapterId }),
fields: {
meta(existingMetas, { readField }) {
return updateMetadata(key, existingMetas, readField, () =>
cache.writeFragment({
data: data!.setChapterMeta!.meta,
fragment: CHAPTER_META_FIELDS,
}),
);
},
},
});
},
...options,
},
);
@@ -2260,6 +2343,21 @@ export class RequestManager {
},
},
},
update(cache, { data }) {
cache.modify({
id: cache.identify({ __typename: 'CategoryType', id: categoryId }),
fields: {
meta(existingMetas, { readField }) {
return updateMetadata(key, existingMetas, readField, () =>
cache.writeFragment({
data: data!.setCategoryMeta!.meta,
fragment: CATEGORY_META_FIELDS,
}),
);
},
},
});
},
...options,
},
);