1818use MediaWikiIntegrationTestCase ;
1919use Wikibase \DataAccess \PrefetchingTermLookup ;
2020use Wikibase \DataAccess \Tests \InMemoryPrefetchingTermLookup ;
21+ use Wikibase \DataModel \Entity \EntityId ;
2122use Wikibase \DataModel \Entity \EntityIdValue ;
2223use Wikibase \DataModel \Entity \Item ;
2324use Wikibase \DataModel \Entity \ItemId ;
@@ -60,8 +61,10 @@ class GraphQLServiceTest extends MediaWikiIntegrationTestCase {
6061 private static Property $ propertyTypeProperty ;
6162 private static Property $ propertyUsedAsValue ;
6263 private static Property $ qualifierProperty ;
64+ private static Property $ customEntityIdProperty ;
6365 private static MediaWikiSite $ sitelinkSite ;
6466 private const ALLOWED_SITELINK_SITES = [ 'examplewiki ' , 'otherwiki ' ];
67+ private const CUSTOM_ENTITY_DATA_TYPE = 'test-type ' ;
6568
6669 public static function setUpBeforeClass (): void {
6770 if ( !class_exists ( GraphQL::class ) ) {
@@ -99,6 +102,7 @@ public function testQuery( string $query, array $expectedResult ): void {
99102 self ::$ quantityProperty ,
100103 self ::$ timeProperty ,
101104 self ::$ propertyTypeProperty ,
105+ self ::$ customEntityIdProperty ,
102106 ] as $ property ) {
103107 $ dataTypeLookup ->setDataTypeForProperty ( $ property ->getId (), $ property ->getDataTypeId () );
104108 }
@@ -146,6 +150,7 @@ public function queryProvider(): Generator {
146150 $ statementWithQuantityValuePropertyId = 'P8 ' ;
147151 $ statementWithTimeValuePropertyId = 'P9 ' ;
148152 $ statementWithPropertyValuePropertyId = 'P10 ' ;
153+ $ statementWithCustomEntityIdValuePropertyId = 'P13 ' ;
149154 $ statementWithItemValueQualifierPropertyId = $ statementWithItemValuePropertyId ; // also type wikibase-item so we can just reuse it.
150155 $ statementReferencePropertyId = 'P11 ' ;
151156 $ unusedPropertyId = 'P9999 ' ;
@@ -231,6 +236,16 @@ public function queryProvider(): Generator {
231236 ->withSomeGuid ()
232237 ->build ();
233238
239+ $ customEntityId = $ this ->createMock ( EntityId::class );
240+ $ customEntityId ->method ( 'getSerialization ' )
241+ ->willReturn ( 'T3 ' );
242+ $ entityIdValue = new EntityIdValue ( $ customEntityId );
243+ $ statementWithCustomEntityIdValue = NewStatement::forProperty ( $ statementWithCustomEntityIdValuePropertyId )
244+ ->withSubject ( $ itemId )
245+ ->withSomeGuid ()
246+ ->withValue ( $ entityIdValue )
247+ ->build ();
248+
234249 self ::$ sitelinkSite = new MediaWikiSite ();
235250 self ::$ sitelinkSite ->setLinkPath ( 'https://wiki.example/wiki/$1 ' );
236251 $ expectedSitelinkUrl = "https://wiki.example/wiki/ $ sitelinkTitle " ;
@@ -272,6 +287,11 @@ public function queryProvider(): Generator {
272287 new Fingerprint ( new TermList ( [ new Term ( 'en ' , 'qualifier prop ' ) ] ) ),
273288 'string ' ,
274289 );
290+ self ::$ customEntityIdProperty = new Property (
291+ new NumericPropertyId ( $ statementWithCustomEntityIdValuePropertyId ),
292+ null ,
293+ self ::CUSTOM_ENTITY_DATA_TYPE
294+ );
275295
276296 self ::$ item1 = NewItem::withId ( $ itemId )
277297 ->andLabel ( 'en ' , $ enLabel )
@@ -288,6 +308,7 @@ public function queryProvider(): Generator {
288308 ->andStatement ( $ statementWithPropertyValue )
289309 ->andStatement ( $ statementWithNoValue )
290310 ->andStatement ( $ statementWithSomeValue )
311+ ->andStatement ( $ statementWithCustomEntityIdValue )
291312 ->build ();
292313
293314 $ item2Id = 'Q321 ' ;
@@ -704,6 +725,22 @@ public function queryProvider(): Generator {
704725 ],
705726 ],
706727 ];
728+ yield 'entity id value for which there is no data type specific GraphQL type ' => [
729+ "{ item(id: \"$ itemId \") {
730+ statements(propertyId: \"{$ statementWithCustomEntityIdValuePropertyId }\") {
731+ value {... on EntityValue { id } }
732+ }
733+ } } " ,
734+ [
735+ 'data ' => [
736+ 'item ' => [
737+ 'statements ' => [
738+ [ 'value ' => [ 'id ' => $ entityIdValue ->getEntityId ()->getSerialization () ] ],
739+ ],
740+ ],
741+ ],
742+ ],
743+ ];
707744 yield 'multiple items at once ' => [
708745 "{
709746 item1: item(id: \"$ itemId \") { label(languageCode: \"en \") }
@@ -804,6 +841,13 @@ private function newGraphQLService(
804841 $ termLookup ?? $ this ->createStub ( PrefetchingTermLookup::class ),
805842 );
806843
844+ $ this ->setTemporaryHook ( 'WikibaseRepoDataTypes ' , function ( array &$ dataTypes ): void {
845+ $ dataTypes ['PT: ' . self ::CUSTOM_ENTITY_DATA_TYPE ] = [
846+ 'value-type ' => 'wikibase-entityid ' ,
847+ ];
848+ } );
849+ $ this ->resetServices ();
850+
807851 return WbReuse::getGraphQLService ();
808852 }
809853}
0 commit comments