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. 1.
    First of all, we going to modify our schema.graphqls to gain an understanding of our data structure
ScandiPWA/SocialShareGraphQl/etc/schema.graphqls
1
type Query {
2
socialShare: socialShareType @resolver(class:"\\ScandiPWA\\SocialShareGraphQl\\Model\\Resolver\\SocialShare")
3
}
4
​
5
type socialShareType {
6
socialShareConfig: socialShareConfig
7
providers: [ socialShareProvider ]
8
}
9
​
10
type socialShareConfig {
11
enabled: Boolean
12
rounded: Boolean
13
size: String
14
categoryPage: Boolean
15
productPage: Boolean
16
homePage: Boolean
17
}
18
​
19
type socialShareProvider {
20
id: String
21
counter: Boolean
22
additional: String
23
}
24
​
Copied!
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
1
<?php
2
declare(strict_types=1);
3
​
4
namespace ScandiPWA\SocialShareGraphQl\Helper;
5
​
6
use Magento\Framework\App\Config\ScopeConfigInterface;
7
/**
8
* @package ScandiPWA\SocialShareGraphQl\Helper
9
*/
10
class DataProvider
11
{
12
const SOCIALSHARE_CONFIG = 'socialshare/general/';
13
​
14
const SOCIALSHARE = 'socialshare/';
15
​
16
const ENABLE = 'enable';
17
​
18
const ROUNDED = 'rounded';
19
​
20
const SIZE = 'size';
21
​
22
const HOME_PAGE = 'home_page';
23
​
24
const CATEGORY_PAGE = 'category_page';
25
​
26
const PRODUCT_PAGE = 'product_page';
27
​
28
const COUNTER = 'enable_counter';
29
​
30
const SUFFIX = 'suffix';
31
​
32
const APP_ID = 'app_id';
33
​
34
const FB_MSG = 'facebook_messenger';
35
​
36
const EMAIL = 'email';
37
​
38
const PROVIDERS = [
39
'facebook',
40
'facebook_messenger',
41
'telegram',
42
'whatsapp',
43
'linkedin',
44
'email',
45
];
46
​
47
/**
48
* @var ScopeConfigInterface
49
*/
50
protected $scopeConfig;
51
​
52
/**
53
* DataProvider constructor.
54
* @param ScopeConfigInterface $scopeConfig
55
*/
56
public function __construct(
57
ScopeConfigInterface $scopeConfig
58
) {
59
$this->scopeConfig = $scopeConfig;
60
}
61
​
62
/**
63
* @return array
64
*/
65
public function getSocialShareConfig() {
66
​
67
return [
68
'enabled' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::ENABLE),
69
'rounded' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::ROUNDED),
70
'size' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::SIZE),
71
'categoryPage' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::CATEGORY_PAGE),
72
'productPage' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::PRODUCT_PAGE),
73
'homePage' => $this->getConfig(self::SOCIALSHARE_CONFIG. self::HOME_PAGE)
74
];
75
}
76
​
77
/**
78
* @return array
79
*/
80
public function getSocialShareProviders() {
81
$result = [];
82
​
83
foreach (self::PROVIDERS as $provider) {
84
$data = [];
85
​
86
switch ($provider) {
87
case self::FB_MSG:
88
$data = $this->getFacebookMessengerData();
89
break;
90
case self::EMAIL:
91
$data = $this->getEmailData();
92
break;
93
default:
94
$data = $this->getData($provider);
95
}
96
​
97
if($data) {
98
array_push($result, $data);
99
}
100
}
101
​
102
return $result;
103
}
104
​
105
/**
106
* @return array|false
107
*/
108
protected function getFacebookMessengerData() {
109
$enabled = $this->getProviderConfig(self::FB_MSG, self::ENABLE);
110
​
111
if($enabled) {
112
return [
113
'id' => self::FB_MSG,
114
'additional' => $this->getProviderConfig(self::FB_MSG, self::APP_ID)
115
];
116
}
117
​
118
return false;
119
}
120
​
121
/**
122
* @param $provider
123
* @return array|false
124
*/
125
protected function getData($provider) {
126
$enabled = $this->getProviderConfig($provider, self::ENABLE);
127
​
128
if($enabled) {
129
return [
130
'id' => $provider,
131
'counter' => $this->getProviderConfig($provider, self::COUNTER)
132
];
133
}
134
​
135
return false;
136
}
137
​
138
/**
139
* @return array|false
140
*/
141
protected function getEmailData() {
142
$enabled = $this->getProviderConfig(self::EMAIL, self::ENABLE);
143
​
144
if($enabled) {
145
return [
146
'id' => self::EMAIL,
147
'additional' => $this->getProviderConfig(self::EMAIL, self::SUFFIX)
148
];
149
}
150
​
151
return false;
152
}
153
​
154
/**
155
* @param $path
156
* @return mixed
157
*/
158
protected function getConfig($path) {
159
return $this->scopeConfig->getValue($path);
160
}
161
​
162
/**
163
* @param $provider
164
* @param $config
165
* @return mixed
166
*/
167
protected function getProviderConfig($provider, $config) {
168
return $this->scopeConfig->getValue(self::SOCIALSHARE. $provider. '/'. $config);
169
}
170
}
171
​
Copied!
3. Now we going to modify our resolver and request data from DataProvider
ScandiPWA/SocialShareGraphQl/Model/Resolver/SocialShare.php
1
<?php
2
​
3
declare(strict_types=1);
4
​
5
namespace ScandiPWA\SocialShareGraphQl\Model\Resolver;
6
​
7
use Magento\Framework\GraphQl\Config\Element\Field;
8
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
9
use Magento\Framework\GraphQl\Query\ResolverInterface;
10
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
11
use ScandiPWA\SocialShareGraphQl\Helper\DataProvider;
12
​
13
/**
14
* @package ScandiPWA\SocialShareGraphQl\Model\Resolver
15
*/
16
class SocialShare implements ResolverInterface
17
{
18
/**
19
* @var DataProvider
20
*/
21
protected $dataProvider;
22
​
23
/**
24
* SocialShare constructor.
25
* @param DataProvider $dataProvider
26
*/
27
public function __construct(
28
DataProvider $dataProvider
29
) {
30
$this->dataProvider = $dataProvider;
31
}
32
/**
33
* @param Field $field
34
* @param ContextInterface $context
35
* @param ResolveInfo $info
36
* @param array|null $value
37
* @param array|null $args
38
* @return string[]
39
*/
40
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
41
{
42
$result = [
43
'socialShareConfig' => $this->dataProvider->getSocialShareConfig(),
44
'providers' => $this->dataProvider->getSocialShareProviders()
45
];
46
​
47
return $result;
48
}
49
}
50
​
Copied!
4. Now again using Altair GraphQL Client we going to check if we did everything right
Could not load image
Copy link