STEP-4 Simple GraphQl and Resolver
The next few steps not going to be so visual, but very important, we going to create an Extension and establish the connection (communication) between this extension and our module.
โ€‹STEP-4โ€‹
In this particular step, we going to create a sample graphql shema and resolver.
  1. 1.
    Create schema.graphqls in <MODULE ROOT>/etc/
ScandiPWA/SocialShareGraphQl/etc/schema.graphqls
1
type Query {
2
socialShare: socialShareType @resolver(class:"\\ScandiPWA\\SocialShareGraphQl\\Model\\Resolver\\SocialShare")
3
}
4
โ€‹
5
type socialShareType {
6
enabled: String
7
}
8
โ€‹
Copied!
2. Create Model folder in <MODULE ROOT> and Resolver folder in it. 3. Create SocialShare.php in <MODULE ROOT>/Model/Resolver
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\Resolver\Value;
10
use Magento\Framework\GraphQl\Query\ResolverInterface;
11
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12
use Magento\Framework\App\Config\ScopeConfigInterface;
13
โ€‹
14
/**
15
* @package ScandiPWA\SocialShareGraphQl\Model\Resolver
16
*/
17
class SocialShare implements ResolverInterface
18
{
19
/**
20
* @param Field $field
21
* @param ContextInterface $context
22
* @param ResolveInfo $info
23
* @param array|null $value
24
* @param array|null $args
25
* @return string[]
26
*/
27
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
28
{
29
$result = [
30
'enabled' => 'works'
31
];
32
โ€‹
33
return $result;
34
}
35
}
36
โ€‹
Copied!
4. run cache:flush
5. Open GraphQl Client, for example, Altair GraphQL Client execute a query and check if you created resolver is responding
Could not load image
Copy link