Skip to content

Commit 60ef919

Browse files
authored
Call AbstractType::resolveValue before AbstractType::resolveType
1 parent 20a3633 commit 60ef919

File tree

6 files changed

+55
-45
lines changed

6 files changed

+55
-45
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
## v15.25.1
13+
14+
### Changed
15+
16+
- Call `AbstractType::resolveValue` before `AbstractType::resolveType` https://github.com/webonyx/graphql-php/pull/1781
17+
1218
## v15.25.0
1319

1420
### Added

src/Executor/ReferenceExecutor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,8 @@ protected function completeAbstractValue(
10821082
&$result,
10831083
$contextValue
10841084
) {
1085-
$typeCandidate = $returnType->resolveType($result, $contextValue, $info);
10861085
$result = $returnType->resolveValue($result, $contextValue, $info);
1086+
$typeCandidate = $returnType->resolveType($result, $contextValue, $info);
10871087

10881088
if ($typeCandidate === null) {
10891089
$runtimeType = static::defaultTypeResolver($result, $contextValue, $info, $returnType);

src/Type/Definition/AbstractType.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@
1212
interface AbstractType
1313
{
1414
/**
15-
* Resolves the concrete ObjectType for the given value.
15+
* Receives the original resolved value and transforms it if necessary.
16+
*
17+
* This will be called before `resolveType`.
1618
*
1719
* @param mixed $objectValue The resolved value for the object type
1820
* @param mixed $context The context that was passed to GraphQL::execute()
1921
*
20-
* @return ObjectType|string|callable|Deferred|null
21-
*
22-
* @phpstan-return ResolveTypeReturn
22+
* @return mixed The possibly transformed value
2323
*/
24-
public function resolveType($objectValue, $context, ResolveInfo $info);
24+
public function resolveValue($objectValue, $context, ResolveInfo $info);
2525

2626
/**
27-
* Receives the original resolved value and transforms it if necessary.
27+
* Resolves the concrete ObjectType for the given value.
28+
*
29+
* This will be called after `resolveValue`.
2830
*
2931
* @param mixed $objectValue The resolved value for the object type
3032
* @param mixed $context The context that was passed to GraphQL::execute()
3133
*
32-
* @return mixed The possibly transformed value
34+
* @return ObjectType|string|callable|Deferred|null
35+
*
36+
* @phpstan-return ResolveTypeReturn
3337
*/
34-
public function resolveValue($objectValue, $context, ResolveInfo $info);
38+
public function resolveType($objectValue, $context, ResolveInfo $info);
3539
}

src/Type/Definition/InterfaceType.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,22 @@ public static function assertInterfaceType($type): self
6969
return $type;
7070
}
7171

72-
public function resolveType($objectValue, $context, ResolveInfo $info)
72+
public function resolveValue($objectValue, $context, ResolveInfo $info)
7373
{
74-
if (isset($this->config['resolveType'])) {
75-
return ($this->config['resolveType'])($objectValue, $context, $info);
74+
if (isset($this->config['resolveValue'])) {
75+
return ($this->config['resolveValue'])($objectValue, $context, $info);
7676
}
7777

78-
return null;
78+
return $objectValue;
7979
}
8080

81-
public function resolveValue($objectValue, $context, ResolveInfo $info)
81+
public function resolveType($objectValue, $context, ResolveInfo $info)
8282
{
83-
if (isset($this->config['resolveValue'])) {
84-
return ($this->config['resolveValue'])($objectValue, $context, $info);
83+
if (isset($this->config['resolveType'])) {
84+
return ($this->config['resolveType'])($objectValue, $context, $info);
8585
}
8686

87-
return $objectValue;
87+
return null;
8888
}
8989

9090
/**

src/Type/Definition/UnionType.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,22 @@ public function getTypes(): array
108108
return $this->types;
109109
}
110110

111-
public function resolveType($objectValue, $context, ResolveInfo $info)
111+
public function resolveValue($objectValue, $context, ResolveInfo $info)
112112
{
113-
if (isset($this->config['resolveType'])) {
114-
return ($this->config['resolveType'])($objectValue, $context, $info);
113+
if (isset($this->config['resolveValue'])) {
114+
return ($this->config['resolveValue'])($objectValue, $context, $info);
115115
}
116116

117-
return null;
117+
return $objectValue;
118118
}
119119

120-
public function resolveValue($objectValue, $context, ResolveInfo $info)
120+
public function resolveType($objectValue, $context, ResolveInfo $info)
121121
{
122-
if (isset($this->config['resolveValue'])) {
123-
return ($this->config['resolveValue'])($objectValue, $context, $info);
122+
if (isset($this->config['resolveType'])) {
123+
return ($this->config['resolveType'])($objectValue, $context, $info);
124124
}
125125

126-
return $objectValue;
126+
return null;
127127
}
128128

129129
public function assertValid(): void

tests/Executor/AbstractTest.php

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function testResolveTypeOnInterfaceYieldsUsefulError(): void
188188
return null;
189189
},
190190
'fields' => [
191-
'name' => ['type' => Type::string()],
191+
'name' => Type::string(),
192192
],
193193
]);
194194

@@ -273,23 +273,23 @@ public function testResolveTypeOnUnionYieldsUsefulError(): void
273273
$HumanType = new ObjectType([
274274
'name' => 'Human',
275275
'fields' => [
276-
'name' => ['type' => Type::string()],
276+
'name' => Type::string(),
277277
],
278278
]);
279279

280280
$DogType = new ObjectType([
281281
'name' => 'Dog',
282282
'fields' => [
283-
'name' => ['type' => Type::string()],
284-
'woofs' => ['type' => Type::boolean()],
283+
'name' => Type::string(),
284+
'woofs' => Type::boolean(),
285285
],
286286
]);
287287

288288
$CatType = new ObjectType([
289289
'name' => 'Cat',
290290
'fields' => [
291-
'name' => ['type' => Type::string()],
292-
'meows' => ['type' => Type::boolean()],
291+
'name' => Type::string(),
292+
'meows' => Type::boolean(),
293293
],
294294
]);
295295

@@ -792,20 +792,20 @@ public function testResolveValueAllowsModifyingObjectValueForInterfaceType(): vo
792792
{
793793
$PetType = new InterfaceType([
794794
'name' => 'Pet',
795-
'resolveType' => static function (PetEntity $objectValue): string {
796-
if ($objectValue->type === 'dog') {
797-
return 'Dog';
798-
}
799-
800-
return 'Cat';
801-
},
802795
'resolveValue' => static function (PetEntity $objectValue): object {
803796
if ($objectValue->type === 'dog') {
804797
return new Dog($objectValue->name, $objectValue->vocalizes);
805798
}
806799

807800
return new Cat($objectValue->name, $objectValue->vocalizes);
808801
},
802+
'resolveType' => static function (object $objectValue): string {
803+
if ($objectValue instanceof Dog) {
804+
return 'Dog';
805+
}
806+
807+
return 'Cat';
808+
},
809809
'fields' => [
810810
'name' => Type::string(),
811811
],
@@ -916,20 +916,20 @@ public function testResolveValueAllowsModifyingObjectValueForUnionType(): void
916916
$PetType = new UnionType([
917917
'name' => 'Pet',
918918
'types' => [$DogType, $CatType],
919-
'resolveType' => static function (PetEntity $objectValue): string {
920-
if ($objectValue->type === 'dog') {
921-
return 'Dog';
922-
}
923-
924-
return 'Cat';
925-
},
926919
'resolveValue' => static function (PetEntity $objectValue): object {
927920
if ($objectValue->type === 'dog') {
928921
return new Dog($objectValue->name, $objectValue->vocalizes);
929922
}
930923

931924
return new Cat($objectValue->name, $objectValue->vocalizes);
932925
},
926+
'resolveType' => static function (object $objectValue): string {
927+
if ($objectValue instanceof Dog) {
928+
return 'Dog';
929+
}
930+
931+
return 'Cat';
932+
},
933933
]);
934934

935935
$schema = new Schema([

0 commit comments

Comments
 (0)