Create a extension attribute for the required datainterface here I'm providing sample:
<extension_attributes for="Magento\Store\Api\Data\StoreConfigInterface">
<attribute code="sample" type="string"/>
</extension_attributes>
Have a plugin on the interface di.xml
<type name="Magento\Store\Api\StoreConfigManagerInterface">
<plugin name="nameofplugin"
type="Namespace\Modulename\Plugin\filename"/>
</type>
Plugin class
<?php
namespace Emc\App\Plugin;
use Magento\Store\Api\Data\StoreConfigExtensionInterface;
use Magento\Store\Api\Data\StoreConfigInterface;
use Magento\Store\Api\Data\StoreConfigExtensionFactory;
use Magento\Setup\Exception;
use Magento\Directory\Model\Currency;
use Magento\Store\Model\StoreManagerInterface;
/**
* Class classname
*/
class classname
{
protected $storeConfigExtensionFactory;
protected $storeConfigInterface;
protected $storeConfigExtensionInterface;
protected $scopeConfig;
/**
* Product constructor.
* @param \Magento\Catalog\Model\ProductRepository $productRepository
*/
// objects need to be changed based on your requirement
public function __construct(
Currency $currencyFactory,
StoreManagerInterface $storeManager,
StoreConfigExtensionFactory $storeConfigExtensionFactory,
StoreConfigInterface $storeConfigInterface,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
) {
$this->storeConfigExtensionFactory = $storeConfigExtensionFactory;
$this->storeConfigInterface = $storeConfigInterface;
$this->scopeConfig = $scopeConfig;
$this->currencyFactory = $currencyFactory;
$this->storeManager = $storeManager;
}
/**
* @param string[] $storeCodes list of stores by store codes, will return all if storeCodes is not set
* @return \Magento\Store\Api\Data\StoreConfigInterface[]
*/
public function aroundGetStoreConfigs(\Magento\Store\Model\Service\StoreConfigManager $storeConfigManager, \Closure $proceed, array $storeCodes = null)
{
$result = $proceed($storeCodes);
foreach ($result as $storevalues) {
$storeconfigExtension = $this->storeConfigExtensionFactory->create();
->getCurrencySymbol());
$storeconfigExtension->setSample($storedKey); //attribute code
$storevalues->setExtensionAttributes($storeconfigExtension);
}
return $result;
}
}
You can use native products graphql query by define the custom products attributes.Hope this may help some one.
This was little bit old question and you might found any solution.
I Have done the same in my recent project.
For this i created a custom module.
1.Create custom module.
2.Define the attributes in di.xml.
3.Add the attributes to ProductFilterInput.
4.Create the ProductAttributeFilter.php file in the relevant directory as defined in di.xml.
5.Run the graphql query to get the result.
My di.xml file looks like below
<?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="color" xsi:type="object">
Ayakil\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ProductAttributeFilter
</item>
<item name="size" xsi:type="object">
Ayakil\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ProductAttributeFilter
</item>
<item name="region" xsi:type="object">
Ayakil\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ProductAttributeFilter
</item>
<item name="language" xsi:type="object">
Ayakil\CatalogGraphQl\Model\Resolver\Products\SearchCriteria\CollectionProcessor\FilterProcessor\ProductAttributeFilter
</item>
</argument>
</arguments>
</virtualType>
Add the attributes to ProductFilterInput
input ProductFilterInput {
category_url_key: FilterTypeInput @doc(description: "Category url_key the product belongs to")
category_url_path: FilterTypeInput @doc(description: "Category url_path the product belongs to")
color: FilterTypeInput @doc(description: "Product color")
size: FilterTypeInput @doc(description: "Product size")
region: FilterTypeInput @doc(description: "Product Region")
language: FilterTypeInput @doc(description: "Product Language")
url_key: FilterTypeInput @doc(description: "Product url")
}
Create the ProductAttributeFilter.php file in the relevant directory as defined in di.xml.
<?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;
}
}
After this want to pass the added attribute to products graphQl query to get the result.
I have written an article How to filter product collection with default graphql products query?
Hope this will help you to build up your task.
My query and answer as images
Best Answer
All you have to do is re-define the 'CustomerInput' parameter (with your custom extension attribute) of the createCustomer mutation.
In your module's schema.graphqls file, re-define the 'CustomerInput' type.
Then run the below commands.
rm -rf generation/
bin/magento c:f
Now you can parse this new parameter in createCuatomer mutation. See the example below.