Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL query for Search API #830

Merged
merged 50 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
6b8e143
wip
dbosen Aug 14, 2024
d6b4a52
some code style fixes
dbosen Aug 14, 2024
e4ef7b4
copy paste error
dbosen Aug 14, 2024
b2fbb04
fix typos
dbosen Aug 14, 2024
2fa07f5
Ignore static error
dbosen Aug 14, 2024
dac8332
renaming
dbosen Aug 14, 2024
f366870
Merge branch '7.3.x' into feature/search_api_endpoint
dbosen Aug 14, 2024
10debcc
Refactor base class
dbosen Aug 14, 2024
9b9edf6
Merge branch 'feature/search_api_endpoint' of github.com:thunder/thun…
dbosen Aug 14, 2024
6864296
Merge remote-tracking branch 'origin/7.3.x' into feature/search_api_e…
dbosen Aug 14, 2024
1cb5e60
rename query
dbosen Aug 14, 2024
9e74496
Improve create
dbosen Aug 19, 2024
f418f08
code styles
dbosen Aug 19, 2024
2d805be
add test
dbosen Aug 19, 2024
9defacd
remove void return
dbosen Aug 19, 2024
528e18f
replace deprecated phpstan config options
dbosen Aug 19, 2024
fedb256
Merge branch '7.3.x' into feature/search_api_endpoint
dbosen Aug 19, 2024
f329c3e
nit picks
dbosen Aug 19, 2024
8098902
Merge branch 'feature/search_api_endpoint' of github.com:thunder/thun…
dbosen Aug 19, 2024
c7f455d
refactor class instatiation
dbosen Aug 19, 2024
8f13a78
fix return type
dbosen Aug 19, 2024
b7aace2
cs
dbosen Aug 19, 2024
086313e
Extract repose method
dbosen Aug 19, 2024
eed2317
make class resolver private
dbosen Aug 19, 2024
8a07c23
refactor EntityListResponse
dbosen Aug 20, 2024
c292e76
Use proper service
dbosen Aug 20, 2024
5b0e859
refactor entity list response
dbosen Aug 20, 2024
b120f14
Needs to be public...
dbosen Aug 20, 2024
6067749
coding style
dbosen Aug 20, 2024
852eed0
Do not use deprecated call
dbosen Aug 20, 2024
40772cd
remove unused use
dbosen Aug 20, 2024
2461712
fix some classes
dbosen Aug 20, 2024
191dff7
even more
dbosen Aug 20, 2024
4cd599b
do not autowire
dbosen Aug 20, 2024
0733c09
add arguments
dbosen Aug 20, 2024
41fe1fd
Add sortBy parameter
dbosen Aug 20, 2024
2c5d2e0
cs
dbosen Aug 20, 2024
062da1b
Remove default endpoint
dbosen Aug 20, 2024
3a8cb2d
Improve language selection
dbosen Aug 21, 2024
e69fa27
add condition test
dbosen Aug 21, 2024
cd01309
remove search schema extension
dbosen Aug 21, 2024
333388a
cs fixes
dbosen Aug 21, 2024
4c8a089
Update modules/thunder_gqls/src/Wrappers/EntityListResponse.php
dbosen Aug 21, 2024
bc2f450
Update modules/thunder_gqls/src/Wrappers/EntityListResponse.php
dbosen Aug 21, 2024
97967b2
addressing issues
dbosen Aug 21, 2024
5325c9a
Ignore cs
dbosen Aug 21, 2024
2265622
Improve bc
dbosen Aug 22, 2024
2b1f74d
cs
dbosen Aug 22, 2024
76b66ae
simplify diff
dbosen Aug 22, 2024
1d8c217
Update modules/thunder_gqls/src/Wrappers/EntityListResponse.php
dbosen Aug 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion modules/thunder_gqls/graphql/thunder_menu.base.graphqls
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
type Menu {
id: String!
name: String!

items: [MenuItem]
}

