class ViewsSchemaProvider in GraphQL 8
Same name and namespace in other branches
- 8.2 src/SchemaProvider/ViewsSchemaProvider.php \Drupal\graphql\SchemaProvider\ViewsSchemaProvider
Generates a GraphQL Schema for views.
Hierarchy
- class \Drupal\graphql\SchemaProvider\SchemaProviderBase implements SchemaProviderInterface
- class \Drupal\graphql\SchemaProvider\ViewsSchemaProvider
Expanded class hierarchy of ViewsSchemaProvider
1 string reference to 'ViewsSchemaProvider'
1 service uses ViewsSchemaProvider
File
- src/
SchemaProvider/ ViewsSchemaProvider.php, line 21
Namespace
Drupal\graphql\SchemaProviderView source
class ViewsSchemaProvider extends SchemaProviderBase {
/**
* The entity manager service.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The type resolver service.
*
* @var \Drupal\graphql\TypeResolverInterface
*/
protected $typeResolver;
/**
* The typed data manager service.
*
* @var \Drupal\Core\TypedData\TypedDataManager
*/
protected $typedDataManager;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs a ViewsSchemaProvider object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entityManager
* The entity manager service.
* @param TypedDataManager $typedDataManager
* The typed data manager service.
* @param \Drupal\graphql\TypeResolverInterface $typeResolver
* The base type resolver service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler service.
*/
public function __construct(EntityManagerInterface $entityManager, TypedDataManager $typedDataManager, TypeResolverInterface $typeResolver, ModuleHandlerInterface $moduleHandler) {
$this->entityManager = $entityManager;
$this->typeResolver = $typeResolver;
$this->typedDataManager = $typedDataManager;
$this->moduleHandler = $moduleHandler;
}
/**
* @return array
*/
protected function getDataTableMap() {
$map = [];
foreach ($this->entityManager
->getDefinitions() as $entityTypeId => $entityType) {
if ($dataTable = $entityType
->getDataTable()) {
$map[$dataTable] = $entityType
->id();
}
}
return $map;
}
/**
* {@inheritdoc}
*/
public function getQuerySchema() {
// Do not attempt to generate the schema if the views module is not enabled.
if (!$this->moduleHandler
->moduleExists('views')) {
return [];
}
$fields = [];
$map = $this
->getDataTableMap();
/** @var \Drupal\views\ViewEntityInterface $view */
foreach ($this->entityManager
->getStorage('view')
->loadMultiple() as $view) {
$baseTable = $view
->get('base_table');
if (!isset($map[$baseTable])) {
continue;
}
$viewId = $view
->id();
foreach ($view
->get('display') as $displayId => $display) {
if ($display['display_plugin'] !== 'graphql') {
continue;
}
$executable = $view
->getExecutable();
$executable
->setDisplay($displayId);
$filters = $executable
->getDisplay()
->getOption('filters');
$filters = array_reduce(array_filter($filters, function (array $filter) {
return !empty($filter['exposed']);
}), function (array $carry, array $filter) {
$type = $filter['expose']['required'] ? new NonNullModifier(Type::stringType()) : Type::stringType();
$type = $filter['expose']['multiple'] ? new ListModifier($type) : $type;
$type = $filter['expose']['required'] ? new NonNullModifier($type) : $type;
$carry[$filter['expose']['identifier']] = [
'type' => $type,
];
return $carry;
}, []);
$definition = $this->typedDataManager
->createDataDefinition("entity:{$map[$baseTable]}");
if (!($resolved = $this->typeResolver
->resolveRecursive($definition))) {
continue;
}
$fields["{$viewId}:{$displayId}"] = [
'type' => new ListModifier($resolved),
'resolve' => [
__CLASS__,
'getViewResults',
],
'args' => $filters,
'resolveData' => [
'view' => $viewId,
'display' => $displayId,
],
];
}
}
$names = StringHelper::formatPropertyNameList(array_keys($fields));
return array_combine($names, $fields);
}
/**
* Views result resolver callback.
*/
public static function getViewResults($source, array $args = NULL, $root, Node $field, $a, $b, $c, $data) {
// @todo Fix injection of container dependencies in resolver functions.
$storage = \Drupal::entityManager()
->getStorage('view');
/** @var \Drupal\views\ViewEntityInterface $view */
if (!($view = $storage
->load($data['view']))) {
return NULL;
}
$executable = $view
->getExecutable();
$executable
->setDisplay($data['display']);
$executable
->setExposedInput(array_filter($args));
$executable
->execute();
$result = [];
foreach ($executable->result as $row) {
$entity = $row->_entity;
$result[$entity
->id()] = $entity
->getTypedData();
}
return $result;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SchemaProviderBase:: |
public | function |
Overrides SchemaProviderInterface:: |
|
ViewsSchemaProvider:: |
protected | property | The entity manager service. | |
ViewsSchemaProvider:: |
protected | property | The module handler service. | |
ViewsSchemaProvider:: |
protected | property | The typed data manager service. | |
ViewsSchemaProvider:: |
protected | property | The type resolver service. | |
ViewsSchemaProvider:: |
protected | function | ||
ViewsSchemaProvider:: |
public | function |
Overrides SchemaProviderBase:: |
|
ViewsSchemaProvider:: |
public static | function | Views result resolver callback. | |
ViewsSchemaProvider:: |
public | function | Constructs a ViewsSchemaProvider object. |