I have created a custom graphQL to get the recently viewed products.
I used the following code .
<?php
class RecentlyViewed implements Magento\Framework\GraphQl\Query\ResolverInterface
{
protected $appEmulation;
protected $recentProducts;
public function __construct(
\Magento\Reports\Block\Product\Viewed $recentProducts,
\Magento\Store\Model\App\Emulation $appEmulation,
) {
$this->appEmulation = $appEmulation;
$this->recentProducts= $recentProducts;
}
public function resolve(
\Magento\Framework\GraphQl\Schema\Type\ResolveInfo $info,
array $value=null,
array $args=null
) {
$this->appEmulation->startEnvironmentEmulation(1, \Magento\Framework\App\Area::AREA_FRONTEND, true);
$collection = $this->recentProducts->getItemsCollection()->load();
$this->appEmulation ->stopEnvironmentEmulation();
return $collection->getData();
}
}
This is returning empty collection. Can anyone suggest me how to get the recently viewed collection in graphQL. Thanks in Advance!.