We cannot get the results for custom_attributes with default products graphql query.
You want to create a custom module to achieve this.
We want to define, undefined(in Native GraphQl) products attribute in the di.xml.
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<virtualType name="Magento\Catalog\Model\Api\SearchCriteria\CollectionProcessor\ProductFilterProcessor" type="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
<arguments>
<argument name="customFilters" xsi:type="array">
<item name="language" xsi:type="object">
Ayakil\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ProductAttributeFilter
</item>
</argument>
</arguments>
</virtualType>
Here language is custom attribute.
After this we want to define the custom attribute as FilterTypeInput to ProductFilterInput in the shchema.graphqls file like below.
input ProductFilterInput {
language: FilterTypeInput @doc(description: "Product Language")
}
After this we want to create ProductAttributeFilter.php file under the Model directory. As we defined in the di.xml file.
<?php
declare(strict_types=1);
namespace Ayakil\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor\CustomFilterInterface;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\Api\Filter;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable;
use Magento\Framework\Registry;
/**
* Category filter allows to filter products collection using custom defined filters from search criteria.
*/
class ProductAttributeFilter implements CustomFilterInterface
{
protected $configurable;
protected $collectionFactory;
protected $registry;
public function __construct(
Configurable $configurable,
CollectionFactory $collectionFactory,
\Psr\Log\LoggerInterface $logger,
Registry $registry
) {
$this->registry = $registry;
$this->configurable = $configurable;
$this->logger = $logger;
$this->collectionFactory = $collectionFactory;
}
public function apply(Filter $filter, AbstractDb $collection)
{
$conditionType = $filter->getConditionType();
$attributeName = $filter->getField();
$attributeValue = $filter->getValue();
$category = $this->registry->registry('current_category');
if($attributeName == 'language'){
$conditions = [];
foreach ($attributeValue as $value){
$conditions[] = ['attribute'=>$attributeName, 'finset'=>$value];
}
$simpleSelect = $this->collectionFactory->create()
->addAttributeToFilter($conditions);
}else{
$simpleSelect = $this->collectionFactory->create()
->addAttributeToFilter($attributeName, [$conditionType => $attributeValue]);
}
$simpleSelect->addAttributeToFilter('status', Status::STATUS_ENABLED);
if ($category) {
$simpleSelect->addCategoriesFilter(['in' => (int)$category->getId()]);
}
$arr = $simpleSelect;
$entity_ids = [];
foreach ($arr->getData() as $a){
$entity_ids[] = $a['entity_id'];
}
$collection->getSelect()->where($collection->getConnection()->prepareSqlCondition(
'e.entity_id', ['in' => $entity_ids]
));
return true;
}
}
Now you can add this added custom product attribute language to Products graphql query.
To clearly read about this visit magento 2 product collection filter by attribute using graphql.
You can read more articles about graphql with these articles below.
1 . Magento get product attribute value by code, magento 2 get attribute value by id using graphql
2 . How to create graphql module in magento 2?
3 . magento 2 search criteria custom attribute using graphql
4 . Magento 2 graphql example for integrate contact us page functionality.