Your IP : 216.73.217.95


Current Path : /home/deltalab/PMS/pim-connector/models/graphql/schemas/
Upload File :
Current File : //home/deltalab/PMS/pim-connector/models/graphql/schemas/product.js

const { schemaComposer } = require('graphql-compose');
const akeneoAdapter = require('../../../logic/akeneo/products');


// The return object Schema -- Modify this to update the model
class ProductSchema{
  sku         = 'String!';
  ean         = 'String!';
  title       = 'String!';
  description = 'String!';

  constructor(){}
}

// The actual GraphQL type used in the queries. Should always use the Schema class in the fields field.
// The constructor is used to set the fields types without hardcoding the schema
const productObj = schemaComposer.createObjectTC({
  name: 'Product',
  fields: new ProductSchema()
});

productObj.addResolver({
  kind: 'query',
  name: 'fetchDetails',
  type: productObj,
  args:{
    prodId:   { type: 'String!'},
  },
  resolve: async ({args}) => {
    const { prodId } = args; 
    let output = await akeneoAdapter.getProductDetails(prodId);
    if(!output){
      throw new Error("No product found");
    }
    return output;
  }
});

productObj.addResolver({
  kind: 'query',
  name: 'fetchSuggestions',
  type: [productObj],
  args:{
    hint:   {type: 'String!'},
    type:   {type: 'String!'},
  },
  resolve: async ({args})=>{
    const { hint, type } = args;
    const suggestions = await akeneoAdapter.getProductSuggestions(hint, type);
    if(suggestions===null){
      throw new Error("No product found");
    } else {
      return suggestions;
    }
  }
});

// QUERIES =============================
const productQueries = {
  fetchDetails:         productObj.getResolver('fetchDetails'),
  fetchSuggestions:     productObj.getResolver('fetchSuggestions'),
}


module.exports = {
  productQueries: productQueries,
  ProductModel: ProductSchema
};