Magento 2.4
I will show you how to add custom attributes into product and customer query and mutation
1) Add custom attribute into customer query and mutation
app/code/VendoreName/ModuleName/etc
schema.graphqls
type Customer {
sample_attribute: String @doc(description: "Customer Custom Attribute Show")
@resolver(
class: "\\VendoreName\\ModuleName\\Model\\Resolver\\GetCustomerCustomAttr"
)
}
input CustomerInput {
sample_attribute: String @doc(description: "Customer Custom Attribute Val")
}
app/code/VendoreName/ModuleName/Model/Resolver
GetCustomerCustomAttr.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare (strict_types = 1);
namespace VendoreName\ModuleName\Model\Resolver;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
/**
* Customer custom attribute field resolver
*/
class GetCustomerCustomAttr implements ResolverInterface
{
/**
* @var Magento\Customer\Api\CustomerRepositoryInterface
*/
protected $customerRepositoryInterface;
/**
* @param Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
*/
public function __construct(
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
) {
$this->customerRepositoryInterface = $customerRepositoryInterface;
}
/**
* @inheritdoc
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var CustomerInterface $customer */
$customer = $value['model'];
$customerId = (int) $customer->getId();
$customerData = $this->customerRepositoryInterface->getById($customerId);
/* Get customer custom attribute value */
if ($customer->getCustomAttribute('sample_attributess')) {
$customerAttributeVal = $customer->getCustomAttribute('sample_attributess')->getValue();
} else {
$customerAttributeVal = null;
}
return $customerAttributeVal;
}
}
Run Magento Commands:
php bin/magento s:up
php bin/magento in:rein
php bin/magento s:s:d -f
php bin/magento c:c
Get customer custom attribute value
{
customer {
email
sample_attribute
}
}
Update customer custom attribute value
mutation {
updateCustomer(
input: {
firstname: "Rob",
sample_attribute: "Text"
}
) {
customer {
firstname
sample_attribute
}
}
}
2)Filter, sort, and get product custom attribute in product query
Create Custom Attribute with below options values.
app/code/VendoreName/ModuleName/etc
schema.graphqls
interface ProductInterface {
sample_attribute: String @doc(description: "Product Custom Attribute Show")
@resolver(
class: "VendoreName\\ModuleName\\Model\\Resolver\\Product\\AddCustomAttribute"
)
}
input ProductAttributeFilterInput {
sample_attribute: FilterEqualTypeInput @doc(description: "Product Data filter with Custom Attribute Value")
}
input ProductAttributeSortInput {
sample_attribute: SortEnum @doc(description: "Sort by the Product Custom Attribute.")
}
app/code/VendoreName/ModuleName/Model/Resolver/Product
AddCustomAttribute.php
<?php
namespace VendoreName\ModuleName\Model\Resolver\Product;
use Magento\Framework\GraphQl\Query\ResolverInterface;
/**
* Product custom attribute field resolver
*/
class AddCustomAttribute implements ResolverInterface
{
/**
* @var Magento\Catalog\Model\ProductFactory
*/
protected $productFactory;
/**
* @param Magento\Catalog\Model\ProductFactory $productFactory
*/
public function __construct(
\Magento\Catalog\Model\ProductFactory $productFactory
) {
$this->productFactory = $productFactory;
}
/**
* @inheritdoc
*/
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());
return $_product->getData('sample_attribute');
}
}
Run Magento Commands:
php bin/magento s:up
php bin/magento in:rein
php bin/magento s:s:d -f
php bin/magento c:c
For show product custom attribute value
{
products(filter: { sku: { eq: "24-MB01" } }) {
items {
name
sku
sample_attribute
}
}
}
For filter product data using custom attribute
{
products(filter: { sample_attribute: { eq: "test" } }) {
items {
name
sku
sample_attribute
}
}
}
For sort product data using custom attribute
{
products(search: "bag", sort: {sample_attribute: DESC}) {
items {
name
sku
sample_attribute
}
}
}