import { Injectable } from '@angular/core' ; import { Apollo , gql , } from 'apollo-angular' ; import { Observable , of , } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { daffArrayToDict } from '@daffodil/core' ; import { DaffProduct , DaffProductTypeEnum , } from '@daffodil/product' ; import { DaffProductDriverResponse , DaffProductServiceInterface , } from '@daffodil/product/driver' ; interface GetAllProductsResponse { shop ? : ShopGraph ; } interface GetAProductResponse { node : ProductNode ; } interface ShopGraph { products ? : ProductGraph ; } interface ProductGraph { edges : ProductEdge [ ] ; } interface ProductEdge { node : ProductNode ; } interface ProductNode { __typename ? : 'Product' ; id : string ; title ? : string ; price ? : string ; } interface Variables { first : number ; } ; /**
 * GraphQL query object for getting all products.
 */ export const GetAllProductsQuery = gql `
  query ShopifyGetAllProducts($length: Int) {
    shop {
      products(first: $length)  {
        edges {
          node {
            id
            title
          }
        }
      }
    }
  }
` ; /**
 * GraphQL query object for getting a product by ID.
 */ export const GetAProduct = gql `
  query ShopifyGetAProduct($id: ID!){
    node(id: $id) {
      id
      ... on Product {
        title
      }
    }
  }
` ; param node returns export const DaffShopifyProductTransformer = ( node ) : DaffProduct => ( { id : node . id , url : null , name : node . title , images : [ ] , thumbnail : null , type : DaffProductTypeEnum . Simple , price : null , discount : null , in_stock : true , } ) ; inheritdoc Param @ Injectable ( { providedIn : 'root' , } ) export class DaffShopifyProductService implements DaffProductServiceInterface { defaultLength = 20 ; constructor ( private apollo ) { } getByUrl ( url ) { return of ( { id : null , products : [ ] , } ) ; } getAll ( ) { return this . apollo . query < GetAllProductsResponse > ( { query : GetAllProductsQuery , variables : { length : this . defaultLength , } , } ) . pipe ( map ( result => result . data . shop . products . edges . map ( edge => DaffShopifyProductTransformer ( edge . node ) ) ) ) ; } getBestSellers ( ) { return this . apollo . query < GetAllProductsResponse > ( { query : GetAllProductsQuery , variables : { length : this . defaultLength , } , } ) . pipe ( map ( result => result . data . shop . products . edges . map ( edge => DaffShopifyProductTransformer ( edge . node ) ) ) ) ; } get ( productId ) { return this . apollo . query < GetAProductResponse > ( { query : GetAProduct , variables : { id : productId , } , } ) . pipe ( map ( result => ( { id : productId , products : [ DaffShopifyProductTransformer ( result . data . node ) ] , } ) ) ) ; } }