Setup graphql subscriptions

This commit is contained in:
schroda
2023-10-04 20:50:11 +02:00
parent 6a1c302e45
commit b79fbcf71d
4 changed files with 37 additions and 3 deletions

View File

@@ -285,7 +285,7 @@ export class RequestManager {
public updateClient(config: Partial<AxiosInstance['defaults']>): void {
this.restClient.updateConfig(config);
this.graphQLClient.updateConfig({ uri: config.baseURL });
this.graphQLClient.updateConfig();
}
public getBaseUrl(): string {

View File

@@ -6,8 +6,18 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { ApolloClient, ApolloClientOptions, InMemoryCache, NormalizedCacheObject, Reference } from '@apollo/client';
import {
ApolloClient,
ApolloClientOptions,
InMemoryCache,
NormalizedCacheObject,
Reference,
split,
} from '@apollo/client';
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 { BaseClient } from '@/lib/requests/client/BaseClient.ts';
import { StrictTypedTypePolicies } from '@/lib/graphql/generated/apollo-helpers.ts';
@@ -55,6 +65,8 @@ export class GraphQLClient extends BaseClient<
public declare client: ApolloClient<NormalizedCacheObject>;
private wsClient!: Client;
public override getBaseUrl(): string {
return `${super.getBaseUrl()}/api/graphql`;
}
@@ -63,11 +75,27 @@ export class GraphQLClient extends BaseClient<
return createUploadLink({ uri: () => this.getBaseUrl() });
}
private createWSLink() {
return new GraphQLWsLink(this.wsClient);
}
private createLink() {
return this.createUploadLink();
return split(
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
},
this.createWSLink(),
this.createUploadLink(),
);
}
protected createClient() {
this.wsClient = createClient({
url: () => this.getBaseUrl().replace(/http(|s)/g, 'ws'),
keepAlive: 20000,
});
this.client = new ApolloClient({
cache: new InMemoryCache({
typePolicies,