We are going to explain, Custom GraphQL Query Module to fetch the Customer’s Data in Magento 2.x to retrieve basic information of the customer entity as below
email
firstname
lastname
gender
dob
created_at
There are following below steps need to follow.
Customer GraphQL Query Module by using custom module
Here, we are considering as Namespace / ModuleName = Mage2db / CustomerGraphQl
Step [1]– Create registration.php file under your module [ Namespace / ModuleName ]
#Custom Module Customer GraphQL
type Query {
customer_details(
id: Int! @doc(description: "Specify The ID of The Customer.")
): CustomerData @resolver( class: "Mage2db\\CustomerGraphQl\\Model\\Resolver\\Customerlist") @doc(description: "Get list of Customer Data for the given customer id.")
}
type CustomerData {
firstname: String
lastname: String
email: String
gender: String
dob: String
city: String
created_at: String
}
id: Int @doc(description: “Id of the Customer”) map to Stored Customers Listing id as Int type
Step [4] – Need to create Customerlist.php file from defined resolver from above schema.
CustomerData @resolver( class: “Mage2db\CustomerGraphQl\Model\Resolver\Customerlist”) @doc(description: “Get list of Customer Data for the given Customer ID.”)
Add below content in this file.
<?php
/**
* @author John
* @email johndusa1021@gmail.com
* @copyright Copyright (c) 2022 (https://mage2db.com)
* @package CustomerGraphQl
*/
namespace Mage2db\CustomerGraphQl\Model\Resolver;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
class Customerlist implements ResolverInterface
{
protected $_customerSession;
protected $_customerFactory;
protected $_addressFactory;
protected $storeManager;
/**
* @param Field $field
* @param \Magento\Framework\GraphQl\Query\Resolver\ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array|\Magento\Framework\GraphQl\Query\Resolver\Value|mixed
* @throws GraphQlInputException
*/
public function __construct(
\Magento\Customer\Model\SessionFactory $customerSession,
\Magento\Customer\Model\CustomerFactory $customerFactory,
\Magento\Customer\Model\AddressFactory $addressFactory,
\Magento\Framework\Url $url,
StoreManagerInterface $storeManager
)
{
$this->_customerSession = $customerSession->create();
$this->_customerFactory = $customerFactory;
$this->_addressFactory = $addressFactory;
$this->urlHelper = $url;
$this->storeManager = $storeManager;
}
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null)
{
$currentStoreId = $this->storeManager->getStore()->getId();
$Customer_Collection = $this->_customerFactory->create()->load($args['id']);
return $Customer_Collection->getData();
}
}
As per above script, resolve() method having script which is responsible for getting Customer GraphQl Data.
Step [5] – Finally your Customer GraphQl Query Module has been created
Run the following below commands at your Magento 2 root directory