Create customer By Graphql Mutation
We can create custom mutation for create customer by graphql. For that we can create custom request and response query string.
Step 1. Create a schema.graphqls file in your module's etc directory.
type Mutation { createCustomerAccount( id: String @doc(description: "Id of requested resource") firstname: String! @doc(description: "The customer's name") lastname: String @doc(description: "The customer's family name") phone: String! @doc(description: "The customer's Phone number") email: String! @doc(description: "The customer's email address. Required") password: String @doc(description: "The customer's password") gender: String @doc(description: "The customer's gender") registrationchannel: Channel @doc(description: "The customer's registration channel") ): CreateCustomerResponse @resolver(class: "\\Vendor\\CustomerLogin\\Model\\Resolver\\CreateCustomer") @doc(description:"Create customer account") } enum Channel { WEB SOCIAL APP } type CreateCustomerResponse { status: Boolean @doc(description: "Request Status") message: String @doc(description: "Success Message") }
Step 2. No need to create resolver class, which have you log in to creating customer
<?php namespace Vendor\CustomerLogin\Model\Resolver; use Magento\Framework\GraphQl\Config\Element\Field; use Magento\Framework\GraphQl\Exception\GraphQlInputException; use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; use Magento\Framework\GraphQl\Query\ResolverInterface; use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; use Magento\Framework\Math\Random; use Psr\Log\LoggerInterface; /** * Create Customer field resolver, used for GraphQL request processing */ class CreateCustomer implements ResolverInterface { /** * @var Random */ private $mathRandom; /** * @param Random $mathRandom * @param LoggerInterface $logger */ public function __construct( Random $mathRandom, LoggerInterface $logger ) { $this->mathRandom = $mathRandom; $this->logger = $logger; } /** * @inheritdoc */ public function resolve( Field $field, $context, ResolveInfo $info, array $value = null, array $args = null ) { $this->vaildateArgs($args); $id = ''; if (isset($args['id'])) { $id = $args['id']; } try { return ['status' => 1, 'message' => "", 'data' => ""]; } catch (\Exception $e) { throw new GraphQlAuthenticationException(__($e->getMessage()), $e); } } /** * Vaildate the arguments * * @param array $args * @throws GraphQlInputException */ private function vaildateArgs(array $args): void { if (empty($args['firstname'])) { throw new GraphQlInputException(__('Specify the "name" value.')); } } }
Step 3. This mutation query your can run in by postman
mutation{ createCustomerAccount( firstname: "Ajeet" lastname: "Singh" email:"ajeet1s@razorpay.com" phone:"8303523417" gender: "1" registrationchannel:WEB ) { message status } }