Magento 2.4
Try This Code
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;
}
}
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
}
}
}