-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Christian Fritsch
committed
Aug 30, 2024
1 parent
3f731a5
commit 1deab6b
Showing
23 changed files
with
1,013 additions
and
107 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
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 |
---|---|---|
@@ -1,7 +1,6 @@ | ||
type Menu { | ||
id: String! | ||
name: String! | ||
|
||
items: [MenuItem] | ||
} | ||
|
||
|
4 changes: 4 additions & 0 deletions
4
modules/thunder_gqls/graphql/thunder_search_api.base.graphqls
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,4 @@ | ||
type SearchApiResult { | ||
items: [Page!] | ||
total: Int! | ||
} |
Empty file.
101 changes: 101 additions & 0 deletions
101
modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.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,101 @@ | ||
<?php | ||
|
||
namespace Drupal\thunder_gqls\GraphQL\Buffers; | ||
|
||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter; | ||
use Drupal\graphql\GraphQL\Buffers\BufferBase; | ||
|
||
/** | ||
* GraphQL Buffer for SearchApi Results. | ||
*/ | ||
class SearchApiResultBuffer extends BufferBase { | ||
|
||
/** | ||
* The entity type manager service. | ||
* | ||
* @var \Drupal\Core\Entity\EntityTypeManagerInterface | ||
*/ | ||
protected EntityTypeManagerInterface $entityTypeManager; | ||
|
||
/** | ||
* EntityBuffer constructor. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | ||
* The entity type manager service. | ||
*/ | ||
public function __construct(EntityTypeManagerInterface $entityTypeManager) { | ||
$this->entityTypeManager = $entityTypeManager; | ||
} | ||
|
||
/** | ||
* Add an item to the buffer. | ||
* | ||
* @param string|int|null $index | ||
* The entity type of the given entity ids. | ||
* @param array|int $id | ||
* The entity id(s) to load. | ||
* | ||
* @return \Closure | ||
* The callback to invoke to load the result for this buffer item. | ||
*/ | ||
public function add($index, $id) { | ||
$item = new \ArrayObject([ | ||
'index' => $index, | ||
'id' => $id, | ||
]); | ||
|
||
return $this->createBufferResolver($item); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function getBufferId($item) { | ||
return $item['index']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolveBufferArray(array $buffer) { | ||
$index = reset($buffer)['index']; | ||
$ids = array_map(function (\ArrayObject $item) { | ||
return (array) $item['id']; | ||
}, $buffer); | ||
|
||
$ids = call_user_func_array('array_merge', $ids); | ||
$ids = array_values(array_unique($ids)); | ||
|
||
// Load the buffered entities. | ||
/** @var \Drupal\search_api\IndexInterface $index */ | ||
$index = $this->entityTypeManager | ||
->getStorage('search_api_index') | ||
->load($index); | ||
|
||
$resultSet = $index->loadItemsMultiple($ids); | ||
$entities = []; | ||
|
||
foreach ($resultSet as $key => $resultItem) { | ||
if ($resultItem instanceof EntityAdapter) { | ||
$entities[$key] = $resultItem->getEntity(); | ||
} | ||
} | ||
|
||
return array_map(function ($item) use ($entities) { | ||
if (is_array($item['id'])) { | ||
return array_reduce($item['id'], static function ($carry, $current) use ($entities) { | ||
if (!empty($entities[$current])) { | ||
$carry[] = $entities[$current]; | ||
return $carry; | ||
} | ||
|
||
return $carry; | ||
}, []); | ||
} | ||
|
||
return $entities[$item['id']] ?? NULL; | ||
}, $buffer); | ||
} | ||
|
||
} |
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
Oops, something went wrong.