Terminate gql subscription on server address change

Otherwise, the subscription is still active for the old server instead of connecting to the new server
This commit is contained in:
schroda
2024-08-25 22:34:01 +02:00
parent 114f9fd34f
commit 08866ace3d
4 changed files with 23 additions and 12 deletions

View File

@@ -136,14 +136,24 @@ export class GraphQLClient extends BaseClient<
> {
readonly fetcher = null;
public declare client: ApolloClient<NormalizedCacheObject>;
public client!: ApolloClient<NormalizedCacheObject>;
private wsClient!: Client;
constructor() {
super();
this.createClient();
}
public override getBaseUrl(): string {
return `${super.getBaseUrl()}/api/graphql`;
}
public terminateSubscriptions(): void {
this.wsClient.terminate();
}
private createUploadLink() {
return createUploadLink({ uri: () => this.getBaseUrl(), credentials: 'include' });
}
@@ -182,13 +192,11 @@ export class GraphQLClient extends BaseClient<
});
const checkHeartbeatInterval = heartbeatInterval + 1000 * 10;
// for some reason "this.wsClient" is undefined in the "setInterval" callback
const { wsClient } = this;
setInterval(() => {
const isHeartbeatMissing = Date.now() - lastHeartbeat > checkHeartbeatInterval * 1.1;
if (isHeartbeatMissing) {
// force a reconnect
wsClient.terminate();
this.wsClient.terminate();
}
}, checkHeartbeatInterval);