[Codegen] Check cache before executing query for a single item (#513)

E.g. in case a manga was already loaded through the category mangas query, the query for that specific manga should not run against the server if it hasn't been executed before
This commit is contained in:
schroda
2023-12-25 21:20:07 +01:00
committed by GitHub
parent c115fcd573
commit 980da657d9
3 changed files with 64 additions and 17 deletions

View File

@@ -11,6 +11,7 @@ import { createUploadLink } from 'apollo-upload-client';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { Client, createClient } from 'graphql-ws';
import { getMainDefinition } from '@apollo/client/utilities';
import { TypePolicies } from '@apollo/client/cache';
import { BaseClient } from '@/lib/requests/client/BaseClient.ts';
import { StrictTypedTypePolicies } from '@/lib/graphql/generated/apollo-helpers.ts';
@@ -27,9 +28,40 @@ const typePolicies: StrictTypedTypePolicies = {
DownloadType: { keyFields: ['chapter'] },
Query: {
fields: {
manga(_, { args, toReference }) {
return toReference({
__typename: 'MangaType',
id: args?.id,
});
},
category(_, { args, toReference }) {
return toReference({
__typename: 'CategoryType',
id: args?.id,
});
},
source(_, { args, toReference }) {
return toReference({
__typename: 'SourceType',
id: args?.id,
});
},
extension(_, { args, toReference }) {
return toReference({
__typename: 'ExtensionType',
apkName: args?.pkgName,
});
},
meta(_, { args, toReference }) {
return toReference({
__typename: 'GlobalMetaType',
key: args?.key,
});
},
chapters: {
keyArgs: ['condition', 'filter', 'orderBy', 'orderByType'],
merge(existing, incoming) {
console.log('merge chapters', { ...existing }, { ...incoming });
if (existing == null) {
return incoming;
}
@@ -116,7 +148,12 @@ export class GraphQLClient extends BaseClient<
this.client = new ApolloClient({
cache: new InMemoryCache({
typePolicies,
// for whatever reason there is some weird TypeError complaining that
// "FieldReadFunction<Reference, Reference, FieldFunctionOptions<SomeObject, Record<string, any>>"
// is not compatible with "FieldReadFunction<any, any, FieldFunctionOptions<Record<string, any, Record<string, any>>"
// Since "typePolicies" is correctly typed as StrictTypedTypePolicies, and it is working as expected,
// the TypeError can just be ignored
typePolicies: typePolicies as TypePolicies,
}),
connectToDevTools: true,
link: this.createLink(),