I need to get all attribute values of product based on the category id. I had implemented this logic in rest api but I couldn't find clear solution for graphql module.
My schema.graphqls file is below
type Query {
productCollection (
filter: Int,
): ProductCollection @resolver(class: "Rbj\\ProductsGraphQl\\Model\\Resolver\\ProductsResolver") @doc(description: "Get Product collection of a store")
}
type ProductCollection @doc(description: "product collection comment") {
allProducts: [ProductRecord] @doc(description: "Product records with info")
}
type ProductRecord {
display: String @doc(description: "Get Product name")
value: String @doc(description: "Get Product image")
count: String @doc(description: "Get cat level")
}
My ProductResolver model is below
<?php
declare(strict_types=1);
namespace Rbj\ProductsGraphQl\Model\Resolver;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
/**
* Product collection resolver
*/
class ProductsResolver implements ResolverInterface
{
public function __construct(
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
) {
$this->productRepository = $productRepository;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null,
int $id
) {
$productsData = $this->getProductsData($id);
return $productsData;
}
/**
* @return array
* @throws GraphQlNoSuchEntityException
*/
private function getProductsData($id)
{
$category = $id;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$filterableAttributes = $objectManager->getInstance()->get(\Magento\Catalog\Model\Layer\Category\FilterableAttributeList::class);
$appState = $objectManager->getInstance()->get(\Magento\Framework\App\State::class);
$layerResolver = $objectManager->getInstance()->get(\Magento\Catalog\Model\Layer\Resolver::class);
$filterList = $objectManager->getInstance()->create(
\Magento\Catalog\Model\Layer\FilterList::class,
[
'filterableAttributes' => $filterableAttributes
]
);
$layer = $layerResolver->get();
$layer->setCurrentCategory($category);
$filters = $filterList->getFilters($layer);
$maxPrice = $layer->getProductCollection()->getMaxPrice();
$minPrice = $layer->getProductCollection()->getMinPrice();
$i = 0;
foreach($filters as $filter)
{
//$availablefilter = $filter->getRequestVar(); //Gives the request param name such as 'cat' for Category, 'price' for Price
$availablefilter = (string)$filter->getName(); //Gives Display Name of the filter such as Category,Price etc.
$items = $filter->getItems(); //Gives all available filter options in that particular filter
$filterValues = array();
$j = 0;
foreach($items as $item)
{
$filterValues[$j]['display'] = strip_tags($item->getLabel());
$filterValues[$j]['value'] = $item->getValue();
$filterValues[$j]['count'] = $item->getCount(); //Gives no. of products in each filter options
$j++;
}
$filterArray['availablefilter'][$availablefilter] = $filterValues;
$i++;
}
return $filterArray;
exit;
}
My Graphql query is below
{
productCollection(filter:2){
allProducts {
display
}
}
}
The above php code returns me array for rest api but returns this below error when I tried to integrate with Graphql
"{\"messages\":{\"error\":[{\"code\":500,\"message\":\"Fatal Error: 'syntax error, unexpected end of file, expecting function (T_FUNCTION) or const (T_CONST)' in '\\/var\\/www\\/html\\/motomag\\/app\\/code\\/Rbj\\/ProductsGraphQl\\/Model\\/Resolver\\/ProductsResolver.php' on line 94\",\"trace\":\"Trace is not available.\"}]}}{\"messages\":{\"error\":[{\"code\":500,\"message\":\"Fatal Error: 'syntax error, unexpected end of file, expecting function (T_FUNCTION) or const (T_CONST)' in '\\/var\\/www\\/html\\/motomag\\/app\\/code\\/Rbj\\/ProductsGraphQl\\/Model\\/Resolver\\/ProductsResolver.php' on line 94\",\"trace\":\"Trace is not available.\"}]}}"