View source
<?php
namespace Drupal\social_event\Plugin\GraphQL\SchemaExtension;
use Drupal\graphql\GraphQL\ResolverBuilder;
use Drupal\graphql\GraphQL\ResolverRegistryInterface;
use Drupal\graphql\Plugin\GraphQL\SchemaExtension\SdlSchemaExtensionPluginBase;
class EventSchemaExtension extends SdlSchemaExtensionPluginBase {
public function registerResolvers(ResolverRegistryInterface $registry) {
$builder = new ResolverBuilder();
$this
->addQueryFields($registry, $builder);
$this
->addEventFields($registry, $builder);
}
protected function addEventFields(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry
->addFieldResolver('Event', 'id', $builder
->produce('entity_uuid')
->map('entity', $builder
->fromParent()));
$registry
->addFieldResolver('Event', 'title', $builder
->produce('entity_label')
->map('entity', $builder
->fromParent()));
$registry
->addFieldResolver('Event', 'author', $builder
->produce('entity_owner')
->map('entity', $builder
->fromParent()));
$registry
->addFieldResolver('Event', 'bodyHtml', $builder
->compose($builder
->produce('field')
->map('entity', $builder
->fromParent())
->map('field', $builder
->fromValue('body')), $builder
->produce('field_renderer')
->map('field', $builder
->fromParent())));
$registry
->addFieldResolver('Event', 'heroImage', $builder
->produce('field')
->map('entity', $builder
->fromParent())
->map('field', $builder
->fromValue('field_event_image')));
$registry
->addFieldResolver('Event', 'comments', $builder
->produce('query_comments')
->map('parent', $builder
->fromParent())
->map('bundle', $builder
->fromValue('comment'))
->map('after', $builder
->fromArgument('after'))
->map('before', $builder
->fromArgument('before'))
->map('first', $builder
->fromArgument('first'))
->map('last', $builder
->fromArgument('last'))
->map('reverse', $builder
->fromArgument('reverse'))
->map('sortKey', $builder
->fromArgument('sortKey')));
$registry
->addFieldResolver('Event', 'startDate', $builder
->compose($builder
->produce('field')
->map('entity', $builder
->fromParent())
->map('field', $builder
->fromValue('field_event_date')), $builder
->produce('date_to_timestamp')
->map('field', $builder
->fromParent())));
$registry
->addFieldResolver('Event', 'endDate', $builder
->compose($builder
->produce('field')
->map('entity', $builder
->fromParent())
->map('field', $builder
->fromValue('field_event_date_end')), $builder
->produce('date_to_timestamp')
->map('field', $builder
->fromParent())
->map('type', $builder
->fromValue('end_date'))));
$registry
->addFieldResolver('Event', 'location', $builder
->fromPath('entity:node', 'field_event_location.value'));
$registry
->addFieldResolver('Event', 'url', $builder
->compose($builder
->produce('social_entity_url')
->map('entity', $builder
->fromParent())
->map('options', $builder
->fromValue([
'absolute' => TRUE,
])), $builder
->produce('url_path')
->map('url', $builder
->fromParent())));
$registry
->addFieldResolver('Event', 'created', $builder
->produce('entity_created')
->map('entity', $builder
->fromParent())
->map('format', $builder
->fromValue('U')));
$registry
->addFieldResolver('Event', 'managers', $builder
->produce('event_managers')
->map('event', $builder
->fromParent())
->map('after', $builder
->fromArgument('after'))
->map('before', $builder
->fromArgument('before'))
->map('first', $builder
->fromArgument('first'))
->map('last', $builder
->fromArgument('last'))
->map('reverse', $builder
->fromArgument('reverse')));
}
protected function addQueryFields(ResolverRegistryInterface $registry, ResolverBuilder $builder) {
$registry
->addFieldResolver('Query', 'events', $builder
->produce('query_event')
->map('after', $builder
->fromArgument('after'))
->map('before', $builder
->fromArgument('before'))
->map('first', $builder
->fromArgument('first'))
->map('last', $builder
->fromArgument('last'))
->map('reverse', $builder
->fromArgument('reverse'))
->map('sortKey', $builder
->fromArgument('sortKey')));
$registry
->addFieldResolver('Query', 'event', $builder
->produce('entity_load_by_uuid')
->map('type', $builder
->fromValue('node'))
->map('bundles', $builder
->fromValue([
'event',
]))
->map('uuid', $builder
->fromArgument('id')));
}
}