Skip to content

Commit

Permalink
AAE-20808 using new GraphQL library
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-2019 committed Dec 6, 2024
1 parent 9faea5c commit 65936d3
Show file tree
Hide file tree
Showing 7 changed files with 400 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,12 @@
import { TestBed } from '@angular/core/testing';
import { ProcessServiceCloudTestingModule } from '../testing/process-service-cloud.testing.module';
import { NotificationCloudService } from './notification-cloud.service';
import { Apollo } from 'apollo-angular';
import { provideMockFeatureFlags } from '@alfresco/adf-core/feature-flags';
import { WebSocketService } from './web-socket.service';

describe('NotificationCloudService', () => {
let service: NotificationCloudService;
let apollo: Apollo;
let apolloCreateSpy: jasmine.Spy;
let apolloSubscribeSpy: jasmine.Spy;

const useMock: any = {
subscribe: () => {}
};
let wsService: WebSocketService;

const queryMock = `
subscription {
Expand All @@ -43,39 +38,25 @@ describe('NotificationCloudService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
imports: [ProcessServiceCloudTestingModule]
imports: [ProcessServiceCloudTestingModule],
providers: [WebSocketService, provideMockFeatureFlags({ ['studio-ws-graphql-subprotocol']: false })]
});
service = TestBed.inject(NotificationCloudService);
apollo = TestBed.inject(Apollo);

service.appsListening = [];
apolloCreateSpy = spyOn(apollo, 'createNamed');
apolloSubscribeSpy = spyOn(apollo, 'use').and.returnValue(useMock);
wsService = TestBed.inject(WebSocketService);
});

it('should not create more than one websocket per app if it was already created', () => {
service.makeGQLQuery('myAppName', queryMock);
expect(service.appsListening.length).toBe(1);
expect(service.appsListening[0]).toBe('myAppName');

service.makeGQLQuery('myAppName', queryMock);
expect(service.appsListening.length).toBe(1);
expect(service.appsListening[0]).toBe('myAppName');

expect(apolloCreateSpy).toHaveBeenCalledTimes(1);
expect(apolloSubscribeSpy).toHaveBeenCalledTimes(2);
});
it('should call getSubscription with the correct parameters', () => {
const getSubscriptionSpy = spyOn(wsService, 'getSubscription').and.callThrough();

it('should create new websocket if it is subscribing to new app', () => {
service.makeGQLQuery('myAppName', queryMock);
expect(service.appsListening.length).toBe(1);
expect(service.appsListening[0]).toBe('myAppName');

service.makeGQLQuery('myOtherAppName', queryMock);
expect(service.appsListening.length).toBe(2);
expect(service.appsListening[1]).toBe('myOtherAppName');

expect(apolloCreateSpy).toHaveBeenCalledTimes(2);
expect(apolloSubscribeSpy).toHaveBeenCalledTimes(2);
expect(getSubscriptionSpy).toHaveBeenCalledWith({
apolloClientName: 'myAppName',
wsUrl: 'myAppName/notifications',
httpUrl: 'myAppName/notifications/graphql',
subscriptionOptions: {
query: jasmine.any(Object)
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,101 +15,23 @@
* limitations under the License.
*/

import { Apollo } from 'apollo-angular';
import { HttpLink } from 'apollo-angular/http';
import { split, gql, InMemoryCache, ApolloLink, InMemoryCacheConfig } from '@apollo/client/core';
import { WebSocketLink } from '@apollo/client/link/ws';
import { onError } from '@apollo/client/link/error';
import { getMainDefinition } from '@apollo/client/utilities';
import { gql } from '@apollo/client/core';
import { Injectable } from '@angular/core';
import { AuthenticationService } from '@alfresco/adf-core';
import { BaseCloudService } from './base-cloud.service';
import { AdfHttpClient } from '@alfresco/adf-core/api';

import { WebSocketService } from './web-socket.service';
@Injectable({
providedIn: 'root'
})
export class NotificationCloudService extends BaseCloudService {
appsListening = [];

constructor(public apollo: Apollo, private http: HttpLink, private authService: AuthenticationService, protected adfHttpClient: AdfHttpClient) {
super(adfHttpClient);
}

private get webSocketHost() {
return this.contextRoot.split('://')[1];
}

private get protocol() {
return this.contextRoot.split('://')[0] === 'https' ? 'wss' : 'ws';
}

initNotificationsForApp(appName: string) {
if (!this.appsListening.includes(appName)) {
this.appsListening.push(appName);
const httpLink = this.http.create({
uri: `${this.getBasePath(appName)}/notifications/graphql`
});

const webSocketLink = new WebSocketLink({
uri: `${this.protocol}://${this.webSocketHost}/${appName}/notifications/ws/graphql`,
options: {
reconnect: true,
lazy: true,
connectionParams: {
kaInterval: 2000,
// eslint-disable-next-line @typescript-eslint/naming-convention
'X-Authorization': 'Bearer ' + this.authService.getToken()
}
}
});

const link = split(
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
},
webSocketLink,
httpLink
);

const errorLink = onError(({ graphQLErrors, operation, forward }) => {
if (graphQLErrors) {
for (const err of graphQLErrors) {
switch (err.extensions.code) {
case 'UNAUTHENTICATED': {
const oldHeaders = operation.getContext().headers;
operation.setContext({
headers: {
...oldHeaders,
// eslint-disable-next-line @typescript-eslint/naming-convention
'X-Authorization': 'Bearer ' + this.authService.getToken()
}
});
forward(operation);
break;
}
default:
break;
}
}
}
});