Expand Down
4 changes: 4 additions & 0 deletions modules/thunder_gqls/graphql/thunder_search_api.base.graphqls
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 modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\taxonomy\TermInterface;
use Drupal\thunder_gqls\Wrappers\EntityListResponse;
use Drupal\thunder_gqls\Wrappers\EntityListResponseInterface;

/**
Expand Down Expand Up @@ -119,7 +118,7 @@ public function resolve(TermInterface $term, string $type, array $bundles, strin
$cacheContext
);

return new EntityListResponse($query);
return $this->entityListResponse($query);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer;

use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\thunder_gqls\Wrappers\EntityListResponse;
use Drupal\thunder_gqls\Wrappers\EntityListResponseInterface;

/**
Expand Down Expand Up @@ -97,7 +96,7 @@ protected function resolve(string $type, array $bundles, int $offset, int $limit
$cacheContext
);

return new EntityListResponse($query);
return $this->entityListResponse($query);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer;

use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\graphql\GraphQL\Execution\FieldContext;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\thunder_gqls\Wrappers\EntityListResponse;
use GraphQL\Error\UserError;
use Symfony\Component\DependencyInjection\ContainerInterface;

Expand All @@ -21,54 +22,95 @@ abstract class ThunderEntityListProducerBase extends DataProducerPluginBase impl
/**
* The entity type manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManager
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
protected EntityTypeManagerInterface $entityTypeManager;

/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
protected AccountInterface $currentUser;

/**
* The response wrapper service.
*
* @var \Drupal\thunder_gqls\Wrappers\EntityListResponse
*/
protected EntityListResponse $responseWrapper;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new static(
$instance = new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
$container->get('current_user')
);

$instance->setResponseWrapper($container->get('thunder_gqls.entity_list_response_wrapper'));

return $instance;
}

/**
* EntityLoad constructor.
* ThunderEntityListProducerBase constructor.
*
* @param array $configuration
* The plugin configuration array.
* @param string $pluginId
* The plugin id.
* @param array $pluginDefinition
* The plugin definition array.
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
*/
public function __construct(
array $configuration,
string $pluginId,
array $pluginDefinition,
EntityTypeManager $entityTypeManager,
AccountInterface $current_user,
EntityTypeManagerInterface $entityTypeManager,
AccountInterface $currentUser,
) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->setEntityTypeManager($entityTypeManager);
$this->setCurrentUser($currentUser);
}

/**
* Set the entity type manager service.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
*/
public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void {
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $current_user;
}

/**
* Set the current user.
*
* @param \Drupal\Core\Session\AccountInterface $currentUser
* The current user.
*/
public function setCurrentUser(AccountInterface $currentUser): void {
$this->currentUser = $currentUser;
}

/**
* Set the response wrapper service.
*
* @param \Drupal\thunder_gqls\Wrappers\EntityListResponse $responseWrapper
* The response wrapper service.
*/
public function setResponseWrapper(EntityListResponse $responseWrapper): void {
$this->responseWrapper = $responseWrapper;
}

/**
Expand Down Expand Up @@ -163,10 +205,7 @@ protected function query(
$query->range($offset, $limit);

$storage = $this->entityTypeManager->getStorage($type);
$entityType = $storage->getEntityType();

$cacheContext->addCacheTags($entityType->getListCacheTags());
$cacheContext->addCacheContexts($entityType->getListCacheContexts());
$cacheContext->addCacheableDependency($storage->getEntityType());
return $query;
}

Expand Down Expand Up @@ -202,4 +241,17 @@ protected function createPublishedCondition(string $type, array $conditions) {
];
}

/**
* The entity list response.
*
* @param \Drupal\Core\Entity\Query\QueryInterface $query
* The entity query.
*
* @return \Drupal\thunder_gqls\Wrappers\EntityListResponse
* The entity list response.
*/
protected function entityListResponse(QueryInterface $query): EntityListResponse {
return $this->responseWrapper->setQuery($query);
}

}
Loading
Loading