Skip to content

Commit

Permalink
AAE-20808 Implementing different approach to create http link handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ehsan-2019 committed Dec 4, 2024
1 parent 60c0803 commit a914029
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/process-services-cloud/src/lib/services/web-socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ import {
DefaultContext,
FetchResult,
from,
HttpLink,
InMemoryCache,
InMemoryCacheConfig,
NextLink,
Operation,
split,
SubscriptionOptions
} from '@apollo/client/core';
import { getMainDefinition } from '@apollo/client/utilities';
import { Kind, OperationTypeNode } from 'graphql';
import { Observable } from 'rxjs';
import { Apollo } from 'apollo-angular';
import { HttpLink, HttpLinkHandler } from 'apollo-angular/http';
import { Kind, OperationTypeNode } from 'graphql';
import { onError } from '@apollo/client/link/error';
import { RetryLink } from '@apollo/client/link/retry';
import { Observable } from 'rxjs';
import { getMainDefinition } from '@apollo/client/utilities';
import { filter, map, switchMap, take, tap } from 'rxjs/operators';
import { FeaturesServiceToken, IFeaturesService } from '@alfresco/adf-core/feature-flags';
import { AuthenticationService, AppConfigService } from '@alfresco/adf-core';
import { FeaturesServiceToken, IFeaturesService } from '@alfresco/adf-core/feature-flags';

interface serviceOptions {
apolloClientName: string;
Expand All @@ -55,10 +55,11 @@ export class WebSocketService {
private host = '';
private subscriptionProtocol: 'graphql-ws' | 'transport-ws' = 'transport-ws';
private wsLink: GraphQLWsLink | WebSocketLink;
private httpLink: HttpLink;
private httpLinkHandler: HttpLinkHandler;

constructor(
private readonly apollo: Apollo,
private readonly httpLink: HttpLink,
private readonly appConfigService: AppConfigService,
private readonly authService: AuthenticationService,
@Optional() @Inject(FeaturesServiceToken) private featuresService: IFeaturesService
Expand Down Expand Up @@ -117,19 +118,15 @@ export class WebSocketService {
throw new Error('Unknown subscription protocol');
}

this.httpLink = options.httpUrl
? new HttpLink({
uri: this.createHttpUrl(options.httpUrl)
})
: undefined;
this.createHttpLinkHandler(options);

const link = split(
({ query }) => {
const definition = getMainDefinition(query);
return definition.kind === Kind.OPERATION_DEFINITION && definition.operation === OperationTypeNode.SUBSCRIPTION;
},
this.wsLink,
this.httpLink
this.httpLinkHandler
);

const authLink = (operation: Operation, forward: NextLink) => {
Expand Down Expand Up @@ -179,7 +176,7 @@ export class WebSocketService {
});
}

private createTransportWsLink(options: serviceOptions) {
private createTransportWsLink(options: serviceOptions): void {
this.wsLink = new WebSocketLink({
uri: this.createWsUrl(options.wsUrl) + '/ws/graphql',
options: {
Expand All @@ -193,7 +190,7 @@ export class WebSocketService {
});
}

private createGraphQLWsLink(options: serviceOptions) {
private createGraphQLWsLink(options: serviceOptions): void {
this.wsLink = new GraphQLWsLink(
createClient({
url: this.createWsUrl(options.wsUrl) + '/v2/ws/graphql',
Expand All @@ -210,4 +207,12 @@ export class WebSocketService {
})
);
}

private createHttpLinkHandler(options: serviceOptions): void {
this.httpLinkHandler = options.httpUrl
? this.httpLink.create({
uri: this.createHttpUrl(options.httpUrl)
})
: undefined;
}
}

0 comments on commit a914029

Please sign in to comment.