I followed this answer to create a custom attribute on customer with graphQL. The query works great, but if I try to change the value with a mutation, it just returns null. Can anyone please help me with adding this functionality please? Code and example below:
schema.graphql:
type Customer {
sample_attribute: String @doc(description: "Customer Custom Attribute Show")
@resolver(
class: "\\Wrightway\\CustomGraphQl\\Model\\Resolver\\GetCustomerCustomAttr"
)
}
input CustomerInput {
sample_attribute: String @doc(description: "Customer Custom Attribute Val")
}
Model/Resolver/GetCustomerCustomAttr.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare (strict_types = 1);
namespace Wrightway\CustomGraphQl\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;
}
}
Query example (works great):
{
customer {
firstname
sample_attribute
}
}
returns:
{
"data": {
"customer": {
"firstname": "Adam",
"sample_attribute": null
}
}
}
Mutation example (doesn't work, returns null when attempted to change data):
mutation {
updateCustomer(
input: {
firstname: "Adam",
sample_attribute: "This is sample text"
}
) {
customer {
firstname
sample_attribute
}
}
}
returns (The added data, "This is sample text" returns null, and also returns null on the query.:
{
"data": {
"updateCustomer": {
"customer": {
"firstname": "Adam",
"sample_attribute": null
}
}
}
}
What do I need to add to make this work? Help much appreciated.