Skip to content

Commit dbdabf2

Browse files
author
dima koushha
committed
GQL: Enable fetch statements with valueType
Bug: T404834 Change-Id: I36d64315732925d7c83fe2301e2b96deda2a5819
1 parent b0c89f6 commit dbdabf2

File tree

14 files changed

+165
-19
lines changed

14 files changed

+165
-19
lines changed

repo/domains/reuse/src/Domain/Model/PropertyValuePair.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
*/
88
class PropertyValuePair {
99

10-
public function __construct( public readonly PredicateProperty $property, public readonly Value $value ) {
10+
public function __construct(
11+
public readonly PredicateProperty $property,
12+
public readonly ?Value $value,
13+
public readonly ValueType $valueType,
14+
) {
1115
}
1216

1317
}

repo/domains/reuse/src/Domain/Model/Rank.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
/**
99
* @license GPL-2.0-or-later
10-
*/class Rank {
10+
*/
11+
class Rank {
1112

1213
private const RANKS = [
1314
StatementWriteModel::RANK_DEPRECATED,

repo/domains/reuse/src/Domain/Model/Statement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ public function __construct(
1414
public readonly Rank $rank,
1515
public readonly Qualifiers $qualifiers,
1616
public readonly PredicateProperty $property,
17-
public readonly Value $value
17+
public readonly ?Value $value,
18+
public readonly ValueType $valueType
1819
) {
1920
}
2021

repo/domains/reuse/src/Domain/Model/Value.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
*/
1010
class Value {
1111

12-
/**
13-
* @param DataValue|null $content Guaranteed to be non-null if value type is "value", always null otherwise.
14-
*/
15-
public function __construct( public readonly ?DataValue $content = null ) {
12+
public function __construct( public readonly DataValue $content ) {
1613
}
1714

1815
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace Wikibase\Repo\Domains\Reuse\Domain\Model;
4+
5+
use InvalidArgumentException;
6+
7+
/**
8+
* @license GPL-2.0-or-later
9+
*/
10+
enum ValueType: string {
11+
case TYPE_VALUE = 'value';
12+
case TYPE_NO_VALUE = 'novalue';
13+
case TYPE_SOME_VALUE = 'somevalue';
14+
15+
public static function fromString( string $type ): self {
16+
return match ( $type ) {
17+
self::TYPE_VALUE->value => self::TYPE_VALUE,
18+
self::TYPE_SOME_VALUE->value => self::TYPE_SOME_VALUE,
19+
self::TYPE_NO_VALUE->value => self::TYPE_NO_VALUE,
20+
default => throw new InvalidArgumentException(
21+
'valueType must be one of "value", "somevalue", "novalue"'
22+
),
23+
};
24+
}
25+
}

repo/domains/reuse/src/Domain/Services/StatementReadModelConverter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Wikibase\Repo\Domains\Reuse\Domain\Model\Rank;
1717
use Wikibase\Repo\Domains\Reuse\Domain\Model\Statement;
1818
use Wikibase\Repo\Domains\Reuse\Domain\Model\Value;
19+
use Wikibase\Repo\Domains\Reuse\Domain\Model\ValueType;
1920

2021
/**
2122
* @license GPL-2.0-or-later
@@ -42,6 +43,7 @@ public function convert( StatementWriteModel $inputStatement ): Statement {
4243
$this->convertQualifiers( $inputStatement->getQualifiers() ),
4344
$mainPropertyValuePair->property,
4445
$mainPropertyValuePair->value,
46+
ValueType::fromString( $inputStatement->getMainSnak()->getType() ),
4547
);
4648
}
4749

@@ -63,9 +65,8 @@ private function convertSnakToPropertyValuePair( Snak $snak ): PropertyValuePair
6365

6466
return new PropertyValuePair(
6567
new PredicateProperty( $snak->getPropertyId(), $dataType ),
66-
new Value(
67-
$snak instanceof PropertyValueSnak ? $snak->getDataValue() : null
68-
)
68+
$snak instanceof PropertyValueSnak ? new Value( $snak->getDataValue() ) : null,
69+
ValueType::fromString( $snak->getType() ),
6970
);
7071
}
7172

repo/domains/reuse/src/Infrastructure/GraphQL/Schema/PropertyValuePairType.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@
1111
*/
1212
class PropertyValuePairType extends ObjectType {
1313

14-
public function __construct( PredicatePropertyType $predicateType, ValueType $valueType ) {
14+
public function __construct(
15+
PredicatePropertyType $predicateType,
16+
ValueType $valueType,
17+
ValueTypeType $valueTypeType
18+
) {
1519
$config = [
1620
'fields' => [
1721
'property' => [
1822
'type' => Type::nonNull( $predicateType ),
1923
'resolve' => fn( PropertyValuePair $rootValue ) => $rootValue->property,
2024
],
2125
'value' => [
22-
'type' => Type::nonNull( $valueType ),
26+
'type' => $valueType,
2327
'resolve' => fn( PropertyValuePair $rootValue ) => $rootValue->value,
2428
],
29+
'valueType' => [
30+
'type' => Type::nonNull( $valueTypeType ),
31+
'resolve' => fn( PropertyValuePair $rootValue ) => $rootValue->valueType,
32+
],
2533
],
2634
];
2735
parent::__construct( $config );

repo/domains/reuse/src/Infrastructure/GraphQL/Schema/Schema.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function __construct(
2424
private readonly PredicatePropertyType $predicatePropertyType,
2525
private readonly PropertyValuePairType $propertyValuePairType,
2626
private readonly ValueType $valueType,
27+
private readonly ValueTypeType $valueTypeType,
2728
) {
2829
parent::__construct( [
2930
'query' => new ObjectType( [
@@ -132,6 +133,10 @@ private function statementType(): ObjectType {
132133
'resolve' => fn( Statement $statement ) => $statement->property,
133134
],
134135
'value' => $this->valueType,
136+
'valueType' => [
137+
'type' => Type::nonNull( $this->valueTypeType ),
138+
'resolve' => fn( Statement $statement ) => $statement->valueType,
139+
],
135140
],
136141
] );
137142
}

repo/domains/reuse/src/Infrastructure/GraphQL/Schema/ValueType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct() {
1919
);
2020
$config = [
2121
'types' => array_values( $valueTypes ),
22-
'resolveType' => fn( Value $v ) => $v->content != null && $v->content instanceof StringValue
22+
'resolveType' => fn( Value $v ) => $v->content instanceof StringValue
2323
? $valueTypes[ 'VT:string' ]
2424
: $valueTypes[ 'PT:wikibase-item' ],
2525
];
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace Wikibase\Repo\Domains\Reuse\Infrastructure\GraphQL\Schema;
4+
5+
use GraphQL\Type\Definition\EnumType;
6+
use Wikibase\Repo\Domains\Reuse\Domain\Model\ValueType;
7+
8+
/**
9+
* @license GPL-2.0-or-later
10+
*/
11+
class ValueTypeType extends EnumType {
12+
13+
public function __construct() {
14+
$config = [
15+
'name' => 'ValueType',
16+
'values' => [
17+
'novalue' => [
18+
'value' => ValueType::TYPE_NO_VALUE,
19+
],
20+
'somevalue' => [
21+
'value' => ValueType::TYPE_SOME_VALUE,
22+
],
23+
'value' => [
24+
'value' => ValueType::TYPE_VALUE,
25+
],
26+
],
27+
];
28+
parent::__construct( $config );
29+
}
30+
31+
}

0 commit comments

Comments
 (0)