import { Injectable } from '@angular/core' ; import { Apollo } from 'apollo-angular' ; import { Observable , throwError , } from 'rxjs' ; import { map , catchError , } from 'rxjs/operators' ; import { DaffCountry } from '@daffodil/geography' ; import { DaffGeographyServiceInterface } from '@daffodil/geography/driver' ; import { transformMagentoGeographyError } from './errors/transform' ; import { getCountry } from './queries/get-country' ; import { getCountries , MagentoGetCountriesResponse , } from './queries/public_api' ; import { MagentoGetCountryResponse } from './queries/responses/get-country' ; import { DaffMagentoCountryTransformer } from './transforms/responses/country.service' ; import { validateGetCountriesResponse } from './validators/public_api' ; inheritdoc @ Injectable ( { providedIn : 'root' , } ) export class DaffGeographyMagentoService implements DaffGeographyServiceInterface < DaffCountry > { constructor ( private apollo , public countryTransformer ) { } list ( ) { return this . apollo . query < MagentoGetCountriesResponse > ( { query : getCountries , } ) . pipe ( map ( validateGetCountriesResponse ) , map ( result => result . data . countries . map ( country => this . countryTransformer . transform ( country ) ) ) , catchError ( err => throwError ( transformMagentoGeographyError ( err ) ) ) ) ; } get ( countryId ) { return this . apollo . query < MagentoGetCountryResponse > ( { query : getCountry , variables : { countryId , } , } ) . pipe ( map ( result => this . countryTransformer . transform ( result . data . country ) ) , catchError ( err => throwError ( transformMagentoGeographyError ( err ) ) ) ) ; } }