You are here

public static function ViewsSchemaProvider::getViewResults in GraphQL 8

Same name and namespace in other branches
  1. 8.2 src/SchemaProvider/ViewsSchemaProvider.php \Drupal\graphql\SchemaProvider\ViewsSchemaProvider::getViewResults()

Views result resolver callback.

File

src/SchemaProvider/ViewsSchemaProvider.php, line 146

Class

ViewsSchemaProvider
Generates a GraphQL Schema for views.

Namespace

Drupal\graphql\SchemaProvider

Code

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;
}