Skip to content

Commit 024f6b5

Browse files
jakobwjenkins-bot
authored andcommitted
GQL: Enable fetching statement string values
Bug: T400148 Change-Id: Ib6964ed9d7efce98a30195a5a8ab20247724470e
1 parent 3fd1bed commit 024f6b5

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

repo/includes/GraphQLPrototype/Schema.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ private function statementType(): ObjectType {
9090
],
9191
],
9292
] ),
93+
'value' => new ObjectType( [
94+
'name' => 'StatementValue',
95+
'fields' => [
96+
'content' => Type::nonNull( Type::string() ),
97+
],
98+
] ),
9399
],
94100
] );
95101
}

repo/includes/GraphQLPrototype/StatementsResolver.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Wikibase\Repo\GraphQLPrototype;
44

5+
use DataValues\StringValue;
56
use Wikibase\DataModel\Entity\Item;
67
use Wikibase\DataModel\Entity\ItemId;
78
use Wikibase\DataModel\Services\Lookup\EntityLookup;
9+
use Wikibase\DataModel\Snak\PropertyValueSnak;
810
use Wikibase\DataModel\Statement\Statement;
911

1012
/**
@@ -23,8 +25,15 @@ public function fetchStatements( array $rootValue ): array {
2325
return array_map(
2426
fn( Statement $statement ) => [
2527
'property' => [ 'id' => $statement->getPropertyId()->getSerialization() ],
28+
// @phan-suppress-next-line PhanUndeclaredMethod guaranteed to be a string value per array_filter
29+
'value' => [ 'content' => $statement->getMainSnak()->getDataValue()->getValue() ],
2630
],
27-
iterator_to_array( $item->getStatements() )
31+
array_filter(
32+
iterator_to_array( $item->getStatements() ),
33+
fn( Statement $statement ) => $statement->getMainSnak() instanceof PropertyValueSnak
34+
// @phan-suppress-next-line PhanUndeclaredMethod guaranteed to be a value snak per line above
35+
&& $statement->getMainSnak()->getDataValue() instanceof StringValue
36+
)
2837
);
2938
}
3039
}

repo/tests/phpunit/includes/GraphQLPrototype/GraphQLQueryServiceTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,22 @@ public function addDBDataOnce() {
4949
$this->saveEntity( $stringProperty );
5050
self::$stringProperty = $stringProperty;
5151

52+
$timeProperty = new Property(
53+
null,
54+
new Fingerprint( new TermList( [ new Term( 'en', 'date of birth' ) ] ) ),
55+
'time',
56+
);
57+
$this->saveEntity( $timeProperty );
58+
5259
$item = NewItem::withLabel( 'en', 'potato' )
5360
->andLabel( 'de', 'Kartoffel' )
5461
->andStatement(
55-
NewStatement::noValueFor( self::$stringProperty->getId() )->build()
62+
NewStatement::forProperty( $stringProperty->getId() )
63+
->withValue( 'potato value' )
64+
->build()
65+
)
66+
->andStatement( // this statement will get filtered out, because we don't support time values yet
67+
NewStatement::noValueFor( $timeProperty->getId() )->build()
5668
)
5769
->build();
5870
$this->saveEntity( $item );
@@ -139,6 +151,36 @@ public function testItemNotFound(): void {
139151
);
140152
}
141153

154+
public function testStatementsQueryWithStringValue(): void {
155+
$itemId = self::$item->getId()->getSerialization();
156+
$value = self::$item->getStatements()
157+
->getByPropertyId( self::$stringProperty->getId() )->toArray()[0]
158+
->getMainSnak()->getDataValue()->getValue();
159+
160+
$this->assertNotNull( $value );
161+
$this->assertEquals(
162+
[ 'data' => [ 'item' => [
163+
'statements' => [
164+
[
165+
'property' => [
166+
'id' => self::$stringProperty->getId()->getSerialization(),
167+
],
168+
'value' => [ 'content' => $value ],
169+
],
170+
],
171+
] ] ],
172+
$this->newGraphQLService()->query( "
173+
query {
174+
item(id: \"$itemId\") {
175+
statements {
176+
property { id }
177+
value { content }
178+
}
179+
}
180+
}" )
181+
);
182+
}
183+
142184
public function newGraphQLService(): GraphQLQueryService {
143185
$entityLookup = WikibaseRepo::getEntityLookup();
144186

0 commit comments

Comments
 (0)