-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #261 from UN-OCHA/berliner/HPC-9890
HPC-9890: Allow to fetch only ids of content entities for export
- Loading branch information
Showing
5 changed files
with
127 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
html/modules/custom/ncms_graphql/src/Wrappers/QueryConnection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Drupal\ncms_graphql\Wrappers; | ||
|
||
use Drupal\Core\Entity\Query\QueryInterface; | ||
use GraphQL\Deferred; | ||
|
||
/** | ||
* Helper class that wraps entity queries. | ||
*/ | ||
class QueryConnection { | ||
|
||
/** | ||
* The query object. | ||
* | ||
* @var \Drupal\Core\Entity\Query\QueryInterface | ||
*/ | ||
protected $query; | ||
|
||
/** | ||
* QueryConnection constructor. | ||
* | ||
* @param \Drupal\Core\Entity\Query\QueryInterface $query | ||
* The query object. | ||
*/ | ||
public function __construct(QueryInterface $query) { | ||
$this->query = $query; | ||
} | ||
|
||
/** | ||
* Return the number of results. | ||
* | ||
* @return int | ||
* The number of results. | ||
*/ | ||
public function count() { | ||
$query = clone $this->query; | ||
$query->range(NULL, NULL)->count(); | ||
/** @var int */ | ||
return $query->execute(); | ||
} | ||
|
||
/** | ||
* Return the ids. | ||
* | ||
* @return int[] | ||
* An array of ids. | ||
*/ | ||
public function ids() { | ||
$result = $this->query->execute(); | ||
if (empty($result)) { | ||
return []; | ||
} | ||
return array_values($result); | ||
} | ||
|
||
/** | ||
* Return all items. | ||
* | ||
* @return array|\GraphQL\Deferred | ||
* The promise. | ||
*/ | ||
public function items() { | ||
$result = $this->query->execute(); | ||
if (empty($result)) { | ||
return []; | ||
} | ||
|
||
$buffer = \Drupal::service('graphql.buffer.entity'); | ||
$callback = $buffer->add($this->query->getEntityTypeId(), array_values($result)); | ||
return new Deferred(function () use ($callback) { | ||
return $callback(); | ||
}); | ||
} | ||
|
||
} |