Skip to content

How to change custom customer attributes with GraphQL mutation?

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 WrightwayCustomGraphQlModelResolver;

use MagentoCustomerApiDataCustomerInterface;
use MagentoFrameworkExceptionLocalizedException;
use MagentoFrameworkGraphQlConfigElementField;
use MagentoFrameworkGraphQlQueryResolverInterface;
use MagentoFrameworkGraphQlSchemaTypeResolveInfo;

/**
 * Customer custom attribute field resolver
 */
class GetCustomerCustomAttr implements ResolverInterface
{
    /**
     * @var MagentoCustomerApiCustomerRepositoryInterface
     */
    protected $customerRepositoryInterface;

    /**
     * @param MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface
     */
    public function __construct(
        MagentoCustomerApiCustomerRepositoryInterface $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.