From 6b8e143abafbad82a4cf53b7a8d4b9c5b7b998fa Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 14 Aug 2024 10:43:26 +0200 Subject: [PATCH 01/45] wip --- .../graphql/thunder_menu.base.graphqls | 1 - .../graphql/thunder_search.base.graphqls | 4 + .../graphql/thunder_search.extension.graphqls | 3 + .../GraphQL/Buffers/SearchApiResultBuffer.php | 102 ++++++++ .../ThunderSearchApiResultsProducer.php | 173 +++++++++++++ .../ThunderSearchApiSchemaExtension.php | 40 +++ .../src/Traits/ResolverHelperTrait.php | 5 +- .../src/Wrappers/EntityListResponse.php | 2 +- .../src/Wrappers/SearchApiResponse.php | 230 ++++++++++++++++++ .../Wrappers/SearchApiResponseInterface.php | 17 ++ .../thunder_gqls/thunder_gqls.services.yml | 4 + 11 files changed, 577 insertions(+), 4 deletions(-) create mode 100644 modules/thunder_gqls/graphql/thunder_search.base.graphqls create mode 100644 modules/thunder_gqls/graphql/thunder_search.extension.graphqls create mode 100644 modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php create mode 100644 modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php create mode 100644 modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php create mode 100644 modules/thunder_gqls/src/Wrappers/SearchApiResponse.php create mode 100644 modules/thunder_gqls/src/Wrappers/SearchApiResponseInterface.php create mode 100644 modules/thunder_gqls/thunder_gqls.services.yml diff --git a/modules/thunder_gqls/graphql/thunder_menu.base.graphqls b/modules/thunder_gqls/graphql/thunder_menu.base.graphqls index a61951f9a..eadd9339d 100644 --- a/modules/thunder_gqls/graphql/thunder_menu.base.graphqls +++ b/modules/thunder_gqls/graphql/thunder_menu.base.graphqls @@ -1,7 +1,6 @@ type Menu { id: String! name: String! - items: [MenuItem] } diff --git a/modules/thunder_gqls/graphql/thunder_search.base.graphqls b/modules/thunder_gqls/graphql/thunder_search.base.graphqls new file mode 100644 index 000000000..29db90559 --- /dev/null +++ b/modules/thunder_gqls/graphql/thunder_search.base.graphqls @@ -0,0 +1,4 @@ +type SearchApiResult { + items: [Page!] + total: Int! +} diff --git a/modules/thunder_gqls/graphql/thunder_search.extension.graphqls b/modules/thunder_gqls/graphql/thunder_search.extension.graphqls new file mode 100644 index 000000000..ff20dead6 --- /dev/null +++ b/modules/thunder_gqls/graphql/thunder_search.extension.graphqls @@ -0,0 +1,3 @@ +extend type Query { + search(search: String!, offset: Int = 0, limit: Int, index: String): SearchApiResult +} diff --git a/modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php b/modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php new file mode 100644 index 000000000..bf2d2e738 --- /dev/null +++ b/modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php @@ -0,0 +1,102 @@ +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) { + // @phpstan-ignore-next-line + 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); + } + +} diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php new file mode 100644 index 000000000..fbe1ae44d --- /dev/null +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php @@ -0,0 +1,173 @@ +get('entity_type.manager'), + $container->get('language_manager'), + ); + } + + /** + * EntityLoad 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 + * The entity type manager service. + * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager + * The language manager service. + */ + public function __construct( + array $configuration, + string $pluginId, + array $pluginDefinition, + EntityTypeManager $entityTypeManager, + LanguageManagerInterface $languageManager, + ) { + parent::__construct($configuration, $pluginId, $pluginDefinition); + $this->entityTypeManager = $entityTypeManager; + $this->languageManager = $languageManager; + } + + /** + * Resolve entity query. + * + * @param int $limit + * Limit of the query. + * @param int $offset + * Offset of the query. + * @param string $index + * Id of the search api index. + * @param array|null $conditions + * List of conditions to filter the result. + * @param string|null $search + * Query Search. + * @param \Drupal\graphql\GraphQL\Execution\FieldContext $cacheContext + * The caching context related to the current field. + * + * @return \Drupal\thunder_gqls\Wrappers\SearchApiResponse|null + * SearchApi result. + * + * @throws \Drupal\search_api\SearchApiException + */ + protected function resolve( + int $limit, + int $offset, + string $index, + ?array $conditions, + ?string $search, + FieldContext $cacheContext, + ): ?SearchApiResponse { + $searchIndex = Index::load($index); + + if (!$searchIndex) { + return NULL; + } + + $defaultConditions = [ + 'status' => TRUE, + 'search_api_language' => $this->languageManager->getCurrentLanguage()->getId(), + ]; + + $conditions = array_merge($defaultConditions, $conditions); + + $query = $searchIndex->query(); + + foreach ($conditions as $field => $value) { + $searchIndex->addCondition($field, $value); + } + + if (!empty($search)) { + $query->keys($search); + } + + $query->range($offset, $limit); + + $query->sort('search_api_relevance', QueryInterface::SORT_DESC); + + $cacheContext->addCacheTags($searchIndex->getCacheTags()); + $cacheContext->addCacheContexts($searchIndex->getCacheContexts()); + + return new SearchApiResponse($query); + } + +} diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php new file mode 100644 index 000000000..d1888202d --- /dev/null +++ b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php @@ -0,0 +1,40 @@ +addFieldResolverIfNotExists('Query', 'search', + $this->builder->produce('thunder_search_api_results') + ->map('search', $this->builder->fromArgument('search')) + ->map('limit', $this->builder->fromArgument('limit')) + ->map('offset', $this->builder->fromArgument('offset')) + ->map('index', $this->builder->fromArgument('index')) + ); + + $this->addFieldResolverIfNotExists('SearchApiResult', 'total', + $this->builder->callback(function (SearchApiResponse $result) { + return $result->total(); + }) + ); + + $this->addFieldResolverIfNotExists('SearchApiResult', 'items', + $this->builder->callback(function (SearchApiResponse $result) { + return $result->items(); + }) + ); + } + +} diff --git a/modules/thunder_gqls/src/Traits/ResolverHelperTrait.php b/modules/thunder_gqls/src/Traits/ResolverHelperTrait.php index d09fbca9e..86d66c1e9 100644 --- a/modules/thunder_gqls/src/Traits/ResolverHelperTrait.php +++ b/modules/thunder_gqls/src/Traits/ResolverHelperTrait.php @@ -4,6 +4,7 @@ use Drupal\graphql\GraphQL\Resolver\ResolverInterface; use Drupal\graphql\GraphQL\ResolverBuilder; +use Drupal\graphql\GraphQL\ResolverRegistryInterface; /** * Helper functions for field resolvers. @@ -15,14 +16,14 @@ trait ResolverHelperTrait { * * @var \Drupal\graphql\GraphQL\ResolverBuilder */ - protected $builder; + protected ResolverBuilder $builder; /** * ResolverRegistryInterface. * * @var \Drupal\graphql\GraphQL\ResolverRegistryInterface */ - protected $registry; + protected ResolverRegistryInterface $registry; /** * Add field resolver to registry, if it does not already exist. diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index d2502ac23..44ef51d07 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -36,7 +36,7 @@ public function __construct(QueryInterface $query) { public function total(): int { $query = clone $this->query; $query->range(NULL, NULL)->count(); - return intval($query->execute()); + return (int) $query->execute(); } /** diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php new file mode 100644 index 000000000..89606f6ce --- /dev/null +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -0,0 +1,230 @@ +query = $query; + $this->result = NULL; + $this->facets = $facets; + $this->facetMapping = $facetMapping; + $this->bundle = $bundle; + } + + /** + * Get search facets. + * + * @return array + * + * @throws \Drupal\search_api\SearchApiException + */ + public function facets(): array { + if (!$this->facets || !$this->facetMapping) { + return []; + } + + if (!$this->result) { + $this->result = $this->query->execute(); + } + + $facets = []; + + $facetData = $this->result->getExtraData('search_api_facets'); + foreach ($facetData as $facetFieldId => $facetResults) { + $facets[] = [ + 'key' => $this->facetMapping[$facetFieldId], + 'values' => $this->processFacetResults($this->facets[$facetFieldId], $facetResults), + ]; + } + + return $facets; + } + + /** + * Get search result items. + * + * @return array|\GraphQL\Deferred + * + * @throws \Drupal\search_api\SearchApiException + */ + public function items() { + if (!$this->result) { + $this->result = $this->query->execute(); + } + + // @phpstan-ignore-next-line + $searchApiResultBuffer = \Drupal::service( + 'thunder_gqls.buffer.search_api_result' + ); + + $ids = array_map(function ($item) { + return $item->getId(); + }, $this->result->getResultItems()); + + $ids = array_unique($ids); + + if (empty($ids)) { + return []; + } + + $callback = $searchApiResultBuffer->add( + $this->query->getIndex()->id(), + array_values($ids) + ); + + return new Deferred(function () use ($callback) { + return $callback(); + }); + } + + /** + * Returns the total results. + * + * @return int + * + * @throws \Drupal\search_api\SearchApiException + */ + public function total(): int { + $query = clone $this->query; + $query->range(0, NULL); + $result = $query->execute(); + + return (int) $result->getResultCount(); + } + + /** + * Handles processing of facet values. + * + * @param \Drupal\facets\Entity\Facet $facet + * The facet to process. + * @param array $facetResults + * The facet results. + * + * @return array + */ + private function processFacetResults( + Facet $facet, + array $facetResults, + ): array { + // First process facet results which contain filter like filter=""9"". + // @see Drupal\facets\Plugin\facets\query_type\SearchApiString#build(). + foreach ($facetResults as $i => $facetResult) { + $facetResult['filter'] = $facetResult['filter'] ?? ''; + + if ($facetResult['filter'][0] === '"') { + $facetResult['filter'] = substr($facetResult['filter'], 1); + } + if ($facetResult['filter'][strlen($facetResult['filter']) - 1] === '"') { + $facetResult['filter'] = substr($facetResult['filter'], 0, -1); + } + + $facetResults[$i] = $facetResult; + } + + return $this->processFacetResultsFromFieldConfig($facet, $facetResults); + } + + /** + * Populates label for facet values from allowed options field config. + * + * @param \Drupal\facets\Entity\Facet $facet + * The facet. + * @param array $facetResults + * The facet results. + * + * @return array + */ + private function processFacetResultsFromFieldConfig( + Facet $facet, + array $facetResults, + ): array { + if (!$this->bundle) { + return $facetResults; + } + + $fieldName = $facet->getFieldIdentifier(); + /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */ + // @phpstan-ignore-next-line + $entityFieldManager = \Drupal::service('entity_field.manager'); + $fieldConfig = $entityFieldManager->getFieldDefinitions('node', $this->bundle); + + if (isset($fieldConfig[$fieldName])) { + $allowedValues = options_allowed_values($fieldConfig[$fieldName]->getFieldStorageDefinition()); + + // Use order of allowedValues. + foreach ($facetResults as $key => $facetResult) { + $facetResults[$key]['label'] = $allowedValues[$facetResult['filter']] ?? $facetResult['filter']; + $facetResults[$key]['value'] = $facetResult['filter']; + } + + $allowedValueKeys = array_keys($allowedValues); + usort($facetResults, function ($a, $b) use ($allowedValueKeys) { + $indexA = array_search($a['filter'], $allowedValueKeys); + $indexB = array_search($b['filter'], $allowedValueKeys); + + return $indexA < $indexB ? -1 : 1; + }); + } + + return $facetResults; + } + +} diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponseInterface.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponseInterface.php new file mode 100644 index 000000000..7ca1ceef0 --- /dev/null +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponseInterface.php @@ -0,0 +1,17 @@ + Date: Wed, 14 Aug 2024 10:55:25 +0200 Subject: [PATCH 02/45] some code style fixes --- .../ThunderSearchApiSchemaExtension.php | 2 +- .../thunder_gqls/src/Wrappers/SearchApiResponse.php | 10 ++++++---- .../src/Wrappers/SearchApiResponseInterface.php | 1 + 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php index d1888202d..d4d6f0a7a 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php @@ -6,7 +6,7 @@ use Drupal\thunder_gqls\Wrappers\SearchApiResponse; /** - * + * Extension to add the search api query. */ class ThunderSearchApiSchemaExtension extends ThunderSchemaExtensionPluginBase { diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index 89606f6ce..b13c97ded 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -70,9 +70,7 @@ public function __construct(QueryInterface $query, mixed $facets = NULL, ?array } /** - * Get search facets. - * - * @return array + * {@inheritdoc} * * @throws \Drupal\search_api\SearchApiException */ @@ -102,10 +100,11 @@ public function facets(): array { * Get search result items. * * @return array|\GraphQL\Deferred + * The search result items. * * @throws \Drupal\search_api\SearchApiException */ - public function items() { + public function items(): array|Deferred { if (!$this->result) { $this->result = $this->query->execute(); } @@ -139,6 +138,7 @@ public function items() { * Returns the total results. * * @return int + * The total results. * * @throws \Drupal\search_api\SearchApiException */ @@ -159,6 +159,7 @@ public function total(): int { * The facet results. * * @return array + * The processed facet results. */ private function processFacetResults( Facet $facet, @@ -191,6 +192,7 @@ private function processFacetResults( * The facet results. * * @return array + * The processed facet results. */ private function processFacetResultsFromFieldConfig( Facet $facet, diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponseInterface.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponseInterface.php index 7ca1ceef0..82d2158aa 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponseInterface.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponseInterface.php @@ -11,6 +11,7 @@ interface SearchApiResponseInterface extends EntityListResponseInterface { * Get search facets. * * @return array + * The facets. */ public function facets(): array; From e4ef7b43b4df16985ea40f869bbd943accc4487c Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 14 Aug 2024 11:52:53 +0200 Subject: [PATCH 03/45] copy paste error --- modules/thunder_gqls/src/Wrappers/SearchApiResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index b13c97ded..471c43cfc 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -50,7 +50,7 @@ class SearchApiResponse implements SearchApiResponseInterface { private ?string $bundle; /** - * PlantfinderResult Constructor. + * SearchApiResponse Constructor. * * @param \Drupal\search_api\Query\QueryInterface $query * The search api query. From b2fbb04d7858121a57a2dfc131eb362ca0085854 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 14 Aug 2024 12:39:15 +0200 Subject: [PATCH 04/45] fix typos --- .../src/GraphQL/Buffers/SearchApiResultBuffer.php | 1 - .../GraphQL/DataProducer/ThunderSearchApiResultsProducer.php | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php b/modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php index bf2d2e738..354dfa53f 100644 --- a/modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php +++ b/modules/thunder_gqls/src/GraphQL/Buffers/SearchApiResultBuffer.php @@ -52,7 +52,6 @@ public function add($index, $id) { * {@inheritdoc} */ protected function getBufferId($item) { - // @phpstan-ignore-next-line return $item['index']; } diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php index fbe1ae44d..4f63318cd 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php @@ -147,13 +147,12 @@ protected function resolve( 'status' => TRUE, 'search_api_language' => $this->languageManager->getCurrentLanguage()->getId(), ]; - $conditions = array_merge($defaultConditions, $conditions); $query = $searchIndex->query(); foreach ($conditions as $field => $value) { - $searchIndex->addCondition($field, $value); + $query->addCondition($field, $value); } if (!empty($search)) { @@ -161,7 +160,6 @@ protected function resolve( } $query->range($offset, $limit); - $query->sort('search_api_relevance', QueryInterface::SORT_DESC); $cacheContext->addCacheTags($searchIndex->getCacheTags()); From 2fa07f5106c868ebe03614b200639c2b3e4bf565 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 14 Aug 2024 12:53:33 +0200 Subject: [PATCH 05/45] Ignore static error --- .../ThunderSearchApiSchemaExtension.php | 9 ++- phpstan-baseline.neon | 60 ------------------- phpstan.neon | 3 + 3 files changed, 11 insertions(+), 61 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php index d4d6f0a7a..c5a2ecb27 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php @@ -6,7 +6,14 @@ use Drupal\thunder_gqls\Wrappers\SearchApiResponse; /** - * Extension to add the search api query. + * The search api query schema extension. + * + * @SchemaExtension( + * id = "thunder_search_api", + * name = "Search API extension", + * description = "Adds search api queries.", + * schema = "thunder" + * ) */ class ThunderSearchApiSchemaExtension extends ThunderSchemaExtensionPluginBase { diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 3fcf0a4db..576ad8cca 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,55 +1,5 @@ parameters: ignoreErrors: - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_article/src/Form/NodeRevisionRevertDefaultForm.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_article/src/Plugin/Derivative/DynamicLocalTasks.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/EntityLinks.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/FocalPoint.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/MenuLinksActiveTrail.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/MetaTags.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntitySubRequestBase.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderImage.php - - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderRedirect.php - - message: "#^Access to an undefined property Drupal\\\\Core\\\\Entity\\\\ContentEntityInterface\\:\\:\\$field_teaser_media\\.$#" count: 1 @@ -70,11 +20,6 @@ parameters: count: 4 path: modules/thunder_gqls/tests/src/Kernel/DataProducer/EntityLinksTest.php - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: modules/thunder_taxonomy/src/ThunderTaxonomyPermissions.php - - message: "#^Access to an undefined property Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:\\$status\\.$#" count: 1 @@ -225,11 +170,6 @@ parameters: count: 1 path: tests/src/TestSuites/ThunderTestSuite.php - - - message: "#^Unsafe usage of new static\\(\\)\\.$#" - count: 1 - path: tests/src/TestSuites/ThunderTestSuite.php - - message: "#^Call to method id\\(\\) on an unknown class Drupal\\\\entity_browser\\\\Entity\\\\EntityBrowser\\.$#" count: 1 diff --git a/phpstan.neon b/phpstan.neon index 654ea7304..9c0cf382d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -4,5 +4,8 @@ parameters: checkMissingIterableValueType: false reportUnmatchedIgnoredErrors: true level: 6 + ignoreErrors: + # new static() is a best practice in Drupal, so we cannot fix that. + - "#^Unsafe usage of new static#" includes: - ./phpstan-baseline.neon From dac8332241b9fc4cc2093ea690565883d4affd92 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 14 Aug 2024 13:06:03 +0200 Subject: [PATCH 06/45] renaming --- ...nder_search.base.graphqls => thunder_search_api.base.graphqls} | 0 ...h.extension.graphqls => thunder_search_api.extension.graphqls} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename modules/thunder_gqls/graphql/{thunder_search.base.graphqls => thunder_search_api.base.graphqls} (100%) rename modules/thunder_gqls/graphql/{thunder_search.extension.graphqls => thunder_search_api.extension.graphqls} (100%) diff --git a/modules/thunder_gqls/graphql/thunder_search.base.graphqls b/modules/thunder_gqls/graphql/thunder_search_api.base.graphqls similarity index 100% rename from modules/thunder_gqls/graphql/thunder_search.base.graphqls rename to modules/thunder_gqls/graphql/thunder_search_api.base.graphqls diff --git a/modules/thunder_gqls/graphql/thunder_search.extension.graphqls b/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls similarity index 100% rename from modules/thunder_gqls/graphql/thunder_search.extension.graphqls rename to modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls From 10debcc4df67b0885b49796bf0f11b4c0c90e909 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 14 Aug 2024 15:42:04 +0200 Subject: [PATCH 07/45] Refactor base class --- .../thunder_search_api.extension.graphqls | 3 +- .../GraphQL/DataProducer/ThunderSearchApi.php | 87 +++++++++++++++++++ ...r.php => ThunderSearchApiProducerBase.php} | 61 ++++--------- .../ThunderSearchApiSchemaExtension.php | 4 +- 4 files changed, 109 insertions(+), 46 deletions(-) create mode 100644 modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php rename modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/{ThunderSearchApiResultsProducer.php => ThunderSearchApiProducerBase.php} (70%) diff --git a/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls b/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls index ff20dead6..4283a2da1 100644 --- a/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls +++ b/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls @@ -1,3 +1,4 @@ extend type Query { - search(search: String!, offset: Int = 0, limit: Int, index: String): SearchApiResult + search(search: String!, index: String!, offset: Int = 0, limit: Int!): SearchApiResult } + diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php new file mode 100644 index 000000000..f10b02fd8 --- /dev/null +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php @@ -0,0 +1,87 @@ +getQuery( + $limit, + $offset, + $index, + $conditions, + $search, + $cacheContext + ); + + return new SearchApiResponse($query); + } + +} diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php similarity index 70% rename from modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php rename to modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 4f63318cd..eefa5b722 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiResultsProducer.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -9,46 +9,15 @@ use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; use Drupal\search_api\Entity\Index; use Drupal\search_api\Query\QueryInterface; -use Drupal\thunder_gqls\Wrappers\SearchApiResponse; +use GraphQL\Error\UserError; use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Produces an entity list. - * - * @DataProducer( - * id = "thunder_search_api_results", - * name = @Translation("Entity list"), - * description = @Translation("Loads a list of entities."), - * produces = @ContextDefinition("any", - * label = @Translation("Entity list") - * ), - * consumes = { - * "limit" = @ContextDefinition("integer", - * label = @Translation("Limit"), - * required = TRUE - * ), - * "offset" = @ContextDefinition("integer", - * label = @Translation("Offset"), - * required = TRUE - * ), - * "index" = @ContextDefinition("string", - * label = @Translation("Search Api Index"), - * required = TRUE - * ), - * "conditions" = @ContextDefinition("map", - * label = @Translation("Filter conditions"), - * multiple = TRUE, - * required = FALSE, - * default_value = {} - * ), - * "search" = @ContextDefinition("string", - * label = @Translation("Search"), - * required = FALSE - * ), - * } - * ) + * The thunder base class for search api producers. */ -class ThunderSearchApiResultsProducer extends DataProducerPluginBase implements ContainerFactoryPluginInterface { +class ThunderSearchApiProducerBase extends DataProducerPluginBase implements ContainerFactoryPluginInterface { + + public const MAX_ITEMS = 100; /** * The entity type manager service. @@ -109,7 +78,7 @@ public function __construct( } /** - * Resolve entity query. + * Build base search api query. * * @param int $limit * Limit of the query. @@ -124,21 +93,26 @@ public function __construct( * @param \Drupal\graphql\GraphQL\Execution\FieldContext $cacheContext * The caching context related to the current field. * - * @return \Drupal\thunder_gqls\Wrappers\SearchApiResponse|null - * SearchApi result. + * @return \Drupal\search_api\Query\QueryInterface|null + * The query interface. * * @throws \Drupal\search_api\SearchApiException */ - protected function resolve( + protected function getQuery( int $limit, int $offset, string $index, ?array $conditions, ?string $search, FieldContext $cacheContext, - ): ?SearchApiResponse { - $searchIndex = Index::load($index); + ): ?QueryInterface { + if ($limit > static::MAX_ITEMS) { + throw new UserError( + sprintf('Exceeded maximum query limit: %s.', static::MAX_ITEMS) + ); + } + $searchIndex = Index::load($index); if (!$searchIndex) { return NULL; } @@ -147,6 +121,7 @@ protected function resolve( 'status' => TRUE, 'search_api_language' => $this->languageManager->getCurrentLanguage()->getId(), ]; + $conditions = array_merge($defaultConditions, $conditions); $query = $searchIndex->query(); @@ -165,7 +140,7 @@ protected function resolve( $cacheContext->addCacheTags($searchIndex->getCacheTags()); $cacheContext->addCacheContexts($searchIndex->getCacheContexts()); - return new SearchApiResponse($query); + return $query; } } diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php index c5a2ecb27..332453895 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php @@ -24,11 +24,11 @@ public function registerResolvers(ResolverRegistryInterface $registry): void { parent::registerResolvers($registry); $this->addFieldResolverIfNotExists('Query', 'search', - $this->builder->produce('thunder_search_api_results') + $this->builder->produce('thunder_search_api') ->map('search', $this->builder->fromArgument('search')) + ->map('index', $this->builder->fromArgument('index')) ->map('limit', $this->builder->fromArgument('limit')) ->map('offset', $this->builder->fromArgument('offset')) - ->map('index', $this->builder->fromArgument('index')) ); $this->addFieldResolverIfNotExists('SearchApiResult', 'total', From 1cb5e60f99b6394e4ff2ba166c9dcc7c58dc3fcb Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 14 Aug 2024 16:02:59 +0200 Subject: [PATCH 08/45] rename query --- .../src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php | 2 +- .../GraphQL/DataProducer/ThunderSearchApiProducerBase.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php index f10b02fd8..c824a81af 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php @@ -72,7 +72,7 @@ protected function resolve( ?string $search, FieldContext $cacheContext, ): ?SearchApiResponse { - $query = $this->getQuery( + $query = $this->buildBaseQuery( $limit, $offset, $index, diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index eefa5b722..89e74207c 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -98,7 +98,7 @@ public function __construct( * * @throws \Drupal\search_api\SearchApiException */ - protected function getQuery( + protected function buildBaseQuery( int $limit, int $offset, string $index, From 9e74496ad3298dfd965a15353eeda8133c282522 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 13:53:47 +0200 Subject: [PATCH 09/45] Improve create --- .../ThunderSearchApiProducerBase.php | 61 +++++++++++-------- .../thunder_gqls/thunder_gqls.services.yml | 3 +- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 89e74207c..b3318fffa 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -2,7 +2,7 @@ namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer; -use Drupal\Core\Entity\EntityTypeManager; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\graphql\GraphQL\Execution\FieldContext; @@ -15,16 +15,16 @@ /** * The thunder base class for search api producers. */ -class ThunderSearchApiProducerBase extends DataProducerPluginBase implements ContainerFactoryPluginInterface { +abstract class ThunderSearchApiProducerBase extends DataProducerPluginBase implements ContainerFactoryPluginInterface { public const MAX_ITEMS = 100; /** * The entity type manager service. * - * @var \Drupal\Core\Entity\EntityTypeManager + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected EntityTypeManager $entityTypeManager; + protected EntityTypeManagerInterface $entityTypeManager; /** * The language manager service. @@ -42,38 +42,39 @@ public static function create( $plugin_id, $plugin_definition, ): self { - return new static( + $instance = new static( $configuration, $plugin_id, $plugin_definition, - $container->get('entity_type.manager'), - $container->get('language_manager'), ); + + $instance->setEntityTypeManager($container->get('entity_type.manager')); + $instance->setLanguageManager($container->get('language_manager')); + + return $instance; } /** - * EntityLoad constructor. + * Set the entity type manager service. * - * @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 - * The entity type manager service. - * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager - * The language manager service. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * The entity type manager service. + * + * @return void */ - public function __construct( - array $configuration, - string $pluginId, - array $pluginDefinition, - EntityTypeManager $entityTypeManager, - LanguageManagerInterface $languageManager, - ) { - parent::__construct($configuration, $pluginId, $pluginDefinition); + public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void { $this->entityTypeManager = $entityTypeManager; + } + + /** + * Set the language manager service. + * + * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager + * The language manager service. + * + * @return void + */ + public function setLanguageManager(LanguageManagerInterface $languageManager): void { $this->languageManager = $languageManager; } @@ -106,6 +107,14 @@ protected function buildBaseQuery( ?string $search, FieldContext $cacheContext, ): ?QueryInterface { + + // Make sure offset is zero or positive. + $offset = max($offset, 0); + + // Make sure limit is positive and cap the max items. + if ($limit <= 0) { + $limit = 10; + } if ($limit > static::MAX_ITEMS) { throw new UserError( sprintf('Exceeded maximum query limit: %s.', static::MAX_ITEMS) diff --git a/modules/thunder_gqls/thunder_gqls.services.yml b/modules/thunder_gqls/thunder_gqls.services.yml index 9847f1b74..ca99675b4 100644 --- a/modules/thunder_gqls/thunder_gqls.services.yml +++ b/modules/thunder_gqls/thunder_gqls.services.yml @@ -1,4 +1,5 @@ services: + _defaults: + autowire: true thunder_gqls.buffer.search_api_result: class: Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer - arguments: ['@entity_type.manager'] From f418f0839b0ab8f7dd3fce66910b9087814278d3 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 13:55:26 +0200 Subject: [PATCH 10/45] code styles --- .../GraphQL/DataProducer/ThunderSearchApiProducerBase.php | 4 ++-- modules/thunder_gqls/src/Wrappers/SearchApiResponse.php | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index b3318fffa..0a399052e 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -58,7 +58,7 @@ public static function create( * Set the entity type manager service. * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager - * The entity type manager service. + * The entity type manager service. * * @return void */ @@ -70,7 +70,7 @@ public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManag * Set the language manager service. * * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager - * The language manager service. + * The language manager service. * * @return void */ diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index 471c43cfc..b85027d00 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -110,9 +110,7 @@ public function items(): array|Deferred { } // @phpstan-ignore-next-line - $searchApiResultBuffer = \Drupal::service( - 'thunder_gqls.buffer.search_api_result' - ); + $searchApiResultBuffer = \Drupal::service('thunder_gqls.buffer.search_api_result'); $ids = array_map(function ($item) { return $item->getId(); From 2d805be7200b4e14ad0e6d409e8a68cf9aa49338 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 15:12:37 +0200 Subject: [PATCH 11/45] add test --- .../DataProducer/ThunderSearchApiTest.php | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php diff --git a/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php b/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php new file mode 100644 index 000000000..8a684c258 --- /dev/null +++ b/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php @@ -0,0 +1,53 @@ +logWithRole('administrator'); + + $this->drupalGet('admin/config/search/search-api/index/content'); + $this->submitForm([], 'Index now'); + $this->assertSession()->statusCodeEquals(200); + $this->checkForMetaRefresh(); + + $result = $this->executeDataProducer('thunder_search_api', [ + 'index' => 'content', + 'search' => 'Drupal', + 'limit' => 10, + 'offset' => 0, + ]); + + $this->assertEquals(2, $result->total()); + + /** @var \GraphQL\Deferred $items */ + $items = $result->items(); + $items->runQueue(); + $this->assertCount(2, $items->result); + $this->assertEquals('Come to DrupalCon New Orleans', $items->result[0]->getTitle()); + } + +} From 9defacd818f5c1e898f72c6ec42f06e0b51e0f3b Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 16:17:59 +0200 Subject: [PATCH 12/45] remove void return --- .../GraphQL/DataProducer/ThunderSearchApiProducerBase.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 0a399052e..de1f9727d 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -59,8 +59,6 @@ public static function create( * * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager service. - * - * @return void */ public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void { $this->entityTypeManager = $entityTypeManager; @@ -71,8 +69,6 @@ public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManag * * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager * The language manager service. - * - * @return void */ public function setLanguageManager(LanguageManagerInterface $languageManager): void { $this->languageManager = $languageManager; From 528e18f82cc3a8f9976a53d5c8c090497e3bf3ba Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 16:22:29 +0200 Subject: [PATCH 13/45] replace deprecated phpstan config options --- phpstan.neon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 9c0cf382d..a604bc45c 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,11 +1,11 @@ parameters: customRulesetUsed: true - checkGenericClassInNonGenericObjectType: false - checkMissingIterableValueType: false reportUnmatchedIgnoredErrors: true level: 6 ignoreErrors: # new static() is a best practice in Drupal, so we cannot fix that. - "#^Unsafe usage of new static#" + - identifier: missingType.generics + - identifier: missingType.iterableValue includes: - ./phpstan-baseline.neon From f329c3e562ae884b2b5a89a4fbf2416837017eae Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 17:24:11 +0200 Subject: [PATCH 14/45] nit picks --- modules/thunder_gqls/src/Wrappers/SearchApiResponse.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index b85027d00..0e1fd145f 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -112,7 +112,7 @@ public function items(): array|Deferred { // @phpstan-ignore-next-line $searchApiResultBuffer = \Drupal::service('thunder_gqls.buffer.search_api_result'); - $ids = array_map(function ($item) { + $ids = array_map(static function ($item) { return $item->getId(); }, $this->result->getResultItems()); @@ -217,8 +217,8 @@ private function processFacetResultsFromFieldConfig( $allowedValueKeys = array_keys($allowedValues); usort($facetResults, function ($a, $b) use ($allowedValueKeys) { - $indexA = array_search($a['filter'], $allowedValueKeys); - $indexB = array_search($b['filter'], $allowedValueKeys); + $indexA = array_search($a['filter'], $allowedValueKeys, TRUE); + $indexB = array_search($b['filter'], $allowedValueKeys, TRUE); return $indexA < $indexB ? -1 : 1; }); From c7f455d5c2d3baf949258015c5c409279b70f036 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 17:54:05 +0200 Subject: [PATCH 15/45] refactor class instatiation --- .../GraphQL/DataProducer/ThunderSearchApi.php | 4 +- .../ThunderSearchApiProducerBase.php | 19 ++++ .../src/Wrappers/SearchApiResponse.php | 93 ++++++++++++++----- 3 files changed, 90 insertions(+), 26 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php index c824a81af..d3fc2924e 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php @@ -81,7 +81,9 @@ protected function resolve( $cacheContext ); - return new SearchApiResponse($query); + return $this->classResolver + ->getInstanceFromDefinition(SearchApiResponse::class) + ->setQuery($query); } } diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index de1f9727d..c0ab04b1a 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -2,6 +2,7 @@ namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer; +use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -33,6 +34,13 @@ abstract class ThunderSearchApiProducerBase extends DataProducerPluginBase imple */ protected LanguageManagerInterface $languageManager; + /** + * The class resolver service. + * + * @var \Drupal\Core\DependencyInjection\ClassResolverInterface + */ + protected ClassResolverInterface $classResolver; + /** * {@inheritdoc} */ @@ -50,6 +58,7 @@ public static function create( $instance->setEntityTypeManager($container->get('entity_type.manager')); $instance->setLanguageManager($container->get('language_manager')); + $instance->setClassResolver($container->get('class_resolver')); return $instance; } @@ -74,6 +83,16 @@ public function setLanguageManager(LanguageManagerInterface $languageManager): v $this->languageManager = $languageManager; } + /** + * Set the class resolver service. + * + * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $classResolver + * The class resolver service. + */ + public function setClassResolver(ClassResolverInterface $classResolver): void { + $this->classResolver = $classResolver; + } + /** * Build base search api query. * diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index 0e1fd145f..112b444b0 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -2,17 +2,21 @@ namespace Drupal\thunder_gqls\Wrappers; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; use Drupal\facets\Entity\Facet; use Drupal\search_api\Query\QueryInterface; use Drupal\search_api\Query\ResultSetInterface; +use Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer; use GraphQL\Deferred; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * SearchApi Result graphql wrapper. * * @package Drupal\thunder_gqls */ -class SearchApiResponse implements SearchApiResponseInterface { +class SearchApiResponse implements SearchApiResponseInterface, ContainerInjectionInterface { /** * The Search Api Query. @@ -31,42 +35,87 @@ class SearchApiResponse implements SearchApiResponseInterface { /** * Array of Facets. * - * @var ?array + * @var array */ - private ?array $facets; + protected array $facets; /** * Array of Facet mapping. * - * @var ?array + * @var array */ - private ?array $facetMapping; + protected array $facetMapping; /** * The bundle for fetching facet field information. * - * @var ?string + * @var string */ - private ?string $bundle; + protected string $bundle; /** * SearchApiResponse Constructor. * + * @param \Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer $buffer + * The search api result buffer. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager + * The entity type manager. + */ + public function __construct(protected SearchApiResultBuffer $buffer, protected EntityFieldManagerInterface $entityFieldManager) { + $this->result = NULL; + } + + /** + * {@inheritDoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('thunder_gqls.buffer.search_api_result'), + $container->get('entity_field.manager'), + ); + } + + /** + * Set query. + * * @param \Drupal\search_api\Query\QueryInterface $query - * The search api query. - * @param array|null $facets - * The facets. - * @param array|null $facetMapping - * The facet mapping. - * @param string|null $bundle - * The bundle. + * The query. */ - public function __construct(QueryInterface $query, mixed $facets = NULL, ?array $facetMapping = NULL, ?string $bundle = NULL) { + public function setQuery(QueryInterface $query): SearchApiResponse { $this->query = $query; - $this->result = NULL; - $this->facets = $facets; + return $this; + } + + /** + * Set Facet mapping. + * + * @param array $facetMapping + */ + public function setFacetMapping(array $facetMapping): SearchApiResponse { $this->facetMapping = $facetMapping; + return $this; + } + + /** + * Set bundle. + * + * @param string $bundle + * The bundle. + */ + public function setBundle(string $bundle): SearchApiResponse { $this->bundle = $bundle; + return $this; + } + + /** + * Set facets. + * + * @param array $facets + * The facets. + */ + public function setFacets(array $facets): SearchApiResponse { + $this->facets = $facets; + return $this; } /** @@ -109,9 +158,6 @@ public function items(): array|Deferred { $this->result = $this->query->execute(); } - // @phpstan-ignore-next-line - $searchApiResultBuffer = \Drupal::service('thunder_gqls.buffer.search_api_result'); - $ids = array_map(static function ($item) { return $item->getId(); }, $this->result->getResultItems()); @@ -122,7 +168,7 @@ public function items(): array|Deferred { return []; } - $callback = $searchApiResultBuffer->add( + $callback = $this->buffer->add( $this->query->getIndex()->id(), array_values($ids) ); @@ -201,10 +247,7 @@ private function processFacetResultsFromFieldConfig( } $fieldName = $facet->getFieldIdentifier(); - /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager */ - // @phpstan-ignore-next-line - $entityFieldManager = \Drupal::service('entity_field.manager'); - $fieldConfig = $entityFieldManager->getFieldDefinitions('node', $this->bundle); + $fieldConfig = $this->entityFieldManager->getFieldDefinitions('node', $this->bundle); if (isset($fieldConfig[$fieldName])) { $allowedValues = options_allowed_values($fieldConfig[$fieldName]->getFieldStorageDefinition()); From 8f13a780295f221bb4c8667acbd9a27432d2cf36 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 17:59:51 +0200 Subject: [PATCH 16/45] fix return type --- modules/thunder_gqls/src/Wrappers/SearchApiResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index 112b444b0..684be6f21 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -68,7 +68,7 @@ public function __construct(protected SearchApiResultBuffer $buffer, protected E /** * {@inheritDoc} */ - public static function create(ContainerInterface $container) { + public static function create(ContainerInterface $container): self { return new static( $container->get('thunder_gqls.buffer.search_api_result'), $container->get('entity_field.manager'), From b7aace2deaf5034c271c006be0d5f20a6b941494 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 18:03:41 +0200 Subject: [PATCH 17/45] cs --- modules/thunder_gqls/src/Wrappers/SearchApiResponse.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index 684be6f21..7edb78d9e 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -90,6 +90,7 @@ public function setQuery(QueryInterface $query): SearchApiResponse { * Set Facet mapping. * * @param array $facetMapping + * The facet mapping. */ public function setFacetMapping(array $facetMapping): SearchApiResponse { $this->facetMapping = $facetMapping; From 086313e492ccef0b1eaa64da8b7468f09485763b Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 18:07:08 +0200 Subject: [PATCH 18/45] Extract repose method --- .../GraphQL/DataProducer/ThunderSearchApi.php | 4 +--- .../ThunderSearchApiProducerBase.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php index d3fc2924e..969014844 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php @@ -81,9 +81,7 @@ protected function resolve( $cacheContext ); - return $this->classResolver - ->getInstanceFromDefinition(SearchApiResponse::class) - ->setQuery($query); + return $this->searchApiResponse($query); } } diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index c0ab04b1a..13aeb8a17 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -10,6 +10,7 @@ use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; use Drupal\search_api\Entity\Index; use Drupal\search_api\Query\QueryInterface; +use Drupal\thunder_gqls\Wrappers\SearchApiResponse; use GraphQL\Error\UserError; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -167,4 +168,19 @@ protected function buildBaseQuery( return $query; } + /** + * The search api response. + * + * @param \Drupal\search_api\Query\QueryInterface $query + * The search api query. + * + * @return \Drupal\thunder_gqls\Wrappers\SearchApiResponse + * The search api response. + */ + protected function searchApiResponse(QueryInterface $query): SearchApiResponse { + return $this->classResolver + ->getInstanceFromDefinition(SearchApiResponse::class) + ->setQuery($query); + } + } From eed23173741848019a268daff5d2f1f062383710 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Mon, 19 Aug 2024 18:08:39 +0200 Subject: [PATCH 19/45] make class resolver private --- .../GraphQL/DataProducer/ThunderSearchApiProducerBase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 13aeb8a17..1bfa6d0a6 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -40,7 +40,7 @@ abstract class ThunderSearchApiProducerBase extends DataProducerPluginBase imple * * @var \Drupal\Core\DependencyInjection\ClassResolverInterface */ - protected ClassResolverInterface $classResolver; + private ClassResolverInterface $classResolver; /** * {@inheritdoc} From 8a07c232e0ebfece33eefa9e1277f656fb140f57 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 11:34:43 +0200 Subject: [PATCH 20/45] refactor EntityListResponse --- .../src/Wrappers/EntityListResponse.php | 48 ++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 44ef51d07..703e1b10e 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -2,29 +2,62 @@ namespace Drupal\thunder_gqls\Wrappers; +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\Query\QueryInterface; +use Drupal\graphql\GraphQL\Buffers\EntityBuffer; use GraphQL\Deferred; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * The thunder entity list response class. */ -class EntityListResponse implements EntityListResponseInterface { +class EntityListResponse implements EntityListResponseInterface, ContainerInjectionInterface { /** * The query interface. * * @var \Drupal\Core\Entity\Query\QueryInterface */ - protected $query; + protected QueryInterface $query; + + /** + * The entity buffer. + * + * @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer + */ + private EntityBuffer $buffer; /** * EntityListResponse constructor. * + * @param \Drupal\Core\Entity\Query\QueryInterface|\Drupal\graphql\GraphQL\Buffers\EntityBuffer $parameter + */ + public function __construct(QueryInterface|EntityBuffer $parameter) { + if ($parameter instanceof QueryInterface) { + $this->setQuery($parameter); + return; + } + $this->buffer = $parameter; + } + + /** + * {@inheritDoc} + */ + public static function create(ContainerInterface $container): self { + return new static( + $container->get('graphql.buffer.entity'), + ); + } + + /** + * Set query. + * * @param \Drupal\Core\Entity\Query\QueryInterface $query - * The query interface. + * The query. */ - public function __construct(QueryInterface $query) { + public function setQuery(QueryInterface $query): EntityListResponse { $this->query = $query; + return $this; } /** @@ -51,8 +84,11 @@ public function items() { return []; } - $buffer = \Drupal::service('graphql.buffer.entity'); - $callback = $buffer->add($this->query->getEntityTypeId(), array_values($result)); + if (empty($this->buffer)) { + $this->buffer = \Drupal::service('graphql.buffer.entity'); + } + + $callback = $this->buffer->add($this->query->getEntityTypeId(), array_values($result)); return new Deferred(fn() => $callback()); } From c292e76bf8d79473d925939016001922fb40dd27 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 12:16:59 +0200 Subject: [PATCH 21/45] Use proper service --- .../ThunderSearchApiProducerBase.php | 20 +++++++++---------- .../src/Wrappers/EntityListResponse.php | 3 ++- .../src/Wrappers/SearchApiResponse.php | 3 +-- .../thunder_gqls/thunder_gqls.services.yml | 3 +++ 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 1bfa6d0a6..39456d1db 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -38,9 +38,9 @@ abstract class ThunderSearchApiProducerBase extends DataProducerPluginBase imple /** * The class resolver service. * - * @var \Drupal\Core\DependencyInjection\ClassResolverInterface + * @var \Drupal\thunder_gqls\Wrappers\SearchApiResponse */ - private ClassResolverInterface $classResolver; + private SearchApiResponse $responseWrapper; /** * {@inheritdoc} @@ -59,7 +59,7 @@ public static function create( $instance->setEntityTypeManager($container->get('entity_type.manager')); $instance->setLanguageManager($container->get('language_manager')); - $instance->setClassResolver($container->get('class_resolver')); + $instance->setResponseWrapper($container->get('thunder_gqls.search_api_response')); return $instance; } @@ -70,7 +70,7 @@ public static function create( * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager service. */ - public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void { + private function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void { $this->entityTypeManager = $entityTypeManager; } @@ -80,18 +80,18 @@ public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManag * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager * The language manager service. */ - public function setLanguageManager(LanguageManagerInterface $languageManager): void { + private function setLanguageManager(LanguageManagerInterface $languageManager): void { $this->languageManager = $languageManager; } /** * Set the class resolver service. * - * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $classResolver + * @param \Drupal\thunder_gqls\Wrappers\SearchApiResponse $responseWrapper * The class resolver service. */ - public function setClassResolver(ClassResolverInterface $classResolver): void { - $this->classResolver = $classResolver; + private function setResponseWrapper(SearchApiResponse $responseWrapper): void { + $this->responseWrapper = $responseWrapper; } /** @@ -178,9 +178,7 @@ protected function buildBaseQuery( * The search api response. */ protected function searchApiResponse(QueryInterface $query): SearchApiResponse { - return $this->classResolver - ->getInstanceFromDefinition(SearchApiResponse::class) - ->setQuery($query); + return $this->responseWrapper->setQuery($query); } } diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 703e1b10e..8e044d45e 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -18,7 +18,7 @@ class EntityListResponse implements EntityListResponseInterface, ContainerInject * * @var \Drupal\Core\Entity\Query\QueryInterface */ - protected QueryInterface $query; + private QueryInterface $query; /** * The entity buffer. @@ -34,6 +34,7 @@ class EntityListResponse implements EntityListResponseInterface, ContainerInject */ public function __construct(QueryInterface|EntityBuffer $parameter) { if ($parameter instanceof QueryInterface) { + @trigger_error('Calling the constructor with the query parameter is deprecated. Use service injection instead of directly instantiating and then use ::setQuery() instead.', E_USER_DEPRECATED); $this->setQuery($parameter); return; } diff --git a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php index 7edb78d9e..612c56d03 100644 --- a/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php +++ b/modules/thunder_gqls/src/Wrappers/SearchApiResponse.php @@ -30,7 +30,7 @@ class SearchApiResponse implements SearchApiResponseInterface, ContainerInjectio * * @var \Drupal\search_api\Query\ResultSetInterface|null */ - protected ?ResultSetInterface $result; + protected ?ResultSetInterface $result = NULL; /** * Array of Facets. @@ -62,7 +62,6 @@ class SearchApiResponse implements SearchApiResponseInterface, ContainerInjectio * The entity type manager. */ public function __construct(protected SearchApiResultBuffer $buffer, protected EntityFieldManagerInterface $entityFieldManager) { - $this->result = NULL; } /** diff --git a/modules/thunder_gqls/thunder_gqls.services.yml b/modules/thunder_gqls/thunder_gqls.services.yml index ca99675b4..e6e22b005 100644 --- a/modules/thunder_gqls/thunder_gqls.services.yml +++ b/modules/thunder_gqls/thunder_gqls.services.yml @@ -3,3 +3,6 @@ services: autowire: true thunder_gqls.buffer.search_api_result: class: Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer + Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer: '@thunder_gqls.buffer.search_api_result' + thunder_gqls.search_api_response: + class: Drupal\thunder_gqls\Wrappers\SearchApiResponse From 5b0e859c9738ca50fe581e3783b44c3a2e144863 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 12:35:12 +0200 Subject: [PATCH 22/45] refactor entity list response --- .../GraphQL/DataProducer/EntitiesWithTerm.php | 3 +- .../ThunderEntityListProducerBase.php | 81 +++++++++++++------ .../ThunderSearchApiProducerBase.php | 9 +-- .../thunder_gqls/thunder_gqls.services.yml | 4 +- 4 files changed, 64 insertions(+), 33 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/EntitiesWithTerm.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/EntitiesWithTerm.php index 8788b2539..9ac9953ae 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/EntitiesWithTerm.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/EntitiesWithTerm.php @@ -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; /** @@ -119,7 +118,7 @@ public function resolve(TermInterface $term, string $type, array $bundles, strin $cacheContext ); - return new EntityListResponse($query); + return $this->entityListResponse($query); } /** diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index 610700ab2..b955e48dd 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -3,11 +3,14 @@ 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 Drupal\thunder_gqls\Wrappers\SearchApiResponse; use GraphQL\Error\UserError; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -23,52 +26,67 @@ abstract class ThunderEntityListProducerBase extends DataProducerPluginBase impl * * @var \Drupal\Core\Entity\EntityTypeManager */ - protected $entityTypeManager; + protected EntityTypeManager $entityTypeManager; /** * The current user. * * @var \Drupal\Core\Session\AccountInterface */ - protected $currentUser; + protected AccountInterface $currentUser; + + /** + * The response wrapper service. + * + * @var \Drupal\thunder_gqls\Wrappers\SearchApiResponse + */ + private SearchApiResponse $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') + $plugin_definition ); + + $instance->setEntityTypeManager($container->get('entity_type.manager')); + $instance->setCurrentUser($container->get('current_user')); + $instance->setResponseWrapper($container->get('thunder_gqls.entity_list_response_wrapper')); + + return $instance; } /** - * EntityLoad constructor. + * Set the entity type manager service. * - * @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 - * The current user. */ - public function __construct( - array $configuration, - string $pluginId, - array $pluginDefinition, - EntityTypeManager $entityTypeManager, - AccountInterface $current_user, - ) { - parent::__construct($configuration, $pluginId, $pluginDefinition); + private 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. + */ + private function setCurrentUser(AccountInterface $currentUser): void { + $this->currentUser = $currentUser; + } + + /** + * Set the response wrapper service. + * + * @param \Drupal\thunder_gqls\Wrappers\SearchApiResponse $responseWrapper + * The response wrapper service. + */ + private function setResponseWrapper(SearchApiResponse $responseWrapper): void { + $this->responseWrapper = $responseWrapper; } /** @@ -202,4 +220,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 search api response. + * The entity list response. + */ + protected function entityListResponse(QueryInterface $query): EntityListResponse { + return $this->responseWrapper->setQuery($query); + } + } diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 39456d1db..949d17d3a 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -2,7 +2,6 @@ namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer; -use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; @@ -36,7 +35,7 @@ abstract class ThunderSearchApiProducerBase extends DataProducerPluginBase imple protected LanguageManagerInterface $languageManager; /** - * The class resolver service. + * The response wrapper service. * * @var \Drupal\thunder_gqls\Wrappers\SearchApiResponse */ @@ -59,7 +58,7 @@ public static function create( $instance->setEntityTypeManager($container->get('entity_type.manager')); $instance->setLanguageManager($container->get('language_manager')); - $instance->setResponseWrapper($container->get('thunder_gqls.search_api_response')); + $instance->setResponseWrapper($container->get('thunder_gqls.search_api_response_wrapper')); return $instance; } @@ -85,10 +84,10 @@ private function setLanguageManager(LanguageManagerInterface $languageManager): } /** - * Set the class resolver service. + * Set the response wrapper service. * * @param \Drupal\thunder_gqls\Wrappers\SearchApiResponse $responseWrapper - * The class resolver service. + * The response wrapper service. */ private function setResponseWrapper(SearchApiResponse $responseWrapper): void { $this->responseWrapper = $responseWrapper; diff --git a/modules/thunder_gqls/thunder_gqls.services.yml b/modules/thunder_gqls/thunder_gqls.services.yml index e6e22b005..98ed22f70 100644 --- a/modules/thunder_gqls/thunder_gqls.services.yml +++ b/modules/thunder_gqls/thunder_gqls.services.yml @@ -4,5 +4,7 @@ services: thunder_gqls.buffer.search_api_result: class: Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer Drupal\thunder_gqls\GraphQL\Buffers\SearchApiResultBuffer: '@thunder_gqls.buffer.search_api_result' - thunder_gqls.search_api_response: + thunder_gqls.search_api_response_wrapper: class: Drupal\thunder_gqls\Wrappers\SearchApiResponse + thunder_gqls.entity_list_response_wrapper: + class: Drupal\thunder_gqls\Wrappers\EntityListResponse From b120f140ac3b5109b8209971954757b5e26587f3 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 12:41:29 +0200 Subject: [PATCH 23/45] Needs to be public... --- .../GraphQL/DataProducer/ThunderEntityListProducerBase.php | 6 +++--- .../GraphQL/DataProducer/ThunderSearchApiProducerBase.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index b955e48dd..f97b03715 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -65,7 +65,7 @@ public static function create(ContainerInterface $container, array $configuratio * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager service. */ - private function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void { + public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void { $this->entityTypeManager = $entityTypeManager; } @@ -75,7 +75,7 @@ private function setEntityTypeManager(EntityTypeManagerInterface $entityTypeMana * @param \Drupal\Core\Session\AccountInterface $currentUser * The current user. */ - private function setCurrentUser(AccountInterface $currentUser): void { + public function setCurrentUser(AccountInterface $currentUser): void { $this->currentUser = $currentUser; } @@ -85,7 +85,7 @@ private function setCurrentUser(AccountInterface $currentUser): void { * @param \Drupal\thunder_gqls\Wrappers\SearchApiResponse $responseWrapper * The response wrapper service. */ - private function setResponseWrapper(SearchApiResponse $responseWrapper): void { + public function setResponseWrapper(SearchApiResponse $responseWrapper): void { $this->responseWrapper = $responseWrapper; } diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 949d17d3a..2bfe3b29e 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -69,7 +69,7 @@ public static function create( * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager service. */ - private function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void { + public function setEntityTypeManager(EntityTypeManagerInterface $entityTypeManager): void { $this->entityTypeManager = $entityTypeManager; } @@ -79,7 +79,7 @@ private function setEntityTypeManager(EntityTypeManagerInterface $entityTypeMana * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager * The language manager service. */ - private function setLanguageManager(LanguageManagerInterface $languageManager): void { + public function setLanguageManager(LanguageManagerInterface $languageManager): void { $this->languageManager = $languageManager; } @@ -89,7 +89,7 @@ private function setLanguageManager(LanguageManagerInterface $languageManager): * @param \Drupal\thunder_gqls\Wrappers\SearchApiResponse $responseWrapper * The response wrapper service. */ - private function setResponseWrapper(SearchApiResponse $responseWrapper): void { + public function setResponseWrapper(SearchApiResponse $responseWrapper): void { $this->responseWrapper = $responseWrapper; } From 6067749164c874ebb0ac095d24b1a6f02ce00ee6 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 13:36:59 +0200 Subject: [PATCH 24/45] coding style --- .../GraphQL/DataProducer/ThunderEntityListProducerBase.php | 2 +- modules/thunder_gqls/src/Wrappers/EntityListResponse.php | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index f97b03715..ea35ee611 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -226,7 +226,7 @@ protected function createPublishedCondition(string $type, array $conditions) { * @param \Drupal\Core\Entity\Query\QueryInterface $query * The entity query. * - * @return \Drupal\thunder_gqls\Wrappers\EntityListResponse The search api response. + * @return \Drupal\thunder_gqls\Wrappers\EntityListResponse * The entity list response. */ protected function entityListResponse(QueryInterface $query): EntityListResponse { diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 8e044d45e..22b0ed7bf 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -31,10 +31,12 @@ class EntityListResponse implements EntityListResponseInterface, ContainerInject * EntityListResponse constructor. * * @param \Drupal\Core\Entity\Query\QueryInterface|\Drupal\graphql\GraphQL\Buffers\EntityBuffer $parameter + * The query or buffer parameter. */ public function __construct(QueryInterface|EntityBuffer $parameter) { if ($parameter instanceof QueryInterface) { - @trigger_error('Calling the constructor with the query parameter is deprecated. Use service injection instead of directly instantiating and then use ::setQuery() instead.', E_USER_DEPRECATED); + // phpcs:ignore + @trigger_error('Calling the constructor with the query parameter is deprecated in Thunder 7.3.3 it will be remove in Thunder 8.0. Use service injection instead of directly instantiating and then use ::setQuery() instead.', E_USER_DEPRECATED); $this->setQuery($parameter); return; } @@ -86,6 +88,7 @@ public function items() { } if (empty($this->buffer)) { + // phpcs:ignore $this->buffer = \Drupal::service('graphql.buffer.entity'); } From 852eed0a432d5563561365ca50b242c1dec9febe Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 13:45:21 +0200 Subject: [PATCH 25/45] Do not use deprecated call --- .../src/Plugin/GraphQL/DataProducer/ThunderEntityList.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php index 3afcc2965..185e65a4e 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php @@ -97,7 +97,7 @@ protected function resolve(string $type, array $bundles, int $offset, int $limit $cacheContext ); - return new EntityListResponse($query); + return $this->entityListResponse($query); } } From 40772cde9ca9db3ae16176a65b83239efd47e2b5 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 13:46:24 +0200 Subject: [PATCH 26/45] remove unused use --- .../src/Plugin/GraphQL/DataProducer/ThunderEntityList.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php index 185e65a4e..43ea0aa03 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityList.php @@ -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; /** From 2461712bdbfe54a577eca57e1338d65c9919d57e Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 13:52:49 +0200 Subject: [PATCH 27/45] fix some classes --- .../DataProducer/ThunderEntityListProducerBase.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index ea35ee611..fdcf7ccc4 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -2,7 +2,6 @@ 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; @@ -24,9 +23,9 @@ abstract class ThunderEntityListProducerBase extends DataProducerPluginBase impl /** * The entity type manager service. * - * @var \Drupal\Core\Entity\EntityTypeManager + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected EntityTypeManager $entityTypeManager; + protected EntityTypeManagerInterface $entityTypeManager; /** * The current user. @@ -82,10 +81,10 @@ public function setCurrentUser(AccountInterface $currentUser): void { /** * Set the response wrapper service. * - * @param \Drupal\thunder_gqls\Wrappers\SearchApiResponse $responseWrapper + * @param \Drupal\thunder_gqls\Wrappers\EntityListResponse $responseWrapper * The response wrapper service. */ - public function setResponseWrapper(SearchApiResponse $responseWrapper): void { + public function setResponseWrapper(EntityListResponse $responseWrapper): void { $this->responseWrapper = $responseWrapper; } From 191dff7fc26a0aa5ee0dc8100b7c3a3bb2995e0a Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 13:58:56 +0200 Subject: [PATCH 28/45] even more --- .../GraphQL/DataProducer/ThunderEntityListProducerBase.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index fdcf7ccc4..2b815e3db 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -9,7 +9,6 @@ use Drupal\graphql\GraphQL\Execution\FieldContext; use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase; use Drupal\thunder_gqls\Wrappers\EntityListResponse; -use Drupal\thunder_gqls\Wrappers\SearchApiResponse; use GraphQL\Error\UserError; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -37,9 +36,9 @@ abstract class ThunderEntityListProducerBase extends DataProducerPluginBase impl /** * The response wrapper service. * - * @var \Drupal\thunder_gqls\Wrappers\SearchApiResponse + * @var \Drupal\thunder_gqls\Wrappers\EntityListResponse */ - private SearchApiResponse $responseWrapper; + private EntityListResponse $responseWrapper; /** * {@inheritdoc} From 4cd599b7746ac53f6314f1e3a558d5418c89e06f Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 14:08:35 +0200 Subject: [PATCH 29/45] do not autowire --- modules/thunder_gqls/thunder_gqls.services.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/thunder_gqls/thunder_gqls.services.yml b/modules/thunder_gqls/thunder_gqls.services.yml index 98ed22f70..91bb26c44 100644 --- a/modules/thunder_gqls/thunder_gqls.services.yml +++ b/modules/thunder_gqls/thunder_gqls.services.yml @@ -7,4 +7,5 @@ services: thunder_gqls.search_api_response_wrapper: class: Drupal\thunder_gqls\Wrappers\SearchApiResponse thunder_gqls.entity_list_response_wrapper: + autowire: false class: Drupal\thunder_gqls\Wrappers\EntityListResponse From 0733c09038523b296e1c8cee221f34a76a42e56f Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 14:35:17 +0200 Subject: [PATCH 30/45] add arguments --- modules/thunder_gqls/thunder_gqls.services.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/thunder_gqls/thunder_gqls.services.yml b/modules/thunder_gqls/thunder_gqls.services.yml index 91bb26c44..863299712 100644 --- a/modules/thunder_gqls/thunder_gqls.services.yml +++ b/modules/thunder_gqls/thunder_gqls.services.yml @@ -9,3 +9,4 @@ services: thunder_gqls.entity_list_response_wrapper: autowire: false class: Drupal\thunder_gqls\Wrappers\EntityListResponse + arguments: [ '@graphql.buffer.entity' ] From 41fe1fd2031a21e5ced2d63d10b5a60d2a52e8a4 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 17:58:51 +0200 Subject: [PATCH 31/45] Add sortBy parameter --- .../GraphQL/DataProducer/ThunderSearchApi.php | 34 +++++++++++++++++++ .../ThunderSearchApiProducerBase.php | 18 +++++----- .../DataProducer/ThunderSearchApiTest.php | 29 ++++++++++++---- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php index 969014844..43a1e636d 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php @@ -3,6 +3,7 @@ namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer; use Drupal\graphql\GraphQL\Execution\FieldContext; +use Drupal\search_api\Query\QueryInterface; use Drupal\thunder_gqls\Wrappers\SearchApiResponse; /** @@ -28,6 +29,12 @@ * label = @Translation("Search Api Index"), * required = TRUE * ), + * "sortBy" = @ContextDefinition("map", + * label = @Translation("Sorts"), + * multiple = TRUE, + * required = FALSE, + * default_value = {} + * ), * "conditions" = @ContextDefinition("map", * label = @Translation("Filter conditions"), * multiple = TRUE, @@ -52,6 +59,8 @@ class ThunderSearchApi extends ThunderSearchApiProducerBase { * Offset of the query. * @param string $index * Id of the search api index. + * @param array|null $sortBy + * List of sorts. * @param array|null $conditions * List of conditions to filter the result. * @param string|null $search @@ -68,14 +77,39 @@ protected function resolve( int $limit, int $offset, string $index, + ?array $sortBy, ?array $conditions, ?string $search, FieldContext $cacheContext, ): ?SearchApiResponse { + + // Add default conditions. + $conditions = $conditions ?: [ + [ + 'field' => 'status', + 'value' => TRUE, + 'operator' => '=', + ], + [ + 'field' => 'search_api_language', + 'value' => $this->languageManager->getCurrentLanguage()->getId(), + 'operator' => '=', + ], + ]; + + // Add default sorts. + $sortBy = $sortBy ?: [ + [ + 'field' => 'search_api_relevance', + 'direction' => QueryInterface::SORT_DESC, + ], + ]; + $query = $this->buildBaseQuery( $limit, $offset, $index, + $sortBy, $conditions, $search, $cacheContext diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 2bfe3b29e..39c5fc0dd 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -118,6 +118,7 @@ protected function buildBaseQuery( int $limit, int $offset, string $index, + ?array $sortBy, ?array $conditions, ?string $search, FieldContext $cacheContext, @@ -141,17 +142,15 @@ protected function buildBaseQuery( return NULL; } - $defaultConditions = [ - 'status' => TRUE, - 'search_api_language' => $this->languageManager->getCurrentLanguage()->getId(), - ]; - - $conditions = array_merge($defaultConditions, $conditions); - $query = $searchIndex->query(); - foreach ($conditions as $field => $value) { - $query->addCondition($field, $value); + foreach ($conditions as $condition) { + $query->addCondition($condition['field'], $condition['value'], $condition['operator']); + } + + foreach ($sortBy as $sort) { + $direction = $sort['direction'] ?? QueryInterface::SORT_ASC; + $query->sort($sort['field'], $direction); } if (!empty($search)) { @@ -159,7 +158,6 @@ protected function buildBaseQuery( } $query->range($offset, $limit); - $query->sort('search_api_relevance', QueryInterface::SORT_DESC); $cacheContext->addCacheTags($searchIndex->getCacheTags()); $cacheContext->addCacheContexts($searchIndex->getCacheContexts()); diff --git a/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php b/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php index 8a684c258..52fce2598 100644 --- a/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php +++ b/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\thunder_gqls\Functional\DataProducer; +use Drupal\search_api\Query\QueryInterface; use Drupal\Tests\graphql\Traits\DataProducerExecutionTrait; use Drupal\Tests\thunder_gqls\Functional\ThunderGqlsTestBase; @@ -34,20 +35,36 @@ public function testThunderSearchApi(): void { $this->assertSession()->statusCodeEquals(200); $this->checkForMetaRefresh(); - $result = $this->executeDataProducer('thunder_search_api', [ + $options = [ 'index' => 'content', - 'search' => 'Drupal', + 'search' => 'the', 'limit' => 10, 'offset' => 0, - ]); + ]; - $this->assertEquals(2, $result->total()); + $result = $this->executeDataProducer('thunder_search_api', $options); + $this->assertEquals(3, $result->total()); + + /** @var \GraphQL\Deferred $items */ + $items = $result->items(); + $items->runQueue(); + $this->assertEquals('Burda Launches Open-Source CMS Thunder', $items->result[0]->getTitle()); + + // Change sort order. + $options['sortBy'] = [ + [ + 'field' => 'search_api_relevance', + 'direction' => QueryInterface::SORT_ASC, + ], + ]; + + $this->container->get('kernel')->rebuildContainer(); + $result = $this->executeDataProducer('thunder_search_api', $options); /** @var \GraphQL\Deferred $items */ $items = $result->items(); $items->runQueue(); - $this->assertCount(2, $items->result); - $this->assertEquals('Come to DrupalCon New Orleans', $items->result[0]->getTitle()); + $this->assertEquals('Legal notice', $items->result[0]->getTitle()); } } From 2c5d2e004b490005cd2ff1e0945253d0f064dde0 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 18:04:36 +0200 Subject: [PATCH 32/45] cs --- .../GraphQL/DataProducer/ThunderSearchApiProducerBase.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index 39c5fc0dd..db4debb74 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -102,6 +102,8 @@ public function setResponseWrapper(SearchApiResponse $responseWrapper): void { * Offset of the query. * @param string $index * Id of the search api index. + * @param array|null $sortBy + * List of sorts. * @param array|null $conditions * List of conditions to filter the result. * @param string|null $search From 062da1bdfaf0f4ef1c6fa6fd61c232c9cfba3588 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Tue, 20 Aug 2024 18:11:34 +0200 Subject: [PATCH 33/45] Remove default endpoint --- .../graphql/thunder_search_api.extension.graphqls | 4 ---- 1 file changed, 4 deletions(-) diff --git a/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls b/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls index 4283a2da1..e69de29bb 100644 --- a/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls +++ b/modules/thunder_gqls/graphql/thunder_search_api.extension.graphqls @@ -1,4 +0,0 @@ -extend type Query { - search(search: String!, index: String!, offset: Int = 0, limit: Int!): SearchApiResult -} - From 3a8cb2d7e5882c89ad1406b90deeeee9561503b8 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 21 Aug 2024 09:52:50 +0200 Subject: [PATCH 34/45] Improve language selection --- .../src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php index 43a1e636d..ef5215cab 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php @@ -2,6 +2,7 @@ namespace Drupal\thunder_gqls\Plugin\GraphQL\DataProducer; +use Drupal\Core\Language\LanguageInterface; use Drupal\graphql\GraphQL\Execution\FieldContext; use Drupal\search_api\Query\QueryInterface; use Drupal\thunder_gqls\Wrappers\SearchApiResponse; @@ -92,7 +93,7 @@ protected function resolve( ], [ 'field' => 'search_api_language', - 'value' => $this->languageManager->getCurrentLanguage()->getId(), + 'value' => $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)->getId(), 'operator' => '=', ], ]; From e69fa275f01c762ee611590fa2dd2e6e1c04cb93 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 21 Aug 2024 10:40:42 +0200 Subject: [PATCH 35/45] add condition test --- .../DataProducer/ThunderSearchApiTest.php | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php b/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php index 52fce2598..b29faaa49 100644 --- a/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php +++ b/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php @@ -43,10 +43,8 @@ public function testThunderSearchApi(): void { ]; $result = $this->executeDataProducer('thunder_search_api', $options); - $this->assertEquals(3, $result->total()); - /** @var \GraphQL\Deferred $items */ $items = $result->items(); $items->runQueue(); $this->assertEquals('Burda Launches Open-Source CMS Thunder', $items->result[0]->getTitle()); @@ -61,10 +59,27 @@ public function testThunderSearchApi(): void { $this->container->get('kernel')->rebuildContainer(); $result = $this->executeDataProducer('thunder_search_api', $options); - /** @var \GraphQL\Deferred $items */ + $items = $result->items(); $items->runQueue(); $this->assertEquals('Legal notice', $items->result[0]->getTitle()); + + // Get articles only + $options['conditions'] = [ + [ + 'field' => 'type', + 'value' => 'article', + 'operator' => '=', + ], + ]; + + $this->container->get('kernel')->rebuildContainer(); + $result = $this->executeDataProducer('thunder_search_api', $options); + + $items = $result->items(); + $items->runQueue(); + $this->assertEquals('Come to DrupalCon New Orleans', $items->result[0]->getTitle()); + } } From cd01309d11c89d17d249cdf66054f8ed1de7a684 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 21 Aug 2024 10:51:29 +0200 Subject: [PATCH 36/45] remove search schema extension --- .../SchemaExtension/ThunderSearchApiSchemaExtension.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php index 332453895..dc9c2fbeb 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/SchemaExtension/ThunderSearchApiSchemaExtension.php @@ -23,14 +23,6 @@ class ThunderSearchApiSchemaExtension extends ThunderSchemaExtensionPluginBase { public function registerResolvers(ResolverRegistryInterface $registry): void { parent::registerResolvers($registry); - $this->addFieldResolverIfNotExists('Query', 'search', - $this->builder->produce('thunder_search_api') - ->map('search', $this->builder->fromArgument('search')) - ->map('index', $this->builder->fromArgument('index')) - ->map('limit', $this->builder->fromArgument('limit')) - ->map('offset', $this->builder->fromArgument('offset')) - ); - $this->addFieldResolverIfNotExists('SearchApiResult', 'total', $this->builder->callback(function (SearchApiResponse $result) { return $result->total(); From 333388ab05fee1cb1aae0f1da32c5121e443d7e6 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 21 Aug 2024 10:55:59 +0200 Subject: [PATCH 37/45] cs fixes --- .../tests/src/Functional/DataProducer/ThunderSearchApiTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php b/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php index b29faaa49..a6831e38f 100644 --- a/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php +++ b/modules/thunder_gqls/tests/src/Functional/DataProducer/ThunderSearchApiTest.php @@ -64,7 +64,7 @@ public function testThunderSearchApi(): void { $items->runQueue(); $this->assertEquals('Legal notice', $items->result[0]->getTitle()); - // Get articles only + // Get articles only. $options['conditions'] = [ [ 'field' => 'type', From 4c8a089bd7d82c31671e728310b742bdc34972c3 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 21 Aug 2024 12:59:10 +0200 Subject: [PATCH 38/45] Update modules/thunder_gqls/src/Wrappers/EntityListResponse.php Co-authored-by: alexpott --- .../thunder_gqls/src/Wrappers/EntityListResponse.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 22b0ed7bf..70a346575 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -33,14 +33,14 @@ class EntityListResponse implements EntityListResponseInterface, ContainerInject * @param \Drupal\Core\Entity\Query\QueryInterface|\Drupal\graphql\GraphQL\Buffers\EntityBuffer $parameter * The query or buffer parameter. */ - public function __construct(QueryInterface|EntityBuffer $parameter) { - if ($parameter instanceof QueryInterface) { + public function __construct(QueryInterface|EntityBuffer $buffer) { + if ($buffer instanceof QueryInterface) { // phpcs:ignore @trigger_error('Calling the constructor with the query parameter is deprecated in Thunder 7.3.3 it will be remove in Thunder 8.0. Use service injection instead of directly instantiating and then use ::setQuery() instead.', E_USER_DEPRECATED); - $this->setQuery($parameter); - return; + $this->setQuery($buffer); + $buffer = \Drupal::service('graphql.buffer.entity'); } - $this->buffer = $parameter; + $this->buffer = $buffer; } /** From bc2f45062410ab146a253f12cacb9cd8e7d5c349 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 21 Aug 2024 12:59:38 +0200 Subject: [PATCH 39/45] Update modules/thunder_gqls/src/Wrappers/EntityListResponse.php Co-authored-by: alexpott --- modules/thunder_gqls/src/Wrappers/EntityListResponse.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 70a346575..52004a378 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -87,11 +87,6 @@ public function items() { return []; } - if (empty($this->buffer)) { - // phpcs:ignore - $this->buffer = \Drupal::service('graphql.buffer.entity'); - } - $callback = $this->buffer->add($this->query->getEntityTypeId(), array_values($result)); return new Deferred(fn() => $callback()); } From 97967b2420cd611a974352b3a0e20f59cce6291e Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 21 Aug 2024 13:44:51 +0200 Subject: [PATCH 40/45] addressing issues --- .../GraphQL/DataProducer/ThunderEntityListProducerBase.php | 5 +---- .../src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php | 4 ++-- .../GraphQL/DataProducer/ThunderSearchApiProducerBase.php | 4 +--- modules/thunder_gqls/src/Wrappers/EntityListResponse.php | 2 +- 4 files changed, 5 insertions(+), 10 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index 2b815e3db..4cbce68ef 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -179,10 +179,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; } diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php index ef5215cab..d26ecc9c6 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApi.php @@ -8,14 +8,14 @@ use Drupal\thunder_gqls\Wrappers\SearchApiResponse; /** - * Produces an entity list. + * Produces a search api response from a search query. * * @DataProducer( * id = "thunder_search_api", * name = @Translation("Search api query"), * description = @Translation("Loads a list of entities from search api."), * produces = @ContextDefinition("any", - * label = @Translation("Entity list") + * label = @Translation("Search API Response") * ), * consumes = { * "limit" = @ContextDefinition("integer", diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index db4debb74..e568b6971 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -160,9 +160,7 @@ protected function buildBaseQuery( } $query->range($offset, $limit); - - $cacheContext->addCacheTags($searchIndex->getCacheTags()); - $cacheContext->addCacheContexts($searchIndex->getCacheContexts()); + $cacheContext->addCacheableDependency($searchIndex); return $query; } diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 52004a378..52df4f049 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -30,7 +30,7 @@ class EntityListResponse implements EntityListResponseInterface, ContainerInject /** * EntityListResponse constructor. * - * @param \Drupal\Core\Entity\Query\QueryInterface|\Drupal\graphql\GraphQL\Buffers\EntityBuffer $parameter + * @param \Drupal\Core\Entity\Query\QueryInterface|\Drupal\graphql\GraphQL\Buffers\EntityBuffer $buffer * The query or buffer parameter. */ public function __construct(QueryInterface|EntityBuffer $buffer) { From 5325c9a3b89617b9c0655d0fea1e1ccc0bdacbca Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Wed, 21 Aug 2024 14:22:04 +0200 Subject: [PATCH 41/45] Ignore cs --- modules/thunder_gqls/src/Wrappers/EntityListResponse.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 52df4f049..3dab0c3b5 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -38,6 +38,7 @@ public function __construct(QueryInterface|EntityBuffer $buffer) { // phpcs:ignore @trigger_error('Calling the constructor with the query parameter is deprecated in Thunder 7.3.3 it will be remove in Thunder 8.0. Use service injection instead of directly instantiating and then use ::setQuery() instead.', E_USER_DEPRECATED); $this->setQuery($buffer); + // phpcs:ignore $buffer = \Drupal::service('graphql.buffer.entity'); } $this->buffer = $buffer; From 226562234c10e57f2501df9ea15fbd5c90282892 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Thu, 22 Aug 2024 11:41:55 +0200 Subject: [PATCH 42/45] Improve bc --- .../ThunderEntityListProducerBase.php | 35 ++++++++++++++++--- .../src/Wrappers/EntityListResponse.php | 4 +-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index 4cbce68ef..283f06b2f 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -38,7 +38,34 @@ abstract class ThunderEntityListProducerBase extends DataProducerPluginBase impl * * @var \Drupal\thunder_gqls\Wrappers\EntityListResponse */ - private EntityListResponse $responseWrapper; + protected EntityListResponse $responseWrapper; + + /** + * 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\EntityTypeManagerInterface $entityTypeManager + * The entity type manager service. + * @param \Drupal\Core\Session\AccountInterface $currentUser + * The current user. + */ + public function __construct( + array $configuration, + string $pluginId, + array $pluginDefinition, + EntityTypeManagerInterface $entityTypeManager, + AccountInterface $currentUser, + ) { + parent::__construct($configuration, $pluginId, $pluginDefinition); + $this->setEntityTypeManager($entityTypeManager); + $this->setCurrentUser($currentUser); + } + /** * {@inheritdoc} @@ -47,11 +74,11 @@ public static function create(ContainerInterface $container, array $configuratio $instance = new static( $configuration, $plugin_id, - $plugin_definition + $plugin_definition, + $container->get('entity_type.manager'), + $container->get('current_user') ); - $instance->setEntityTypeManager($container->get('entity_type.manager')); - $instance->setCurrentUser($container->get('current_user')); $instance->setResponseWrapper($container->get('thunder_gqls.entity_list_response_wrapper')); return $instance; diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 3dab0c3b5..4a8b2d605 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -18,14 +18,14 @@ class EntityListResponse implements EntityListResponseInterface, ContainerInject * * @var \Drupal\Core\Entity\Query\QueryInterface */ - private QueryInterface $query; + protected QueryInterface $query; /** * The entity buffer. * * @var \Drupal\graphql\GraphQL\Buffers\EntityBuffer */ - private EntityBuffer $buffer; + protected EntityBuffer $buffer; /** * EntityListResponse constructor. From 2b1f74d6ff10aa77e69172c8c5fc1099b2167232 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Thu, 22 Aug 2024 11:43:45 +0200 Subject: [PATCH 43/45] cs --- .../GraphQL/DataProducer/ThunderEntityListProducerBase.php | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index 283f06b2f..e2bda8abf 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -66,7 +66,6 @@ public function __construct( $this->setCurrentUser($currentUser); } - /** * {@inheritdoc} */ From 76b66ae15330dc00a49a6c030ee20581afe1e1cd Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Thu, 22 Aug 2024 11:47:47 +0200 Subject: [PATCH 44/45] simplify diff --- .../ThunderEntityListProducerBase.php | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php index e2bda8abf..c2ad6bbe8 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderEntityListProducerBase.php @@ -40,6 +40,23 @@ abstract class ThunderEntityListProducerBase extends DataProducerPluginBase impl */ protected EntityListResponse $responseWrapper; + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self { + $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; + } + /** * ThunderEntityListProducerBase constructor. * @@ -66,23 +83,6 @@ public function __construct( $this->setCurrentUser($currentUser); } - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self { - $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; - } - /** * Set the entity type manager service. * From 1d8c21780fa003bae49151195de9489fcc968766 Mon Sep 17 00:00:00 2001 From: Daniel Bosen Date: Thu, 22 Aug 2024 14:27:23 +0200 Subject: [PATCH 45/45] Update modules/thunder_gqls/src/Wrappers/EntityListResponse.php Co-authored-by: Volker Killesreiter --- modules/thunder_gqls/src/Wrappers/EntityListResponse.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php index 4a8b2d605..4b25e5044 100644 --- a/modules/thunder_gqls/src/Wrappers/EntityListResponse.php +++ b/modules/thunder_gqls/src/Wrappers/EntityListResponse.php @@ -36,7 +36,7 @@ class EntityListResponse implements EntityListResponseInterface, ContainerInject public function __construct(QueryInterface|EntityBuffer $buffer) { if ($buffer instanceof QueryInterface) { // phpcs:ignore - @trigger_error('Calling the constructor with the query parameter is deprecated in Thunder 7.3.3 it will be remove in Thunder 8.0. Use service injection instead of directly instantiating and then use ::setQuery() instead.', E_USER_DEPRECATED); + @trigger_error('Calling the constructor with a query parameter is deprecated in Thunder 7.3.3 and will be removed in Thunder 8.0. Use service injection and ::setQuery() instead.', E_USER_DEPRECATED); $this->setQuery($buffer); // phpcs:ignore $buffer = \Drupal::service('graphql.buffer.entity');