View source
<?php
namespace Drupal\thunder_gqls\Plugin\GraphQL\SchemaExtension;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\paragraphs\ParagraphInterface;
use GraphQL\Type\Definition\ResolveInfo;
class ThunderParagraphsSchemaExtension extends ThunderSchemaExtensionPluginBase {
public function registerResolvers(ResolverRegistryInterface $registry) {
parent::registerResolvers($registry);
$this->registry
->addTypeResolver('Paragraph', \Closure::fromCallable([
__CLASS__,
'resolveParagraphTypes',
]));
$this
->resolveFields();
}
protected function resolveFields() {
$this
->resolveParagraphInterfaceFields('ParagraphText');
$this
->addFieldResolverIfNotExists('ParagraphText', 'text', $this->builder
->fromPath('entity', 'field_text.processed'));
$this
->resolveParagraphInterfaceFields('ParagraphImage');
$this
->addFieldResolverIfNotExists('ParagraphImage', 'image', $this->builder
->fromPath('entity', 'field_image.entity'));
$this
->resolveParagraphInterfaceFields('ParagraphTwitter');
$this
->addFieldResolverIfNotExists('ParagraphTwitter', 'url', $this->builder
->compose($this->builder
->fromPath('entity', 'field_media.entity'), $this->builder
->fromPath('entity', 'field_url.value')));
$this
->resolveParagraphInterfaceFields('ParagraphInstagram');
$this
->addFieldResolverIfNotExists('ParagraphInstagram', 'url', $this->builder
->compose($this->builder
->fromPath('entity', 'field_media.entity'), $this->builder
->fromPath('entity', 'field_url.value')));
$this
->resolveParagraphInterfaceFields('ParagraphPinterest');
$this
->addFieldResolverIfNotExists('ParagraphPinterest', 'url', $this->builder
->compose($this->builder
->fromPath('entity', 'field_media.entity'), $this->builder
->fromPath('entity', 'field_url.value')));
$this
->resolveParagraphInterfaceFields('ParagraphGallery');
$this
->addFieldResolverIfNotExists('ParagraphGallery', 'name', $this->builder
->compose($this->builder
->fromPath('entity', 'field_media.entity'), $this->builder
->produce('entity_label')
->map('entity', $this->builder
->fromParent())));
$this
->addFieldResolverIfNotExists('ParagraphGallery', 'images', $this->builder
->compose($this->builder
->fromPath('entity', 'field_media.entity'), $this
->fromEntityReference('field_media_images')));
$this
->resolveParagraphInterfaceFields('ParagraphLink');
$this
->addFieldResolverIfNotExists('ParagraphLink', 'links', $this->builder
->fromPath('entity', 'field_link'));
$this
->resolveParagraphInterfaceFields('ParagraphVideo');
$this
->addFieldResolverIfNotExists('ParagraphVideo', 'video', $this->builder
->fromPath('entity', 'field_video.entity'));
$this
->resolveParagraphInterfaceFields('ParagraphQuote');
$this
->addFieldResolverIfNotExists('ParagraphQuote', 'text', $this->builder
->fromPath('entity', 'field_text.processed'));
}
protected function resolveParagraphTypes($value, ResolveContext $context, ResolveInfo $info) : string {
if ($value instanceof ParagraphInterface) {
return 'Paragraph' . $this
->mapBundleToSchemaName($value
->bundle());
}
throw new \Exception('Invalid paragraph type.');
}
}