this.apollo.createNamed(appName, {
link: ApolloLink.from([errorLink, link]),
cache: new InMemoryCache({ merge: true } as InMemoryCacheConfig),
defaultOptions: {
watchQuery: {
errorPolicy: 'all'
}
}
});
}
}
export class NotificationCloudService {
constructor(private readonly webSocketService: WebSocketService) {}

makeGQLQuery(appName: string, gqlQuery: string) {
this.initNotificationsForApp(appName);
return this.apollo.use(appName).subscribe({ query: gql(gqlQuery) });
return this.webSocketService.getSubscription({
apolloClientName: appName,
wsUrl: `${appName}/notifications`,
httpUrl: `${appName}/notifications/graphql`,
subscriptionOptions: {
query: gql(gqlQuery)
}
});
}
}
1 change: 1 addition & 0 deletions lib/process-services-cloud/src/lib/services/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ export * from './form-fields.interfaces';
export * from './base-cloud.service';
export * from './task-list-cloud.service.interface';
export * from './variable-mapper.sevice';
export * from './web-socket.service';
134 changes: 134 additions & 0 deletions lib/process-services-cloud/src/lib/services/web-socket.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*!
* @license
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { TestBed } from '@angular/core/testing';
import { Apollo, gql } from 'apollo-angular';
import { of, Subject } from 'rxjs';
import { WebSocketService } from './web-socket.service';
import { SubscriptionOptions } from '@apollo/client/core';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AuthenticationService, AppConfigService } from '@alfresco/adf-core';
import { FeaturesServiceToken, IFeaturesService, provideMockFeatureFlags } from '@alfresco/adf-core/feature-flags';

describe('WebSocketService', () => {
let service: WebSocketService;
let featureService: IFeaturesService;
const onLogoutSubject: Subject<void> = new Subject<void>();

const apolloMock = jasmine.createSpyObj('Apollo', ['use', 'createNamed']);

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{
provide: Apollo,
useValue: apolloMock
},
{
provide: AppConfigService,
useValue: {
get: () => 'wss://testHost'
}
},
{
provide: AuthenticationService,
useValue: {
getToken: () => 'testToken',
onLogout: onLogoutSubject.asObservable()
}
},
provideMockFeatureFlags({ ['studio-ws-graphql-subprotocol']: true })
]
});
service = TestBed.inject(WebSocketService);
featureService = TestBed.inject(FeaturesServiceToken);
apolloMock.use.and.returnValues(undefined, { subscribe: () => of({}) });
});

afterEach(() => {
apolloMock.use.calls.reset();
apolloMock.createNamed.calls.reset();
});

it('should not create a new Apollo client if it is already in use', (done) => {
const apolloClientName = 'testClient';
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) };
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions };

apolloMock.use.and.returnValues(true, { subscribe: () => of({}) });

service.getSubscription(wsOptions).subscribe(() => {
expect(apolloMock.use).toHaveBeenCalledTimes(2);
expect(apolloMock.use).toHaveBeenCalledWith(apolloClientName);
expect(apolloMock.createNamed).not.toHaveBeenCalled();
done();
});
});

it('should subscribe to Apollo client if not already in use', (done) => {
const apolloClientName = 'testClient';
const expectedApolloClientName = 'testClient';
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) };
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions };

service.getSubscription(wsOptions).subscribe(() => {
expect(apolloMock.use).toHaveBeenCalledWith(expectedApolloClientName);
expect(apolloMock.use).toHaveBeenCalledTimes(2);
expect(apolloMock.createNamed).toHaveBeenCalledTimes(1);
expect(apolloMock.createNamed).toHaveBeenCalledWith(expectedApolloClientName, jasmine.any(Object));
done();
});
});

it('should create named client with the right authentication token when FF is on', (done) => {
let headers = {};
const expectedHeaders = { Authorization: 'Bearer testToken' };
const apolloClientName = 'testClient';
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) };
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions };
apolloMock.createNamed.and.callFake((_, options) => {
headers = options.headers;
});

service.getSubscription(wsOptions).subscribe(() => {
expect(apolloMock.use).toHaveBeenCalledTimes(2);
expect(apolloMock.createNamed).toHaveBeenCalled();
expect(headers).toEqual(expectedHeaders);
done();
});
});

it('should create named client with the right authentication token when FF is off', (done) => {
featureService.getFlags$ = jasmine.createSpy().and.returnValue(of({ 'studio-ws-graphql-subprotocol': { current: false } }));
let headers = {};
const expectedHeaders = { 'X-Authorization': 'Bearer testToken' };
const apolloClientName = 'testClient';
const subscriptionOptions: SubscriptionOptions = { query: gql(`subscription {testQuery}`) };
const wsOptions = { apolloClientName, wsUrl: 'testUrl', subscriptionOptions };
apolloMock.createNamed.and.callFake((_, options) => {
headers = options.headers;
});

service.getSubscription(wsOptions).subscribe(() => {
expect(apolloMock.use).toHaveBeenCalledTimes(2);
expect(apolloMock.createNamed).toHaveBeenCalled();
expect(headers).toEqual(expectedHeaders);
done();
});
});
});
Loading

0 comments on commit 65936d3

Please sign in to comment.