import { Injectable } from '@angular/core'; import { Apollo } from 'apollo-angular'; import { jsonToGraphQLQuery } from 'json-to-graphql-query'; import gql from 'graphql-tag'; import { Utils } from './utils'; import { throwError } from 'rxjs'; import { catchError } from 'rxjs/operators'; import { LoaderService } from '../components/generic/loader/loader.service'; @Injectable({ providedIn: 'root', }) export class GraphQLService { constructor(private readonly apollo: Apollo, private loader: LoaderService) {} // fetch data from graphQL server query(query, variables = {}) { try { return this.apollo.query({ query, variables }).pipe( catchError(error => { // Utils.showError('GraphQLService.watchQuery()', error); return throwError(error); }) ); } catch (error) { Utils.showError('GraphQLService.query()', error); } } // mutate data to graphql server. i.e. edit, delete, create mutate(mutation, variables = {}) { try { return this.apollo.mutate({ mutation, variables }).pipe( catchError(error => { // Utils.addLog("error", 'GraphQLService.mutate()', error); return throwError(error); }) ); } catch (error) { Utils.addLog("error", 'GraphQLService.mutate()', error); } } subscribe(query) { try { query = { subscription: query }; query = jsonToGraphQLQuery(query, { pretty: true }); return this.apollo.subscribe(query); } catch (error) { Utils.showError('subscribe() ', error); } } // upload files to graphql server. upload(query, variables) { try { return this.apollo.mutate({ mutation: gql(query), variables, context: { useMultipart: true, }, }); } catch (error) { Utils.showError('upload()', error); } } }