2023-09-01 20:21:59 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-10-04 20:50:11 +02:00
|
|
|
import {
|
|
|
|
|
ApolloClient,
|
|
|
|
|
ApolloClientOptions,
|
|
|
|
|
InMemoryCache,
|
|
|
|
|
NormalizedCacheObject,
|
|
|
|
|
Reference,
|
|
|
|
|
split,
|
|
|
|
|
} from '@apollo/client';
|
2023-09-01 20:21:59 +02:00
|
|
|
import { createUploadLink } from 'apollo-upload-client';
|
2023-10-04 20:50:11 +02:00
|
|
|
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
|
|
|
|
|
import { Client, createClient } from 'graphql-ws';
|
|
|
|
|
import { getMainDefinition } from '@apollo/client/utilities';
|
2023-09-01 20:21:59 +02:00
|
|
|
import { BaseClient } from '@/lib/requests/client/BaseClient.ts';
|
|
|
|
|
import { StrictTypedTypePolicies } from '@/lib/graphql/generated/apollo-helpers.ts';
|
|
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
/* eslint-disable no-underscore-dangle */
|
2023-09-01 20:21:59 +02:00
|
|
|
const typePolicies: StrictTypedTypePolicies = {
|
|
|
|
|
GlobalMetaType: { keyFields: ['key'] },
|
|
|
|
|
ExtensionType: { keyFields: ['apkName'] },
|
|
|
|
|
AboutPayload: { keyFields: [] },
|
2023-09-30 16:55:35 +02:00
|
|
|
Query: {
|
|
|
|
|
fields: {
|
|
|
|
|
chapters: {
|
|
|
|
|
keyArgs: ['condition', 'filter', 'orderBy', 'orderByType'],
|
|
|
|
|
merge(existing, incoming) {
|
|
|
|
|
const merged = {
|
|
|
|
|
...existing,
|
|
|
|
|
...incoming,
|
|
|
|
|
nodes: existing?.nodes ?? [],
|
|
|
|
|
};
|
|
|
|
|
const isRefetch = incoming.nodes.some(
|
|
|
|
|
(incomingChapter) =>
|
|
|
|
|
existing?.nodes.some(
|
|
|
|
|
(existingChapter) =>
|
|
|
|
|
(existingChapter as unknown as Reference).__ref ===
|
|
|
|
|
(incomingChapter as unknown as Reference).__ref,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
if (!isRefetch) {
|
|
|
|
|
merged.nodes = [...(existing?.nodes ?? []), ...incoming.nodes];
|
|
|
|
|
}
|
|
|
|
|
return merged;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2023-09-01 20:21:59 +02:00
|
|
|
};
|
2023-09-30 16:55:35 +02:00
|
|
|
/* eslint-enable no-underscore-dangle */
|
2023-09-01 20:21:59 +02:00
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/prefer-default-export
|
|
|
|
|
export class GraphQLClient extends BaseClient<
|
|
|
|
|
ApolloClient<NormalizedCacheObject>,
|
|
|
|
|
ApolloClientOptions<NormalizedCacheObject>,
|
|
|
|
|
null
|
|
|
|
|
> {
|
|
|
|
|
readonly fetcher = null;
|
|
|
|
|
|
|
|
|
|
public declare client: ApolloClient<NormalizedCacheObject>;
|
|
|
|
|
|
2023-10-04 20:50:11 +02:00
|
|
|
private wsClient!: Client;
|
|
|
|
|
|
2023-09-01 20:21:59 +02:00
|
|
|
public override getBaseUrl(): string {
|
|
|
|
|
return `${super.getBaseUrl()}/api/graphql`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createUploadLink() {
|
|
|
|
|
return createUploadLink({ uri: () => this.getBaseUrl() });
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-04 20:50:11 +02:00
|
|
|
private createWSLink() {
|
|
|
|
|
return new GraphQLWsLink(this.wsClient);
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-01 20:21:59 +02:00
|
|
|
private createLink() {
|
2023-10-04 20:50:11 +02:00
|
|
|
return split(
|
|
|
|
|
({ query }) => {
|
|
|
|
|
const definition = getMainDefinition(query);
|
|
|
|
|
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
|
|
|
|
|
},
|
|
|
|
|
this.createWSLink(),
|
|
|
|
|
this.createUploadLink(),
|
|
|
|
|
);
|
2023-09-01 20:21:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected createClient() {
|
2023-10-04 20:50:11 +02:00
|
|
|
this.wsClient = createClient({
|
|
|
|
|
url: () => this.getBaseUrl().replace(/http(|s)/g, 'ws'),
|
|
|
|
|
keepAlive: 20000,
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-01 20:21:59 +02:00
|
|
|
this.client = new ApolloClient({
|
|
|
|
|
cache: new InMemoryCache({
|
|
|
|
|
typePolicies,
|
|
|
|
|
}),
|
|
|
|
|
connectToDevTools: true,
|
|
|
|
|
link: this.createLink(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override updateConfig() {}
|
|
|
|
|
}
|