Skip to content

Commit ff7eac7

Browse files
jakobwjenkins-bot
authored andcommitted
GQL: Request labels in batch
This reduces the number of DB queries to fetch labels. It combines the labels to fetch of the main item with the ones to fetch of statement properties. In theory, this could result in over-fetching if item labels are requested in different languages than the statement property labels, but in reality this is unlikely and probably not worth optimizing. Bug: T400146 Change-Id: Ie92b5bfaad562649c284b6de41d4d533c4903f6a
1 parent bf09532 commit ff7eac7

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

repo/includes/GraphQLPrototype/LabelsResolver.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Wikibase\Repo\GraphQLPrototype;
44

5+
use GraphQL\Deferred;
56
use GraphQL\Type\Definition\ResolveInfo;
67
use Wikibase\DataAccess\PrefetchingTermLookup;
78
use Wikibase\DataModel\Entity\BasicEntityIdParser;
@@ -12,19 +13,31 @@
1213
*/
1314
class LabelsResolver {
1415

16+
private array $entityIdsBatch = [];
17+
private array $languageCodesBatch = [];
18+
private bool $hasPrefetched = false;
19+
1520
public function __construct( private PrefetchingTermLookup $termLookup ) {
1621
}
1722

18-
public function fetchLabels( array $rootValue, ResolveInfo $info ): array {
23+
public function fetchLabels( array $rootValue, ResolveInfo $info ): Deferred {
1924
$entityId = ( new BasicEntityIdParser() )->parse( $rootValue['id'] );
2025
$languageCodes = array_keys( $info->getFieldSelection() );
2126

22-
$this->termLookup->prefetchTerms(
23-
[ $entityId ],
24-
[ TermTypes::TYPE_LABEL ],
25-
$languageCodes
26-
);
27+
$this->entityIdsBatch[] = $entityId;
28+
$this->languageCodesBatch = array_merge( $this->languageCodesBatch, $languageCodes );
29+
30+
return new Deferred( function() use( $entityId, $languageCodes ) {
31+
if ( !$this->hasPrefetched ) {
32+
$this->termLookup->prefetchTerms(
33+
array_unique( $this->entityIdsBatch ),
34+
[ TermTypes::TYPE_LABEL ],
35+
array_unique( $this->languageCodesBatch )
36+
);
37+
$this->hasPrefetched = true;
38+
}
2739

28-
return $this->termLookup->getLabels( $entityId, $languageCodes );
40+
return $this->termLookup->getLabels( $entityId, $languageCodes );
41+
} );
2942
}
3043
}

0 commit comments

Comments
 (0)