I created an attribute following your provided example and incorporated customizations into the product query. Please add the code below to your module and check the product query.
app/code/VendorName/ModuleName/etc
schema.graphqls
interface ProductInterface {
is_outlet_available: [String] @doc(description: "Product Custom Attribute Show")
@resolver(
class: "VendorName\\ModuleName\\Model\\Resolver\\Product\\AddCustomAttribute"
)
}
app/code/VendorName/ModuleName/Model/Resolver/Product
AddCustomAttribute.php
<?php
namespace VendorName\ModuleName\Model\Resolver\Product;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
class AddCustomAttribute implements ResolverInterface
{
protected $productFactory;
protected $attributeOptionsManagement;
public function __construct(
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Eav\Api\AttributeOptionManagementInterface $attributeOptionsManagement
) {
$this->productFactory = $productFactory;
$this->attributeOptionsManagement = $attributeOptionsManagement;
}
public function resolve(
\Magento\Framework\GraphQl\Config\Element\Field $field,
$context,
\Magento\Framework\GraphQl\Schema\Type\ResolveInfo $info,
array $value = null,
array $args = null
) {
$product = $value['model'];
$_product = $this->productFactory->create()->load($product->getId());
$optionData = explode(',', $_product->getData('is_outlet_available') ?? '');
$attributeOptions = $this->attributeOptionsManagement->getItems(ProductAttributeInterface::ENTITY_TYPE_CODE, 'is_outlet_available');
$featuredOptions = [];
if (count($optionData)) {
foreach ($attributeOptions as $attributeOption) {
if (in_array($attributeOption->getValue(), $optionData)) {
$featuredOptions[$attributeOption->getValue()] = $attributeOption->getLabel();
}
}
}
return $featuredOptions;
}
}
Product Query with your attribute value:
{
products(filter: { sku: { eq: "YOUR_SKU" } }) {
items {
name
sku
url_key
is_outlet_available
}
}
}
OUTPUT:
Now, the value is available within the product data, allowing you to directly check it in your Headless React code.