|
2 | 2 |
|
3 | 3 | namespace Wikibase\Repo\GraphQLPrototype;
|
4 | 4 |
|
| 5 | +use GraphQL\Deferred; |
5 | 6 | use GraphQL\Type\Definition\ResolveInfo;
|
6 | 7 | use Wikibase\DataAccess\PrefetchingTermLookup;
|
7 | 8 | use Wikibase\DataModel\Entity\BasicEntityIdParser;
|
|
12 | 13 | */
|
13 | 14 | class LabelsResolver {
|
14 | 15 |
|
| 16 | + private array $entityIdsBatch = []; |
| 17 | + private array $languageCodesBatch = []; |
| 18 | + private bool $hasPrefetched = false; |
| 19 | + |
15 | 20 | public function __construct( private PrefetchingTermLookup $termLookup ) {
|
16 | 21 | }
|
17 | 22 |
|
18 |
| - public function fetchLabels( array $rootValue, ResolveInfo $info ): array { |
| 23 | + public function fetchLabels( array $rootValue, ResolveInfo $info ): Deferred { |
19 | 24 | $entityId = ( new BasicEntityIdParser() )->parse( $rootValue['id'] );
|
20 | 25 | $languageCodes = array_keys( $info->getFieldSelection() );
|
21 | 26 |
|
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 | + } |
27 | 39 |
|
28 |
| - return $this->termLookup->getLabels( $entityId, $languageCodes ); |
| 40 | + return $this->termLookup->getLabels( $entityId, $languageCodes ); |
| 41 | + } ); |
29 | 42 | }
|
30 | 43 | }
|
0 commit comments