Skip to content

Commit 18322fd

Browse files
committed
AAE-30358 Remove apollo client if already exists and init subscription to use the frefreshed token changing the page, turn connectionParams into a function to ensures that the latest token is always used for WebSocket connections
1 parent 659805a commit 18322fd

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/process-services-cloud/src/lib/services/web-socket.service.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('WebSocketService', () => {
2727
let service: WebSocketService;
2828
const onLogoutSubject: Subject<void> = new Subject<void>();
2929

30-
const apolloMock = jasmine.createSpyObj('Apollo', ['use', 'createNamed']);
30+
const apolloMock = jasmine.createSpyObj('Apollo', ['use', 'createNamed', 'removeClient']);
3131

3232
beforeEach(() => {
3333
TestBed.configureTestingModule({
@@ -61,7 +61,7 @@ describe('WebSocketService', () => {
6161
apolloMock.createNamed.calls.reset();
6262
});
6363

64-
it('should not create a new Apollo client if it is already in use', async () => {
64+
it('should create a new Apollo client if it is already in use to connect with a new auth token', async () => {
6565
const apolloClientName = 'testClient';
6666
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) };
6767
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions };
@@ -72,7 +72,7 @@ describe('WebSocketService', () => {
7272

7373
expect(apolloMock.use).toHaveBeenCalledTimes(2);
7474
expect(apolloMock.use).toHaveBeenCalledWith(apolloClientName);
75-
expect(apolloMock.createNamed).not.toHaveBeenCalled();
75+
expect(apolloMock.createNamed).toHaveBeenCalled();
7676
});
7777

7878
it('should subscribe to Apollo client if not already in use', async () => {

lib/process-services-cloud/src/lib/services/web-socket.service.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,17 @@ export class WebSocketService {
6666
}
6767
});
6868

69-
if (this.apollo.use(apolloClientName) === undefined) {
70-
this.initSubscriptions(options);
71-
}
69+
this.removeApolloClientIfExists(apolloClientName);
70+
this.initSubscriptions(options);
7271
return this.apollo.use(apolloClientName).subscribe<T>({ errorPolicy: 'all', ...subscriptionOptions });
7372
}
7473

74+
private removeApolloClientIfExists(apolloClientName: string) {
75+
if (this.apollo.use(apolloClientName)) {
76+
this.apollo.removeClient(apolloClientName);
77+
}
78+
}
79+
7580
private get contextRoot() {
7681
return this.appConfigService.get('bpmHost', '');
7782
}
@@ -155,10 +160,13 @@ export class WebSocketService {
155160
this.wsLink = new GraphQLWsLink(
156161
createClient({
157162
url: this.createWsUrl(options.wsUrl) + '/v2/ws/graphql',
158-
connectionParams: {
163+
connectionParams: () => ({
159164
Authorization: 'Bearer ' + this.authService.getToken()
160-
},
165+
}),
161166
on: {
167+
closed: () => {
168+
this.removeApolloClientIfExists(options.apolloClientName);
169+
},
162170
error: () => {
163171
this.apollo.removeClient(options.apolloClientName);
164172
this.initSubscriptions(options);

0 commit comments

Comments
 (0)