How to write grapgQl mutation to create and integrate the contact us page functionality in magento 2.3.2?

Good Day for all.Today’s article is going to cover graphQl mutation for contact-us page. The custom contact us Mutation and functionalities in magento 2.3.

Contact us page is a common and very important page in websites.Where website visitors interact with the website owners. In magento we have a common contact us page and email functionalities in built with.When it comes to graphQl we need to write a custom module to deal it. Currently we don’t have default magento 2.3 contact us Mutation to integrate magento contact-us functionality.

For this we want to create a custom module.What i tried with this custom module is passing the form data to resolver and use the magento default contact us functionality to deal the mailing stuff. Main part in the data provider file was arranging the data as default contact us form data in the magento contact-us module.

graphQl mutation for contact-us page
Contact Us entire module structure

Apart from creating the custom module, i will explain how i wrote the
1. Shema.graphqls.
2. Resolver file.
3. Data provider file.

Shema.graphqls

type Mutation {
    contactusFormSubmit(input: ContactusInput!): ContactusOutput @resolver(class: "\\Ayakil\\ContactUs\\Model\\Resolver\\Contactus") @doc(description:"Contact us form")
}

input ContactusInput {
    fullname: String @doc(description: "The customer's full name")
    email: String @doc(description: "The customer's email address")
    telephone: String @doc(description: "The Telephone")
    message: String @doc(description: "The customer's message")
}

type ContactusOutput {
   success_message: String @doc(description: "Success Message")
}

In this Mutation we have ContactusInput and ContactusOutput where passing the data to resolver and return the success message to front-end.

Resolver file

<?php
namespace Ayakil\ContactUs\Model\Resolver;
//resolver section
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;


class Contactus implements ResolverInterface
{
    private $contactusDataProvider;

    /**
     * @param
     */
    public function __construct(
        \Ayakil\ContactUs\Model\Resolver\DataProvider\Contactus $contactusDataProvider
    ) {
        $this->contactusDataProvider = $contactusDataProvider;
    }

    /**
     * @inheritdoc
     */
    public function resolve(
        Field $field,
        $context,
        ResolveInfo $info,
        array $value = null,
        array $args = null
    ) {
        $fullname = $args['input']['fullname'];
        $email = $args['input']['email'];
        $telephone = $args['input']['telephone'];
        $message = $args['input']['message'];

        $success_message = $this->contactusDataProvider->contactUs(
            $fullname,
            $email,
            $telephone,
            $message
        );
        return $success_message;
    }
}

Data Provider file

<?php

namespace Ayakil\ContactUs\Model\Resolver\DataProvider;

//contact us section
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
use Magento\Contact\Model\ConfigInterface;
use Magento\Contact\Model\MailInterface;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;

class Contactus
{
    /**
     * @var DataPersistorInterface
     */
    private $dataPersistor;

    /**
     * @var MailInterface
     */
    private $mail;

    private $formKey;

    /**
     * @param
     */
    public function __construct(
        ConfigInterface $contactsConfig,
        MailInterface $mail,
        DataPersistorInterface $dataPersistor,
        \Magento\Framework\Data\Form\FormKey $formKey
    ) {
        $this->mail = $mail;
        $this->dataPersistor = $dataPersistor;
        $this->formKey = $formKey;
    }

    public function contactUs($fullname,$email,$subject,$message){
        $thanks_message = [];
        try {
            $this->sendEmail($fullname,$email,$subject,$message);
        }catch (LocalizedException $e) { }
        $thanks_message['success_message']="Thanks For Contacting Us";
        return $thanks_message;
    }

    /**
     * @param array $post Post data from contact form
     * @return void
     */
    private function sendEmail($fullname,$email,$telephone,$message)
    {
        $form_data = [];
        $form_data['name']      =   $fullname;
        $form_data['email']     =   $email;
        $form_data['telephone'] =   $telephone;
        $form_data['comment']   =   $message;
        $form_data['hideit']    =   "";
        $form_data['form_key']  =   $this->getFormKey();

        $this->mail->send(
            $email,
            ['data' => new DataObject($form_data)]
        );
    }

    /**
     * get form key
     *
     * @return string
     */
    public function getFormKey()
    {
        return $this->formKey->getFormKey();
    }
}

I did all my work to use the default contact-us functionality here in the data provider file.Main stuff is creating the data set as used in the default magento and use the mail function as it is.This is totally a hack by myself and please come up with any better solutions if you have.

$form_data = [];
$form_data['name']      =   $fullname;
$form_data['email']     =   $email;
$form_data['telephone'] =   $telephone;
$form_data['comment']   =   $message;
$form_data['hideit']    =   "";
$form_data['form_key']  =   $this->getFormKey();

I created the form data as it is in the default contact-us module and simply pass it with the mail function like below.

$this->mail->send(
            $email,
            ['data' => new DataObject($form_data)]
        );

We need to pass the form key, so i used the method getFormKey which is used to create the magento default form key.

$thanks_message['success_message']="Thanks For Contacting Us";

for the thank you message return variable you want to pass the exact same name[ success_message ] as you define in your type ContactusOutput. In the mutation also use the same wording to get the mutation run.

After every thing finish you want to run the upgrade command and compile command.You want to pass the query like below in the server.

 mutation {
   contactusFormSubmit(
     input:{
        fullname: "Mujahidh haseem"
        email: "mujahidh@gmail.com"
        telephone: "+94769819118"
        message: "Hi i Want to order some Dress from your store"       
     }
   ){
       success_message
   }
 }

For this query you want to pass the input variables as same as u used in the mutation input in the GraphQl schema. You may read What is GraphQl in magento 2.3 and how to access it? get the idea to run the qeuery.

Finally you will get the contact us email to what ever the email you configured in the magento admin panel back-end.

Contact us email configuration
Contact us email configuration

That’s it. Have a nice day.Enjoy coding , Learn , Experience , Teach and Help.

1 thought on “How to write grapgQl mutation to create and integrate the contact us page functionality in magento 2.3.2?”

Leave a Reply

Your email address will not be published. Required fields are marked *