STEP-7 GraphQL types, Helpers

As we have successfully established a connection with backend and frontend, let's prepare and collect all configurations we created at STEP-3 transfer.

STEP-7

  1. First of all, we going to modify our schema.graphqls to gain an understanding of our data structure

ScandiPWA/SocialShareGraphQl/etc/schema.graphqls
type Query {
   socialShare: socialShareType @resolver(class:"\\ScandiPWA\\SocialShareGraphQl\\Model\\Resolver\\SocialShare")
}

type socialShareType {
   socialShareConfig: socialShareConfig
   providers: [ socialShareProvider ]
}

type socialShareConfig {
   enabled: Boolean
   rounded: Boolean
   size: String
   categoryPage: Boolean
   productPage: Boolean
   homePage: Boolean
}

type socialShareProvider {
   id: String
   counter: Boolean
   additional: String
}

Ok now we know how our data structure will look like, then we going to pass data through graphql, will create a Helper which will provide socialShareConfig and providers and map all fields. 2. Create Helper folder in <MODULE ROOT> and DataProvider.php in it

ScandiPWA/SocialShareGraphQl/Helper/DataProvider.php
<?php
declare(strict_types=1);

namespace ScandiPWA\SocialShareGraphQl\Helper;

use Magento\Framework\App\Config\ScopeConfigInterface;
/**
* @package ScandiPWA\SocialShareGraphQl\Helper
*/
class DataProvider
{
   const SOCIALSHARE_CONFIG = 'socialshare/general/';

   const SOCIALSHARE = 'socialshare/';

   const ENABLE = 'enable';

   const ROUNDED = 'rounded';

   const SIZE = 'size';

   const HOME_PAGE = 'home_page';

   const CATEGORY_PAGE = 'category_page';

   const PRODUCT_PAGE = 'product_page';

   const COUNTER = 'enable_counter';

   const SUFFIX = 'suffix';

   const APP_ID = 'app_id';

   const FB_MSG = 'facebook_messenger';

   const EMAIL = 'email';

   const PROVIDERS = [
       'facebook',
       'facebook_messenger',
       'telegram',
       'whatsapp',
       'linkedin',
       'email',
   ];

   /**
    * @var ScopeConfigInterface
    */
   protected $scopeConfig;

   /**
    * DataProvider constructor.
    * @param ScopeConfigInterface $scopeConfig
    */
   public function __construct(
       ScopeConfigInterface $scopeConfig
   ) {
       $this->scopeConfig = $scopeConfig;
   }

   /**
    * @return array
    */
   public function getSocialShareConfig() {

       return [
           'enabled' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::ENABLE),
           'rounded' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::ROUNDED),
           'size' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::SIZE),
           'categoryPage' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::CATEGORY_PAGE),
           'productPage' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::PRODUCT_PAGE),
           'homePage' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::HOME_PAGE)
       ];
   }

   /**
    * @return array
    */
   public function getSocialShareProviders() {
       $result = [];

       foreach (self::PROVIDERS as $provider) {
           $data = [];

           switch ($provider) {
               case self::FB_MSG:
                   $data = $this->getFacebookMessengerData();
                   break;
               case self::EMAIL:
                   $data = $this->getEmailData();
                   break;
               default:
                   $data = $this->getData($provider);
           }

           if($data) {
               array_push($result, $data);
           }
       }

       return $result;
   }

   /**
    * @return array|false
    */
   protected function getFacebookMessengerData() {
       $enabled = $this->getProviderConfig(self::FB_MSG, self::ENABLE);

       if($enabled) {
           return [
               'id' => self::FB_MSG,
               'additional' => $this->getProviderConfig(self::FB_MSG, self::APP_ID)
           ];
       }

       return false;
   }

   /**
    * @param $provider
    * @return array|false
    */
   protected function getData($provider) {
       $enabled = $this->getProviderConfig($provider, self::ENABLE);

       if($enabled) {
           return [
               'id' => $provider,
               'counter' => $this->getProviderConfig($provider, self::COUNTER)
           ];
       }

       return false;
   }

   /**
    * @return array|false
    */
   protected function getEmailData() {
       $enabled = $this->getProviderConfig(self::EMAIL, self::ENABLE);

       if($enabled) {
           return [
               'id' => self::EMAIL,
               'additional' => $this->getProviderConfig(self::EMAIL, self::SUFFIX)
           ];
       }

       return false;
   }

   /**
    * @param $path
    * @return mixed
    */
   protected function getConfig($path) {
        return $this->scopeConfig->getValue($path);
   }

   /**
    * @param $provider
    * @param $config
    * @return mixed
    */
   protected function getProviderConfig($provider, $config) {
       return $this->scopeConfig->getValue(self::SOCIALSHARE. $provider. '/'. $config);
   }
}

3. Now we going to modify our resolver and request data from DataProvider

ScandiPWA/SocialShareGraphQl/Model/Resolver/SocialShare.php
<?php

declare(strict_types=1);

namespace ScandiPWA\SocialShareGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use ScandiPWA\SocialShareGraphQl\Helper\DataProvider;

/**
* @package ScandiPWA\SocialShareGraphQl\Model\Resolver
*/
class SocialShare implements ResolverInterface
{
   /**
    * @var DataProvider
    */
   protected $dataProvider;

   /**
    * SocialShare constructor.
    * @param DataProvider $dataProvider
    */
   public function __construct(
       DataProvider $dataProvider
   ) {
       $this->dataProvider = $dataProvider;
   }
   /**
    * @param Field $field
    * @param ContextInterface $context
    * @param ResolveInfo $info
    * @param array|null $value
    * @param array|null $args
    * @return string[]
    */
   public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
   {
       $result = [
           'socialShareConfig' => $this->dataProvider->getSocialShareConfig(),
           'providers' => $this->dataProvider->getSocialShareProviders()
       ];

       return $result;
   }
}

4. Now again using Altair GraphQL Client we going to check if we did everything right

Last updated