From d9bb3eb54423cb7a65f743c3b7d18a92611c181f Mon Sep 17 00:00:00 2001 From: Killesreiter Volker Date: Mon, 9 Dec 2024 18:00:33 +0100 Subject: [PATCH 1/3] load entity types and add cache tags and contexts --- .../GraphQL/DataProducer/ThunderSearchApiProducerBase.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php index e568b6971..bd3bcb0e2 100644 --- a/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php +++ b/modules/thunder_gqls/src/Plugin/GraphQL/DataProducer/ThunderSearchApiProducerBase.php @@ -161,6 +161,13 @@ protected function buildBaseQuery( $query->range($offset, $limit); $cacheContext->addCacheableDependency($searchIndex); + foreach ($searchIndex->getDatasources() as $datasource) { + $storage = $this->entityTypeManager->getStorage($datasource->getEntityTypeId()); + $entityType = $storage->getEntityType(); + + $cacheContext->addCacheTags($entityType->getListCacheTags()); + $cacheContext->addCacheContexts($entityType->getListCacheContexts()); + } return $query; } From b7f02525a2fdd20434bb5f158404f4fdc6c736a8 Mon Sep 17 00:00:00 2001 From: Killesreiter Volker Date: Wed, 11 Dec 2024 16:20:49 +0100 Subject: [PATCH 2/3] Add test --- .../DataProducer/ThunderSearchApiTest.php | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 modules/thunder_gqls/tests/src/Kernel/DataProducer/ThunderSearchApiTest.php diff --git a/modules/thunder_gqls/tests/src/Kernel/DataProducer/ThunderSearchApiTest.php b/modules/thunder_gqls/tests/src/Kernel/DataProducer/ThunderSearchApiTest.php new file mode 100644 index 000000000..cd85c2249 --- /dev/null +++ b/modules/thunder_gqls/tests/src/Kernel/DataProducer/ThunderSearchApiTest.php @@ -0,0 +1,116 @@ +installSchema('search_api', [ + 'search_api_item', + ]); + $this->installEntitySchema('search_api_task'); + $this->installConfig('search_api'); + + // Create a test server. + $this->server = Server::create([ + 'name' => 'Test Server', + 'id' => 'test_server', + 'status' => 1, + 'backend' => 'search_api_test', + ]); + $this->server->save(); + + $this->index = Index::create([ + 'name' => 'Test Index', + 'id' => 'test_index', + 'status' => 1, + 'tracker_settings' => [ + 'default' => [], + ], + 'datasource_settings' => [ + 'entity:node' => [], + ], + 'server' => $this->server->id(), + 'options' => ['index_directly' => FALSE], + ]); + $this->index->save(); + + $schema = <<setUpSchema($schema); + } + + /** + * Test cache metadata for the query. + */ + public function testQueryCacheMetadata(): void { + $query = <<mockResolver('Query', 'search', + $this->builder->produce('thunder_search_api') + ->map('index', $this->builder->fromValue('test_index')) + ->map('offset', $this->builder->fromValue(0)) + ->map('limit', $this->builder->fromValue(20)) + ); + $this->mockResolver('SearchApiResult', 'total', $this->builder->fromValue(1)); + + $metadata = $this->defaultCacheMetaData(); + $metadata->setCacheContexts(['languages:language_interface', 'user.permissions', 'user.node_grants:view']); + $metadata->addCacheTags(['config:search_api.index.test_index', 'node_list']); + + $this->assertResults($query, [], [ + 'search' => ['total' => '1'], + ], $metadata); + } + +} From 6721271ca6ccebf5ec5828ab1fcbc05627943b25 Mon Sep 17 00:00:00 2001 From: Killesreiter Volker Date: Thu, 12 Dec 2024 12:04:34 +0100 Subject: [PATCH 3/3] fix overlapping props --- .../Kernel/DataProducer/ThunderSearchApiTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/thunder_gqls/tests/src/Kernel/DataProducer/ThunderSearchApiTest.php b/modules/thunder_gqls/tests/src/Kernel/DataProducer/ThunderSearchApiTest.php index cd85c2249..c12a7236e 100644 --- a/modules/thunder_gqls/tests/src/Kernel/DataProducer/ThunderSearchApiTest.php +++ b/modules/thunder_gqls/tests/src/Kernel/DataProducer/ThunderSearchApiTest.php @@ -27,14 +27,14 @@ class ThunderSearchApiTest extends GraphQLTestBase { * * @var \Drupal\search_api\ServerInterface */ - protected $server; + protected $searchApiServer; /** * The search index used for testing. * * @var \Drupal\search_api\IndexInterface */ - protected $index; + protected $searchApiIndex; /** * {@inheritdoc} @@ -49,15 +49,15 @@ public function setUp(): void { $this->installConfig('search_api'); // Create a test server. - $this->server = Server::create([ + $this->searchApiServer = Server::create([ 'name' => 'Test Server', 'id' => 'test_server', 'status' => 1, 'backend' => 'search_api_test', ]); - $this->server->save(); + $this->searchApiServer->save(); - $this->index = Index::create([ + $this->searchApiIndex = Index::create([ 'name' => 'Test Index', 'id' => 'test_index', 'status' => 1, @@ -67,10 +67,10 @@ public function setUp(): void { 'datasource_settings' => [ 'entity:node' => [], ], - 'server' => $this->server->id(), + 'server' => $this->searchApiServer->id(), 'options' => ['index_directly' => FALSE], ]); - $this->index->save(); + $this->searchApiIndex->save(); $schema = <<