dependencies Legend  Declarations  Module  Bootstrap  Providers  Exports cluster_GqlModule cluster_GqlModule_imports cluster_GqlModule_providers HttpClientModule HttpClientModule GqlModule GqlModule HttpClientModule->GqlModule ToastModule ToastModule ToastModule->GqlModule GqlService GqlService GqlService->GqlModule

File

projects/commons/src/lib/core/gql/gql.module.ts

Methods

Private errorMessage
errorMessage(message)
Parameters :
Name Optional
message No
Returns : void
Private handleOnError
handleOnError()
Returns : any
Private openToast
openToast(message: string)
Parameters :
Name Type Optional
message string No
Returns : void
import { NgModule } from '@angular/core';
import { ApolloLink } from 'apollo-link';
import { onError } from 'apollo-link-error';
import { Apollo, ApolloModule } from 'apollo-angular';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { HttpClientModule } from '@angular/common/http';
import { HttpLink, HttpLinkModule } from 'apollo-angular-link-http';

import { GqlService } from './services';
import { colorsEnum } from '../../shared/enums';
import { environment } from '../../../environments/environment';
import { ToastModule, toastPositionEnum, ToastService } from '../../elements/toast';

@NgModule({
    imports: [
        ApolloModule,
        HttpLinkModule,
        HttpClientModule,

        ToastModule,
    ],
    providers: [
        GqlService,
    ],
})

export class GqlModule {
    constructor(
        private readonly apollo: Apollo,
        private readonly httpLink: HttpLink,
        private readonly toastService: ToastService,
    ) {
        const errorLink = this.handleOnError();
        const httpLinked = this.httpLink.create({ uri: environment.uri });
        const httpLinkWithErrorHandling = ApolloLink.from([ errorLink, httpLinked, ]);

        this.apollo.create({
            cache: new InMemoryCache(),
            link: httpLinkWithErrorHandling,
        });
    }

    private handleOnError() {
        return onError(({ graphQLErrors, networkError }) => {
            if (graphQLErrors) {
                graphQLErrors.map(({ message }) => this.errorMessage(message));
            }

            if (networkError) {
                this.errorMessage(networkError.message);
            }
        });
    }

    private errorMessage(message): void {
        if (environment.production) {
            message = 'Error: Cable crossing';
        }

        this.openToast(message)
    }

    private openToast(message: string): void {
        this.toastService.open({
            message,
            dismissible: false,
            type: colorsEnum.error,
            position: toastPositionEnum.topCenter,
        });
    }
}

result-matching ""

    No results matching ""