Restart graphql subscriptions after ws client recreation
Disposing the client does not restart active subscriptions.
This commit is contained in:
@@ -1186,11 +1186,16 @@ export class RequestManager {
|
|||||||
abortRequest,
|
abortRequest,
|
||||||
};
|
};
|
||||||
case GQLMethod.USE_SUBSCRIPTION:
|
case GQLMethod.USE_SUBSCRIPTION:
|
||||||
return useSubscription<Data, Variables>(operation, {
|
// eslint-disable-next-line no-case-declarations
|
||||||
|
const subscription = useSubscription<Data, Variables>(operation, {
|
||||||
client: this.graphQLClient.client,
|
client: this.graphQLClient.client,
|
||||||
variables,
|
variables,
|
||||||
...(options as SubscriptionHookOptions<Data, Variables>),
|
...(options as SubscriptionHookOptions<Data, Variables>),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.graphQLClient.useRestartSubscription(subscription.restart);
|
||||||
|
|
||||||
|
return subscription;
|
||||||
default:
|
default:
|
||||||
throw new Error(`unexpected GQLRequest type "${method}"`);
|
throw new Error(`unexpected GQLRequest type "${method}"`);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ import { getMainDefinition } from '@apollo/client/utilities';
|
|||||||
import { TypePolicies } from '@apollo/client/cache';
|
import { TypePolicies } from '@apollo/client/cache';
|
||||||
import { removeTypenameFromVariables } from '@apollo/client/link/remove-typename';
|
import { removeTypenameFromVariables } from '@apollo/client/link/remove-typename';
|
||||||
import { d } from 'koration';
|
import { d } from 'koration';
|
||||||
|
import { useId } from '@mantine/hooks';
|
||||||
|
import { useEffect } from 'react';
|
||||||
import { BaseClient } from '@/lib/requests/client/BaseClient.ts';
|
import { BaseClient } from '@/lib/requests/client/BaseClient.ts';
|
||||||
import { StrictTypedTypePolicies } from '@/lib/graphql/generated/apollo-helpers.ts';
|
import { StrictTypedTypePolicies } from '@/lib/graphql/generated/apollo-helpers.ts';
|
||||||
import { AuthManager } from '@/features/authentication/AuthManager.ts';
|
import { AuthManager } from '@/features/authentication/AuthManager.ts';
|
||||||
@@ -197,6 +199,8 @@ export class GraphQLClient extends BaseClient<
|
|||||||
|
|
||||||
private wsClientAliveCheckInterval: NodeJS.Timeout | undefined = undefined;
|
private wsClientAliveCheckInterval: NodeJS.Timeout | undefined = undefined;
|
||||||
|
|
||||||
|
private activeConnectionSubscriptions = new Map<string, () => void>();
|
||||||
|
|
||||||
constructor(handleRefreshToken: (refreshToken: string) => AbortableApolloMutationResponse<UserRefreshMutation>) {
|
constructor(handleRefreshToken: (refreshToken: string) => AbortableApolloMutationResponse<UserRefreshMutation>) {
|
||||||
super(handleRefreshToken);
|
super(handleRefreshToken);
|
||||||
|
|
||||||
@@ -335,6 +339,8 @@ export class GraphQLClient extends BaseClient<
|
|||||||
|
|
||||||
this.createWSClient(false);
|
this.createWSClient(false);
|
||||||
this.client.setLink(this.createLink());
|
this.client.setLink(this.createLink());
|
||||||
|
|
||||||
|
this.activeConnectionSubscriptions.forEach((callback) => callback());
|
||||||
}
|
}
|
||||||
}, checkHeartbeatInterval);
|
}, checkHeartbeatInterval);
|
||||||
}
|
}
|
||||||
@@ -356,4 +362,19 @@ export class GraphQLClient extends BaseClient<
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override updateConfig() {}
|
public override updateConfig() {}
|
||||||
|
|
||||||
|
public useRestartSubscription(restart: () => void) {
|
||||||
|
const id = useId();
|
||||||
|
|
||||||
|
this.activeConnectionSubscriptions.set(id, () => {
|
||||||
|
restart();
|
||||||
|
});
|
||||||
|
|
||||||
|
useEffect(
|
||||||
|
() => () => {
|
||||||
|
this.activeConnectionSubscriptions.delete(id);
|
||||||
|
},
|
||||||
|
[id],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user