Graphql Mutation for customer update in magento 2
Update customer by graphql Mutation. With existing fields, If you want to update your customer field you need to add that field in schema.graphqls input CustomerInput
GraphQL mutation is used for updating data; it is similar to POST API. You can run a customer update mutation that needs a customer token.
In header you can set the customer Barrar Token "Authorization: Bearer bvnqda5g61zt3w6gxxxo0t222ubgcj4k"
mutation{ updateCustomer(input: { email:"ajeets@gamil.com" firstname: "Ajeet" }) { customer { email firstname } } }
Note: If you want to add a custom attribute in the core customer GraphQL query.
Then you need to follow a few steps.
1. In your vendor module etc. dir, create a schema.graphqls file. I'm adding a mobile number in the customer query.
type Customer { mobile: String @resolver(class: "\\Vendor\\CustomerGraphQl\\Model\\Resolver\\GetMobile") }
After creating schema.graphqls.
2. Now need to create resolver class which provide the data of mobile number from customer
<?php namespace Vendor\CustomerGraphQl\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 GetMobile implements ResolverInterface { /** * @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']; if ($customer->getCustomAttribute('mobile')) { $customerAttributeVal = $customer->getCustomAttribute('mobile')->getValue(); } else { $customerAttributeVal = null; } return $customerAttributeVal; } }