Skip to content

Commit eedba83

Browse files
jenkins-botGerrit Code Review
authored andcommitted
Merge "GQL: Introduce PropertyValuePair interface type"
2 parents 7a5f6a6 + 7a08a68 commit eedba83

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

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

5-
use GraphQL\Type\Definition\ObjectType;
5+
use GraphQL\Type\Definition\InterfaceType;
66
use GraphQL\Type\Definition\Type;
77
use Wikibase\Repo\Domains\Reuse\Domain\Model\PropertyValuePair;
8+
use Wikibase\Repo\Domains\Reuse\Domain\Model\Statement;
89

910
/**
1011
* @license GPL-2.0-or-later
1112
*/
12-
class PropertyValuePairType extends ObjectType {
13+
class PropertyValuePairType extends InterfaceType {
1314

1415
public function __construct(
1516
PredicatePropertyType $predicateType,
@@ -20,16 +21,16 @@ public function __construct(
2021
'fields' => [
2122
'property' => [
2223
'type' => Type::nonNull( $predicateType ),
23-
'resolve' => fn( PropertyValuePair $rootValue ) => $rootValue->property,
24+
'resolve' => fn( PropertyValuePair|Statement $rootValue ) => $rootValue->property,
2425
],
2526
'value' => [
2627
'type' => $valueType,
27-
// The whole PropertyValuePair is passed down here so that the Value type has access to the property data type.
28-
'resolve' => fn( PropertyValuePair $rootValue ) => $rootValue->value ? $rootValue : null,
28+
// The whole root value is passed down here so that the Value type has access to the property data type.
29+
'resolve' => fn( PropertyValuePair|Statement $rootValue ) => $rootValue->value ? $rootValue : null,
2930
],
3031
'valueType' => [
3132
'type' => Type::nonNull( $valueTypeType ),
32-
'resolve' => fn( PropertyValuePair $rootValue ) => $rootValue->valueType,
33+
'resolve' => fn( PropertyValuePair|Statement $rootValue ) => $rootValue->valueType,
3334
],
3435
],
3536
] );

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

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ public function __construct(
2222
private readonly ItemIdType $itemIdType,
2323
private readonly SiteIdType $siteIdType,
2424
private readonly LanguageCodeType $languageCodeType,
25-
private readonly PredicatePropertyType $predicatePropertyType,
2625
private readonly PropertyValuePairType $propertyValuePairType,
27-
private readonly ValueType $valueType,
28-
private readonly ValueTypeType $valueTypeType,
2926
private readonly PropertyIdType $propertyIdType
3027
) {
3128
parent::__construct( [
@@ -110,9 +107,22 @@ private function itemType(): ObjectType {
110107
}
111108

112109
private function statementType(): ObjectType {
110+
$qualifierType = new ObjectType( [
111+
'name' => 'Qualifier',
112+
'fields' => [
113+
$this->propertyValuePairType->getField( 'property' ),
114+
$this->propertyValuePairType->getField( 'value' ),
115+
$this->propertyValuePairType->getField( 'valueType' ),
116+
],
117+
'interfaces' => [ $this->propertyValuePairType ],
118+
] );
119+
113120
return new ObjectType( [
114121
'name' => 'Statement',
115122
'fields' => [
123+
$this->propertyValuePairType->getField( 'property' ),
124+
$this->propertyValuePairType->getField( 'value' ),
125+
$this->propertyValuePairType->getField( 'valueType' ),
116126
'id' => [
117127
'type' => Type::nonNull( Type::string() ),
118128
'resolve' => fn( Statement $statement ) => $statement->id,
@@ -123,7 +133,7 @@ private function statementType(): ObjectType {
123133
],
124134
'qualifiers' => [
125135
// @phan-suppress-next-line PhanUndeclaredInvokeInCallable
126-
'type' => Type::nonNull( Type::listOf( $this->propertyValuePairType ) ),
136+
'type' => Type::nonNull( Type::listOf( $qualifierType ) ),
127137
'args' => [
128138
'propertyId' => Type::nonNull( $this->propertyIdType ),
129139
],
@@ -135,20 +145,8 @@ private function statementType(): ObjectType {
135145
'type' => Type::nonNull( Type::listOf( $this->referenceType() ) ),
136146
'resolve' => fn( Statement $statement ) => $statement->references,
137147
],
138-
'property' => [
139-
'type' => Type::nonNull( $this->predicatePropertyType ),
140-
'resolve' => fn( Statement $statement ) => $statement->property,
141-
],
142-
'value' => [
143-
'type' => $this->valueType,
144-
// The whole Statement is passed down here so that the Value type has access to the property data type.
145-
'resolve' => fn( Statement $statement ) => $statement->value ? $statement : null,
146-
],
147-
'valueType' => [
148-
'type' => Type::nonNull( $this->valueTypeType ),
149-
'resolve' => fn( Statement $statement ) => $statement->valueType,
150-
],
151148
],
149+
'interfaces' => [ $this->propertyValuePairType ],
152150
] );
153151
}
154152

@@ -170,12 +168,22 @@ private function rankType(): EnumType {
170168
}
171169

172170
private function referenceType(): ObjectType {
171+
$referencePartType = new ObjectType( [
172+
'name' => 'ReferencePart',
173+
'fields' => [
174+
$this->propertyValuePairType->getField( 'property' ),
175+
$this->propertyValuePairType->getField( 'value' ),
176+
$this->propertyValuePairType->getField( 'valueType' ),
177+
],
178+
'interfaces' => [ $this->propertyValuePairType ],
179+
] );
180+
173181
return new ObjectType( [
174182
'name' => 'Reference',
175183
'fields' => [
176184
'parts' => [
177185
// @phan-suppress-next-line PhanUndeclaredInvokeInCallable
178-
'type' => Type::nonNull( Type::listOf( $this->propertyValuePairType ) ),
186+
'type' => Type::nonNull( Type::listOf( $referencePartType ) ),
179187
'resolve' => fn( Reference $reference ) => $reference->parts,
180188
],
181189
],

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,17 @@ type Sitelink {
2424

2525
scalar PropertyId
2626

27-
type Statement {
28-
id: String!
29-
rank: Rank!
30-
qualifiers(propertyId: PropertyId!): [PropertyValuePair]!
31-
references: [Reference]!
27+
type Statement implements PropertyValuePair {
3228
property: PredicateProperty!
3329
value: Value
3430
valueType: ValueType!
31+
id: String!
32+
rank: Rank!
33+
qualifiers(propertyId: PropertyId!): [Qualifier]!
34+
references: [Reference]!
3535
}
3636

37-
enum Rank {
38-
deprecated
39-
normal
40-
preferred
41-
}
42-
43-
type PropertyValuePair {
37+
interface PropertyValuePair {
4438
property: PredicateProperty!
4539
value: Value
4640
valueType: ValueType!
@@ -112,6 +106,24 @@ enum ValueType {
112106
value
113107
}
114108

109+
enum Rank {
110+
deprecated
111+
normal
112+
preferred
113+
}
114+
115+
type Qualifier implements PropertyValuePair {
116+
property: PredicateProperty!
117+
value: Value
118+
valueType: ValueType!
119+
}
120+
115121
type Reference {
116-
parts: [PropertyValuePair]!
122+
parts: [ReferencePart]!
123+
}
124+
125+
type ReferencePart implements PropertyValuePair {
126+
property: PredicateProperty!
127+
value: Value
128+
valueType: ValueType!
117129
}

repo/domains/reuse/src/WbReuse.ServiceWiring.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@
4343
},
4444
'WbReuse.GraphQLSchema' => function( MediaWikiServices $services ): Schema {
4545
$languageCodeType = WbReuse::getLanguageCodeType( $services );
46-
$predicatePropertyType = new PredicatePropertyType(
47-
WbReuse::getPropertyLabelsResolver( $services ),
48-
$languageCodeType,
49-
);
50-
$valueType = new ValueType( WikibaseRepo::getDataTypeDefinitions( $services )->getGraphqlValueTypes() );
51-
$valueTypeType = new ValueTypeType();
5246

5347
return new Schema(
5448
new ItemResolver(
@@ -67,10 +61,14 @@
6761
WikibaseRepo::getSettings( $services ),
6862
),
6963
$languageCodeType,
70-
$predicatePropertyType,
71-
new PropertyValuePairType( $predicatePropertyType, $valueType, $valueTypeType ),
72-
$valueType,
73-
$valueTypeType,
64+
new PropertyValuePairType(
65+
new PredicatePropertyType(
66+
WbReuse::getPropertyLabelsResolver( $services ),
67+
$languageCodeType,
68+
),
69+
new ValueType( WikibaseRepo::getDataTypeDefinitions( $services )->getGraphqlValueTypes() ),
70+
new ValueTypeType()
71+
),
7472
new PropertyIdType()
7573
);
7674
},

0 commit comments

Comments
 (0)