You are here

public static function EntitySchemaProvider::getEntityList in GraphQL 8.2

Same name and namespace in other branches
  1. 8 src/SchemaProvider/EntitySchemaProvider.php \Drupal\graphql\SchemaProvider\EntitySchemaProvider::getEntityList()

Entity list resolver callback.

File

src/SchemaProvider/EntitySchemaProvider.php, line 177

Class

EntitySchemaProvider
Generates a GraphQL Schema for content entity types.

Namespace

Drupal\graphql\SchemaProvider

Code

public static function getEntityList($source, array $args = NULL, $root, Node $field, $a, $b, $c, $data) {
  $storage = \Drupal::entityManager()
    ->getStorage($data['type']);
  $query = $storage
    ->getQuery()
    ->accessCheck(TRUE);
  $rangeArgs = array(
    'offset',
    'limit',
  );
  $filterArgs = array_diff_key($args, array_flip($rangeArgs));
  foreach ($filterArgs as $key => $arg) {
    if (isset($arg) && isset($data['args'][$key])) {
      $arg = is_array($arg) && sizeof($arg) === 1 ? reset($arg) : $arg;
      $operator = is_array($arg) ? 'IN' : '=';
      $query
        ->condition($data['args'][$key], $arg, $operator);
    }
  }
  if (!empty($args['offset']) || !empty($args['limit'])) {
    $query
      ->range($args['offset'] ?: NULL, $args['limit'] ?: NULL);
  }
  $result = $query
    ->execute();
  if (!empty($result)) {
    $entities = $storage
      ->loadMultiple($result);

    // Filter entities that the current user doesn't have view access for.
    return array_map(function (ContentEntityInterface $entity) {
      return $entity
        ->getTypedData();
    }, array_filter($entities, function (ContentEntityInterface $entity) {
      return $entity
        ->access('view');
    }));
  }
  return [];
}