Skip to content

Commit ae30914

Browse files
committed
GQL: Support monolingualtext value type
Bug: T404838 Change-Id: I8dc172459524b4f3e49dcee4d8ae0274fcedf386
1 parent 1b1cea8 commit ae30914

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

repo/WikibaseRepo.datatypes.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,26 @@
262262
) {
263263
return new MonolingualTextRdfBuilder();
264264
},
265+
'graphql-value-type' => static function () {
266+
return new ObjectType( [
267+
'name' => 'MonolingualTextValue',
268+
'fields' => [
269+
'language' => Type::nonNull( Type::string() ),
270+
'text' => Type::nonNull( Type::string() ),
271+
],
272+
'resolveField' => function ( Statement|PropertyValuePair $valueProvider, $args, $context, ResolveInfo $info ) {
273+
/** @var MonolingualTextValue $value */
274+
$value = $valueProvider->value->content;
275+
'@phan-var MonolingualTextValue $value';
276+
277+
return match ( $info->fieldName ) {
278+
'language' => $value->getLanguageCode(),
279+
'text' => $value->getText(),
280+
default => null,
281+
};
282+
},
283+
] );
284+
},
265285
],
266286
'VT:quantity' => [
267287
'expert-module' => 'jquery.valueview.experts.QuantityInput',

repo/domains/reuse/src/Infrastructure/GraphQL/schema.graphql

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ type PredicateProperty {
5050
label(languageCode: LanguageCode!): String
5151
}
5252

53-
union Value = StringValue | ItemValue | GlobeCoordinateValue
53+
union Value = StringValue | ItemValue | GlobeCoordinateValue | MonolingualTextValue
5454

5555
type StringValue {
5656
content: String!
@@ -68,6 +68,11 @@ type GlobeCoordinateValue {
6868
globe: String!
6969
}
7070

71+
type MonolingualTextValue {
72+
language: String!
73+
text: String!
74+
}
75+
7176
enum ValueType {
7277
novalue
7378
somevalue

repo/domains/reuse/tests/phpunit/Infrastructure/GraphQL/GraphQLServiceTest.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DataValues\Geo\Values\GlobeCoordinateValue;
66
use DataValues\Geo\Values\LatLongValue;
7+
use DataValues\MonolingualTextValue;
78
use DataValues\StringValue;
89
use Generator;
910
use GraphQL\GraphQL;
@@ -48,6 +49,7 @@ class GraphQLServiceTest extends MediaWikiIntegrationTestCase {
4849
private static Property $stringTypeProperty;
4950
private static Property $itemTypeProperty;
5051
private static Property $globeCoordinateTypeProperty;
52+
private static Property $monolingualTextProperty;
5153
private static Property $qualifierProperty;
5254
private static MediaWikiSite $sitelinkSite;
5355
private const ALLOWED_SITELINK_SITES = [ 'examplewiki', 'otherwiki' ];
@@ -78,10 +80,15 @@ public function testQuery( string $query, array $expectedResult ): void {
7880
] );
7981

8082
$dataTypeLookup = new InMemoryDataTypeLookup();
81-
$dataTypeLookup->setDataTypeForProperty( self::$stringTypeProperty->getId(), 'string' );
82-
$dataTypeLookup->setDataTypeForProperty( self::$qualifierProperty->getId(), 'string' );
83-
$dataTypeLookup->setDataTypeForProperty( self::$itemTypeProperty->getId(), 'wikibase-item' );
84-
$dataTypeLookup->setDataTypeForProperty( self::$globeCoordinateTypeProperty->getId(), 'globe-coordinate' );
83+
foreach ( [
84+
self::$qualifierProperty,
85+
self::$stringTypeProperty,
86+
self::$itemTypeProperty,
87+
self::$globeCoordinateTypeProperty,
88+
self::$monolingualTextProperty,
89+
] as $property ) {
90+
$dataTypeLookup->setDataTypeForProperty( $property->getId(), $property->getDataTypeId() );
91+
}
8592

8693
$this->assertEquals(
8794
$expectedResult,
@@ -122,6 +129,7 @@ public function queryProvider(): Generator {
122129
$statementWithNoReferencesPropertyId = $statementWithNoValuePropertyId;
123130
$statementWithSomeValuePropertyId = 'P5';
124131
$statementWithGlobeCoordinateValuePropertyId = 'P6';
132+
$statementWithMonolingualTextValuePropertyId = 'P7';
125133
$statementWithItemValueQualifierPropertyId = $statementWithItemValuePropertyId; // also type wikibase-item so we can just reuse it.
126134
$statementReferencePropertyId = 'P11';
127135
$unusedPropertyId = 'P9999';
@@ -145,6 +153,11 @@ public function queryProvider(): Generator {
145153
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
146154
->withValue( $globeCoordinateValue )
147155
->build();
156+
$monolingualTextValue = new MonolingualTextValue( 'en', 'potato' );
157+
$statementWithMonolingualTextValue = NewStatement::forProperty( $statementWithMonolingualTextValuePropertyId )
158+
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
159+
->withValue( $monolingualTextValue )
160+
->build();
148161

149162
$statementWithNoValue = NewStatement::noValueFor( ( $statementWithNoValuePropertyId ) )
150163
->withGuid( "$itemId\$bed933b7-4207-d679-7571-3630cfb49d9f" )
@@ -169,6 +182,11 @@ public function queryProvider(): Generator {
169182
null,
170183
'globe-coordinate',
171184
);
185+
self::$monolingualTextProperty = new Property(
186+
new NumericPropertyId( $statementWithMonolingualTextValuePropertyId ),
187+
null,
188+
'monolingualtext',
189+
);
172190
self::$qualifierProperty = new Property(
173191
new NumericPropertyId( $qualifierPropertyId ),
174192
new Fingerprint( new TermList( [ new Term( 'en', 'qualifier prop' ) ] ) ),
@@ -183,6 +201,7 @@ public function queryProvider(): Generator {
183201
->andStatement( $statementWithStringValue )
184202
->andStatement( $statementWithItemValue )
185203
->andStatement( $statementWithGlobeCoordinateValue )
204+
->andStatement( $statementWithMonolingualTextValue )
186205
->andStatement( $statementWithNoValue )
187206
->andStatement( $statementWithSomeValue )
188207
->build();
@@ -394,6 +413,29 @@ public function queryProvider(): Generator {
394413
],
395414
],
396415
];
416+
yield 'statement with monolingualtext value' => [
417+
"{ item(id: \"$itemId\") {
418+
statements(propertyId: \"$statementWithMonolingualTextValuePropertyId\") {
419+
value {
420+
... on MonolingualTextValue { language text }
421+
}
422+
}
423+
} }",
424+
[
425+
'data' => [
426+
'item' => [
427+
'statements' => [
428+
[
429+
'value' => [
430+
'language' => $monolingualTextValue->getLanguageCode(),
431+
'text' => $monolingualTextValue->getText(),
432+
],
433+
],
434+
],
435+
],
436+
],
437+
],
438+
];
397439
yield 'statements with novalue and somevalue' => [
398440
"{ item(id: \"$itemId\") {
399441
$statementWithSomeValuePropertyId: statements(propertyId: \"$statementWithSomeValuePropertyId\") {

0 commit comments

Comments
 (0)