Skip to content

Magento 2 How to use header variable in resolver?

I have created one graphql extension for customer OTP send and verify for login with OTP in the project’s POS app.

There are two requests

  • When POS app request OTP with customer_id and pos session_id, it sends OTP to the customer.
  • When POS app requests verify OTP with customer_id, OTP, and session_id, it checks the sent OTP with the requested OTP if is correct it returns success.

SmsGraphQl/Model/Resolver/CustomerOtpSend.php

public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        if (!isset($args['customer_id']) || !isset($args['session_id'])) {
            throw new GraphQlInputException(__("Please provide 'customer_id' and 'session_id' first!"));
        }
        $sessionId = $args['session_id'];
        if (!$this->data->validateStaffSession($sessionId)){
            return [
                'success' => false,
                'message' => __("Invalid 'session_id'!")
            ];
        }
        try {
            $customerId = $args['customer_id'];
            $customer = $this->customerRegistry->retrieve($customerId);
            $success = true;
            $message = "OTP send successfully!";
            $this->seeSmsHelper->sendAuthentication($customer);
        } catch (Exception $e) {
            $success = false;
            $message = $e->getMessage();
        }

        return [
            'success' => $success,
            'message' => $message
        ];
    }

SmsGraphQl/Model/Resolver/CustomerOtpVerify.php

public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        if (!isset($args['customer_id']) || !isset($args['otp']) || !isset($args['session_id'])) {
            throw new GraphQlInputException(__("Please provide 'customer_id', 'session_id' and 'otp' first!"));
        }

        $customerId = $args['customer_id'];
        $sessionId = $args['session_id'];
        $otp = $args['otp'];

        if (!$this->_data->validateStaffSession($sessionId)){
            return [
                'success' => false,
                'message' => __("Invalid 'session_id'!")
            ];
        }

        $collection = $this->_mobileCollection->addFieldToFilter('customer_id', $customerId)
            ->addFieldToFilter('status', Mobile::STATUS_NOT_VERIFIED)
            ->addFieldToFilter('otp', $otp);

        $mobile = $collection->getFirstItem();
        if ($collection->count() == 0) {
            return [
                'success' => false,
                'message' => ConstantsInterface::OTP_INVALID_MSG
            ];
        }

        $mobile->setStatus(Mobile::STATUS_VERIFIED)->save();

        return [
            'success' => true,
            'message' => __("valid OTP.")
        ];
    }

Requirement

POS is already sent session_id in the header. So in place of the use parameter, we need to use the header variable.

If anyone has used the header variable in Magento 2 graphql then help or provide documentation, how to do it?

Thanks in advance.