0

I have created Rest API for getting product collection for learning purpose , I wanted to convert it in GraphQl Api. My question is below code valid for GraphQl or there is something else? My rest APi code:

<?php
namespace Kanika\Myapi\Model;

use Magento\Framework\Exception\InputException; 
class Productapi implements \Kanika\Myapi\Api\ProductapiInterface
{

     protected $_productCollection;
     public function __construct(           
             \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $_productCollection
        ) {        

            $this->_productCollection = $_productCollection;           
        }
     public function getlist($category_ids,$is_featured,$special_price,$brands,$product_type,$page=1,$pagesize=10){
        $collection = $this->_productCollection->create();
        $collection->addAttributeToSelect('name');
        $collection->addAttributeToSelect('price');
        if(!empty($category_ids)){
            $collection->addCategoriesFilter(['in' =>[$category_ids]]);
        }
        if($is_featured==1){
            $collection->addAttributeToFilter('is_featuredproduct', array('eq' => 1));
        }
        if($special_price==1){
            $todayDate = date('m/d/y');
            $tomorrow = mktime(0, 0, 0, date('m'), date('d')+1, date('y'));
            $tomorrowDate = date('m/d/y', $tomorrow);

            $collection->addAttributeToFilter('special_from_date', array('date' => true, 'to' => $todayDate))
                ->addAttributeToFilter(
                        'special_to_date', array('or'=> array(
                        0 => array('date' => true, 'from' => $tomorrowDate),
                        1 => array('is' => new \Zend_Db_Expr('null')))
                        ), 'left'
                    );
        }    
        if($product_type=='bundle'){ // bundle | simple | configruable
            $collection->addAttributeToFilter('type_id','bundle');
        }
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
        $data=[];
        $collection->setPageSize($pagesize)->setCurPage($page)->load();
        foreach ($collection as $product) {
           $data[] = [
                'id'=>$product->getId(),
                'sku'=>$product->getSku(),
                'name'=>$product->getName(),
                'brand'=>$product->getBrand(),
                'price'=>$product->getPrice(),
                'special_price'=>$product->getSpecialPrice(),
                'product_type'=>$product->getTypeId()
           ];
        }
        $pagination=[
            'current_page'=>$page,
            'last_page'=>$collection->getLastPageNumber(),
            #'total_product'=>$collection->count()
        ];
        return [
            'items'=>$data,
            'pagination'=>$pagination
        ];
    }
}
2

1 Answer 1

0

You have to create schema.graphqls and resolver class to implement this graphql and some changes too. create a resolver class with graphql exception handling & some part of above code would definitely work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.