AAE-30358 Remove client if already exists when connection is closed to use the refreshed token changing the page, turn connectionParams into a function to ensures that the latest token is always used for WebSocket connections

This commit is contained in:
alep85
2025-04-14 14:56:56 +02:00
parent d09b8daa3a
commit 76999ee830
2 changed files with 12 additions and 3 deletions
@@ -27,7 +27,7 @@ describe('WebSocketService', () => {
let service: WebSocketService; let service: WebSocketService;
const onLogoutSubject: Subject<void> = new Subject<void>(); const onLogoutSubject: Subject<void> = new Subject<void>();
const apolloMock = jasmine.createSpyObj('Apollo', ['use', 'createNamed']); const apolloMock = jasmine.createSpyObj('Apollo', ['use', 'createNamed', 'removeClient']);
beforeEach(() => { beforeEach(() => {
TestBed.configureTestingModule({ TestBed.configureTestingModule({
@@ -72,6 +72,12 @@ export class WebSocketService {
return this.apollo.use(apolloClientName).subscribe<T>({ errorPolicy: 'all', ...subscriptionOptions }); return this.apollo.use(apolloClientName).subscribe<T>({ errorPolicy: 'all', ...subscriptionOptions });
} }
private removeApolloClientIfExists(apolloClientName: string) {
if (this.apollo.use(apolloClientName)) {
this.apollo.removeClient(apolloClientName);
}
}
private get contextRoot() { private get contextRoot() {
return this.appConfigService.get('bpmHost', ''); return this.appConfigService.get('bpmHost', '');
} }
@@ -155,10 +161,13 @@ export class WebSocketService {
this.wsLink = new GraphQLWsLink( this.wsLink = new GraphQLWsLink(
createClient({ createClient({
url: this.createWsUrl(options.wsUrl) + '/v2/ws/graphql', url: this.createWsUrl(options.wsUrl) + '/v2/ws/graphql',
connectionParams: { connectionParams: () => ({
Authorization: 'Bearer ' + this.authService.getToken() Authorization: 'Bearer ' + this.authService.getToken()
}, }),
on: { on: {
closed: () => {
this.removeApolloClientIfExists(options.apolloClientName);
},
error: () => { error: () => {
this.apollo.removeClient(options.apolloClientName); this.apollo.removeClient(options.apolloClientName);
this.initSubscriptions(options); this.initSubscriptions(options);