Skip to content

Commit 6692db2

Browse files
committed
GQL: Support time value type
Bug: T404838 Change-Id: I4cb695a252011c78839b48727bef106648d39a9d
1 parent 3c5fd10 commit 6692db2

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

repo/WikibaseRepo.datatypes.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,25 @@
398398
new ComplexValueRdfHelper( $vocab, $writer->sub(), $dedupe ) : null;
399399
return new TimeRdfBuilder( $dateCleaner, $complexValueHelper );
400400
},
401+
'graphql-value-type' => static function () {
402+
return new ObjectType( [
403+
'name' => 'TimeValue',
404+
'fields' => [
405+
'time' => Type::nonNull( Type::string() ),
406+
'timezone' => Type::nonNull( Type::int() ),
407+
'before' => Type::nonNull( Type::int() ),
408+
'after' => Type::nonNull( Type::int() ),
409+
'precision' => Type::nonNull( Type::int() ),
410+
'calendarModel' => Type::nonNull( Type::string() ),
411+
],
412+
'resolveField' => function ( Statement|PropertyValuePair $valueProvider, $args, $context, ResolveInfo $info ) {
413+
$value = $valueProvider->value->content->getArrayValue();
414+
$value['calendarModel'] = $value['calendarmodel'] ?? null; // prefer camel case over all lowercase
415+
416+
return $value[$info->fieldName] ?? null;
417+
},
418+
] );
419+
},
401420
],
402421
'PT:url' => [
403422
'validator-factory-callback' => function() {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ type PredicateProperty {
5252
label(languageCode: LanguageCode!): String
5353
}
5454

55-
union Value = StringValue | ItemValue | GlobeCoordinateValue | MonolingualTextValue | QuantityValue
55+
union Value = StringValue | ItemValue | GlobeCoordinateValue | MonolingualTextValue | QuantityValue | TimeValue
5656

5757
type StringValue {
5858
content: String!
@@ -82,6 +82,15 @@ type QuantityValue {
8282
upperBound: String
8383
}
8484

85+
type TimeValue {
86+
time: String!
87+
timezone: Int!
88+
before: Int!
89+
after: Int!
90+
precision: Int!
91+
calendarModel: String!
92+
}
93+
8594
enum ValueType {
8695
novalue
8796
somevalue

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use DataValues\MonolingualTextValue;
99
use DataValues\QuantityValue;
1010
use DataValues\StringValue;
11+
use DataValues\TimeValue;
1112
use DataValues\UnboundedQuantityValue;
1213
use Generator;
1314
use GraphQL\GraphQL;
@@ -54,6 +55,7 @@ class GraphQLServiceTest extends MediaWikiIntegrationTestCase {
5455
private static Property $globeCoordinateTypeProperty;
5556
private static Property $monolingualTextProperty;
5657
private static Property $quantityProperty;
58+
private static Property $timeProperty;
5759
private static Property $qualifierProperty;
5860
private static MediaWikiSite $sitelinkSite;
5961
private const ALLOWED_SITELINK_SITES = [ 'examplewiki', 'otherwiki' ];
@@ -91,6 +93,7 @@ public function testQuery( string $query, array $expectedResult ): void {
9193
self::$globeCoordinateTypeProperty,
9294
self::$monolingualTextProperty,
9395
self::$quantityProperty,
96+
self::$timeProperty,
9497
] as $property ) {
9598
$dataTypeLookup->setDataTypeForProperty( $property->getId(), $property->getDataTypeId() );
9699
}
@@ -136,6 +139,7 @@ public function queryProvider(): Generator {
136139
$statementWithGlobeCoordinateValuePropertyId = 'P6';
137140
$statementWithMonolingualTextValuePropertyId = 'P7';
138141
$statementWithQuantityValuePropertyId = 'P8';
142+
$statementWithTimeValuePropertyId = 'P9';
139143
$statementWithItemValueQualifierPropertyId = $statementWithItemValuePropertyId; // also type wikibase-item so we can just reuse it.
140144
$statementReferencePropertyId = 'P11';
141145
$unusedPropertyId = 'P9999';
@@ -182,6 +186,18 @@ public function queryProvider(): Generator {
182186
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
183187
->withValue( $unboundedQuantityValue )
184188
->build();
189+
$timeValue = new TimeValue(
190+
timestamp: '+2001-01-01T00:00:00Z',
191+
timezone: 60,
192+
before: 0,
193+
after: 1,
194+
precision: TimeValue::PRECISION_MONTH,
195+
calendarModel: 'http://www.wikidata.org/entity/Q1985727',
196+
);
197+
$statementWithTimeValue = NewStatement::forProperty( $statementWithTimeValuePropertyId )
198+
->withGuid( "$itemId\$a82559b1-da8f-4e02-9f72-e304b90a9bde" )
199+
->withValue( $timeValue )
200+
->build();
185201

186202
$statementWithNoValue = NewStatement::noValueFor( ( $statementWithNoValuePropertyId ) )
187203
->withGuid( "$itemId\$bed933b7-4207-d679-7571-3630cfb49d9f" )
@@ -216,6 +232,11 @@ public function queryProvider(): Generator {
216232
null,
217233
'quantity',
218234
);
235+
self::$timeProperty = new Property(
236+
new NumericPropertyId( $statementWithTimeValuePropertyId ),
237+
null,
238+
'time',
239+
);
219240
self::$qualifierProperty = new Property(
220241
new NumericPropertyId( $qualifierPropertyId ),
221242
new Fingerprint( new TermList( [ new Term( 'en', 'qualifier prop' ) ] ) ),
@@ -233,6 +254,7 @@ public function queryProvider(): Generator {
233254
->andStatement( $statementWithMonolingualTextValue )
234255
->andStatement( $statementWithQuantityValue )
235256
->andStatement( $statementWithUnboundedQuantityValue )
257+
->andStatement( $statementWithTimeValue )
236258
->andStatement( $statementWithNoValue )
237259
->andStatement( $statementWithSomeValue )
238260
->build();
@@ -500,6 +522,33 @@ public function queryProvider(): Generator {
500522
],
501523
],
502524
];
525+
yield 'statement with time value' => [
526+
"{ item(id: \"$itemId\") {
527+
statements(propertyId: \"$statementWithTimeValuePropertyId\") {
528+
value {
529+
... on TimeValue { time timezone before after precision calendarModel }
530+
}
531+
}
532+
} }",
533+
[
534+
'data' => [
535+
'item' => [
536+
'statements' => [
537+
[
538+
'value' => [
539+
'time' => $timeValue->getTime(),
540+
'timezone' => $timeValue->getTimezone(),
541+
'before' => $timeValue->getBefore(),
542+
'after' => $timeValue->getAfter(),
543+
'precision' => $timeValue->getPrecision(),
544+
'calendarModel' => $timeValue->getCalendarModel(),
545+
],
546+
],
547+
],
548+
],
549+
],
550+
],
551+
];
503552
yield 'statements with novalue and somevalue' => [
504553
"{ item(id: \"$itemId\") {
505554
$statementWithSomeValuePropertyId: statements(propertyId: \"$statementWithSomeValuePropertyId\") {

0 commit comments

Comments
 (0)