We sacrifice by not doing any other technology, so that you get the best of Magento.

We sacrifice by not doing any other technology, so that you get the best of Magento.

By using GraphQL, You can get data of logged in customers.

Magento 2 can directly call api to get data by resolver.

Module name : Magemonkeys::Custom

create file schema.graphqls in etc folder in that module

type Query
{
    getCustomerData: Response 
    @resolver(class:"Magemonkeys\Custom\Model\Resolver\Getdataforcustomer")
}

type Response
{
    customer_id: String
    customer_name: String
}

Create Getdataforcustomer.php in Model/Resolver Folder in that module

namespace MagemonkeysCustomModelResolver;

use MagentoFrameworkGraphQlQueryResolverInterface;
use MagentoFrameworkGraphQlConfigElementField;
use MagentoFrameworkGraphQlSchemaTypeResolveInfo;
use MagentoFrameworkGraphQlExceptionGraphQlNoSuchEntityException;
use MagentoFrameworkGraphQlExceptionGraphQlAuthorizationException;
use MagentoCustomerGraphQlModelCustomerGetCustomer;

class Getdataforcustomer implements ResolverInterface
{
   private $getdataforCustomer;
     
   public function __construct(GetCustomer $getdataforCustomer)
   {
      $this->getCustomer = $getdataforCustomer;
   }
   public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
   {
      try
      {
         if (false === $context->getExtensionAttributes()->getIsCustomer())
         {
            throw new GraphQlAuthorizationException(__('The current customer not getting'));
         }
         $customerdata = $this->getCustomer->execute($context);
         $customername = $customerdata->getFirstName()." ".$customerdata->getLastName();
         $result = ['customer_id' => $customer->getId(),
                    'customer_name' => $customername
                   ];   
         return $result;
      }
      catch(Exception $exception)
      {
          throw new GraphQlNoSuchEntityException(__($exception->getMessage()));
      }
   }
}

After that run command to call api url

That’s it.

 

Fill the below form if you have any Magento programming need.

    Recent Articles
    Get a Free Quote

      Let’s initiate a discussion!!