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. Create schema.graphqls in <MODULE ROOT>/etc/

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

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
<?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\Resolver\Value;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\App\Config\ScopeConfigInterface;
​
/**
* @package ScandiPWA\SocialShareGraphQl\Model\Resolver
*/
class SocialShare implements ResolverInterface
{
/**
* @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 = [
'enabled' => 'works'
];
​
return $result;
}
}
​

4. run cache:flush

5. Open GraphQl Client, for example, Altair GraphQL Client execute a query and check if you created resolver is responding

useful materials: GraphQl queries, Working with GraphQL