Skip to content

Commit 2fe282e

Browse files
committed
Call resolveValue before calling resolveType
In hindsight it makes more sense to first resolve the value to the desired type before calling `resolveType`. Related to webonyx#1776
1 parent 20a3633 commit 2fe282e

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public function resolveType($objectValue, $context, ResolveInfo $info);
2525

2626
/**
2727
* Receives the original resolved value and transforms it if necessary.
28+
* This will be called before calling `resolveType`.
2829
*
2930
* @param mixed $objectValue The resolved value for the object type
3031
* @param mixed $context The context that was passed to GraphQL::execute()

tests/Executor/AbstractTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ($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 ($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)