Skip to content

Commit 7116495

Browse files
committed
GQL: Support quantity value type
Bug: T404838 Change-Id: I72767d5ca31c03c2da788190621100de06131162
1 parent ae30914 commit 7116495

File tree

3 files changed

+87
-1
lines changed

3 files changed

+87
-1
lines changed

repo/WikibaseRepo.datatypes.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,21 @@
316316
'search-index-data-formatter-callback' => function ( UnboundedQuantityValue $value ) {
317317
return (string)round( $value->getAmount()->getValueFloat() );
318318
},
319+
'graphql-value-type' => static function () {
320+
return new ObjectType( [
321+
'name' => 'QuantityValue',
322+
'fields' => [
323+
'amount' => Type::nonNull( Type::string() ),
324+
'unit' => Type::nonNull( Type::string() ),
325+
'lowerBound' => Type::string(),
326+
'upperBound' => Type::string(),
327+
],
328+
'resolveField' => function ( Statement|PropertyValuePair $valueProvider, $args, $context, ResolveInfo $info ) {
329+
return $valueProvider->value->content
330+
->getArrayValue()[$info->fieldName] ?? null;
331+
},
332+
] );
333+
},
319334
],
320335
'VT:string' => [
321336
'expert-module' => 'jquery.valueview.experts.StringValue',

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

Lines changed: 8 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 | MonolingualTextValue
53+
union Value = StringValue | ItemValue | GlobeCoordinateValue | MonolingualTextValue | QuantityValue
5454

5555
type StringValue {
5656
content: String!
@@ -73,6 +73,13 @@ type MonolingualTextValue {
7373
text: String!
7474
}
7575

76+
type QuantityValue {
77+
amount: String!
78+
unit: String!
79+
lowerBound: String
80+
upperBound: String
81+
}
82+
7683
enum ValueType {
7784
novalue
7885
somevalue

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Wikibase\Repo\Tests\Domains\Reuse\Infrastructure\GraphQL;
44

5+
use DataValues\DecimalValue;
56
use DataValues\Geo\Values\GlobeCoordinateValue;
67
use DataValues\Geo\Values\LatLongValue;
78
use DataValues\MonolingualTextValue;
9+
use DataValues\QuantityValue;
810
use DataValues\StringValue;
11+
use DataValues\UnboundedQuantityValue;
912
use Generator;
1013
use GraphQL\GraphQL;
1114
use MediaWiki\Site\HashSiteStore;
@@ -50,6 +53,7 @@ class GraphQLServiceTest extends MediaWikiIntegrationTestCase {
5053
private static Property $itemTypeProperty;
5154
private static Property $globeCoordinateTypeProperty;
5255
private static Property $monolingualTextProperty;
56+
private static Property $quantityProperty;
5357
private static Property $qualifierProperty;
5458
private static MediaWikiSite $sitelinkSite;
5559
private const ALLOWED_SITELINK_SITES = [ 'examplewiki', 'otherwiki' ];
@@ -86,6 +90,7 @@ public function testQuery( string $query, array $expectedResult ): void {
8690
self::$itemTypeProperty,
8791
self::$globeCoordinateTypeProperty,
8892
self::$monolingualTextProperty,
93+
self::$quantityProperty,
8994
] as $property ) {
9095
$dataTypeLookup->setDataTypeForProperty( $property->getId(), $property->getDataTypeId() );
9196
}
@@ -130,6 +135,7 @@ public function queryProvider(): Generator {
130135
$statementWithSomeValuePropertyId = 'P5';
131136
$statementWithGlobeCoordinateValuePropertyId = 'P6';
132137
$statementWithMonolingualTextValuePropertyId = 'P7';
138+
$statementWithQuantityValuePropertyId = 'P8';
133139
$statementWithItemValueQualifierPropertyId = $statementWithItemValuePropertyId; // also type wikibase-item so we can just reuse it.
134140
$statementReferencePropertyId = 'P11';
135141
$unusedPropertyId = 'P9999';
@@ -158,6 +164,24 @@ public function queryProvider(): Generator {
158164
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
159165
->withValue( $monolingualTextValue )
160166
->build();
167+
$quantityValue = new QuantityValue(
168+
new DecimalValue( '+0.111' ),
169+
'https://wikibase.example/wiki/Q123',
170+
new DecimalValue( '+0.1150' ),
171+
new DecimalValue( '+0.1105' ),
172+
);
173+
$unboundedQuantityValue = new UnboundedQuantityValue(
174+
new DecimalValue( '+321' ),
175+
'https://wikibase.example/wiki/Q321',
176+
);
177+
$statementWithQuantityValue = NewStatement::forProperty( $statementWithQuantityValuePropertyId )
178+
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
179+
->withValue( $quantityValue )
180+
->build();
181+
$statementWithUnboundedQuantityValue = NewStatement::forProperty( $statementWithQuantityValuePropertyId )
182+
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
183+
->withValue( $unboundedQuantityValue )
184+
->build();
161185

162186
$statementWithNoValue = NewStatement::noValueFor( ( $statementWithNoValuePropertyId ) )
163187
->withGuid( "$itemId\$bed933b7-4207-d679-7571-3630cfb49d9f" )
@@ -187,6 +211,11 @@ public function queryProvider(): Generator {
187211
null,
188212
'monolingualtext',
189213
);
214+
self::$quantityProperty = new Property(
215+
new NumericPropertyId( $statementWithQuantityValuePropertyId ),
216+
null,
217+
'quantity',
218+
);
190219
self::$qualifierProperty = new Property(
191220
new NumericPropertyId( $qualifierPropertyId ),
192221
new Fingerprint( new TermList( [ new Term( 'en', 'qualifier prop' ) ] ) ),
@@ -202,6 +231,8 @@ public function queryProvider(): Generator {
202231
->andStatement( $statementWithItemValue )
203232
->andStatement( $statementWithGlobeCoordinateValue )
204233
->andStatement( $statementWithMonolingualTextValue )
234+
->andStatement( $statementWithQuantityValue )
235+
->andStatement( $statementWithUnboundedQuantityValue )
205236
->andStatement( $statementWithNoValue )
206237
->andStatement( $statementWithSomeValue )
207238
->build();
@@ -436,6 +467,39 @@ public function queryProvider(): Generator {
436467
],
437468
],
438469
];
470+
yield 'statement with quantity value' => [
471+
"{ item(id: \"$itemId\") {
472+
statements(propertyId: \"$statementWithQuantityValuePropertyId\") {
473+
value {
474+
... on QuantityValue { amount unit lowerBound upperBound }
475+
}
476+
}
477+
} }",
478+
[
479+
'data' => [
480+
'item' => [
481+
'statements' => [
482+
[
483+
'value' => [
484+
'amount' => $quantityValue->getAmount()->getValue(),
485+
'unit' => $quantityValue->getUnit(),
486+
'lowerBound' => $quantityValue->getLowerBound()->getValue(),
487+
'upperBound' => $quantityValue->getUpperBound()->getValue(),
488+
],
489+
],
490+
[
491+
'value' => [
492+
'amount' => $unboundedQuantityValue->getAmount()->getValue(),
493+
'unit' => $unboundedQuantityValue->getUnit(),
494+
'lowerBound' => null,
495+
'upperBound' => null,
496+
],
497+
],
498+
],
499+
],
500+
],
501+
],
502+
];
439503
yield 'statements with novalue and somevalue' => [
440504
"{ item(id: \"$itemId\") {
441505
$statementWithSomeValuePropertyId: statements(propertyId: \"$statementWithSomeValuePropertyId\") {

0 commit comments

Comments
 (0)