Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

## v15.25.1

### Changed

- Call `AbstractType::resolveValue` before `AbstractType::resolveType` https://github.com/webonyx/graphql-php/pull/1781

## v15.25.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,8 +1082,8 @@ protected function completeAbstractValue(
&$result,
$contextValue
) {
$typeCandidate = $returnType->resolveType($result, $contextValue, $info);
$result = $returnType->resolveValue($result, $contextValue, $info);
$typeCandidate = $returnType->resolveType($result, $contextValue, $info);

if ($typeCandidate === null) {
$runtimeType = static::defaultTypeResolver($result, $contextValue, $info, $returnType);
Expand Down
20 changes: 12 additions & 8 deletions src/Type/Definition/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,28 @@
interface AbstractType
{
/**
* Resolves the concrete ObjectType for the given value.
* Receives the original resolved value and transforms it if necessary.
*
* This will be called before `resolveType`.
*
* @param mixed $objectValue The resolved value for the object type
* @param mixed $context The context that was passed to GraphQL::execute()
*
* @return ObjectType|string|callable|Deferred|null
*
* @phpstan-return ResolveTypeReturn
* @return mixed The possibly transformed value
*/
public function resolveType($objectValue, $context, ResolveInfo $info);
public function resolveValue($objectValue, $context, ResolveInfo $info);

/**
* Receives the original resolved value and transforms it if necessary.
* Resolves the concrete ObjectType for the given value.
*
* This will be called after `resolveValue`.
*
* @param mixed $objectValue The resolved value for the object type
* @param mixed $context The context that was passed to GraphQL::execute()
*
* @return mixed The possibly transformed value
* @return ObjectType|string|callable|Deferred|null
*
* @phpstan-return ResolveTypeReturn
*/
public function resolveValue($objectValue, $context, ResolveInfo $info);
public function resolveType($objectValue, $context, ResolveInfo $info);
}
16 changes: 8 additions & 8 deletions src/Type/Definition/InterfaceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,22 @@ public static function assertInterfaceType($type): self
return $type;
}

public function resolveType($objectValue, $context, ResolveInfo $info)
public function resolveValue($objectValue, $context, ResolveInfo $info)
{
if (isset($this->config['resolveType'])) {
return ($this->config['resolveType'])($objectValue, $context, $info);
if (isset($this->config['resolveValue'])) {
return ($this->config['resolveValue'])($objectValue, $context, $info);
}

return null;
return $objectValue;
}

public function resolveValue($objectValue, $context, ResolveInfo $info)
public function resolveType($objectValue, $context, ResolveInfo $info)
{
if (isset($this->config['resolveValue'])) {
return ($this->config['resolveValue'])($objectValue, $context, $info);
if (isset($this->config['resolveType'])) {
return ($this->config['resolveType'])($objectValue, $context, $info);
}

return $objectValue;
return null;
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Type/Definition/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,22 @@ public function getTypes(): array
return $this->types;
}

public function resolveType($objectValue, $context, ResolveInfo $info)
public function resolveValue($objectValue, $context, ResolveInfo $info)
{
if (isset($this->config['resolveType'])) {
return ($this->config['resolveType'])($objectValue, $context, $info);
if (isset($this->config['resolveValue'])) {
return ($this->config['resolveValue'])($objectValue, $context, $info);
}

return null;
return $objectValue;
}

public function resolveValue($objectValue, $context, ResolveInfo $info)
public function resolveType($objectValue, $context, ResolveInfo $info)
{
if (isset($this->config['resolveValue'])) {
return ($this->config['resolveValue'])($objectValue, $context, $info);
if (isset($this->config['resolveType'])) {
return ($this->config['resolveType'])($objectValue, $context, $info);
}

return $objectValue;
return null;
}

public function assertValid(): void
Expand Down
40 changes: 20 additions & 20 deletions tests/Executor/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public function testResolveTypeOnInterfaceYieldsUsefulError(): void
return null;
},
'fields' => [
'name' => ['type' => Type::string()],
'name' => Type::string(),
],
]);

Expand Down Expand Up @@ -273,23 +273,23 @@ public function testResolveTypeOnUnionYieldsUsefulError(): void
$HumanType = new ObjectType([
'name' => 'Human',
'fields' => [
'name' => ['type' => Type::string()],
'name' => Type::string(),
],
]);

$DogType = new ObjectType([
'name' => 'Dog',
'fields' => [
'name' => ['type' => Type::string()],
'woofs' => ['type' => Type::boolean()],
'name' => Type::string(),
'woofs' => Type::boolean(),
],
]);

$CatType = new ObjectType([
'name' => 'Cat',
'fields' => [
'name' => ['type' => Type::string()],
'meows' => ['type' => Type::boolean()],
'name' => Type::string(),
'meows' => Type::boolean(),
],
]);

Expand Down Expand Up @@ -792,20 +792,20 @@ public function testResolveValueAllowsModifyingObjectValueForInterfaceType(): vo
{
$PetType = new InterfaceType([
'name' => 'Pet',
'resolveType' => static function (PetEntity $objectValue): string {
if ($objectValue->type === 'dog') {
return 'Dog';
}

return 'Cat';
},
'resolveValue' => static function (PetEntity $objectValue): object {
if ($objectValue->type === 'dog') {
return new Dog($objectValue->name, $objectValue->vocalizes);
}

return new Cat($objectValue->name, $objectValue->vocalizes);
},
'resolveType' => static function (object $objectValue): string {
if ($objectValue instanceof Dog) {
return 'Dog';
}

return 'Cat';
},
'fields' => [
'name' => Type::string(),
],
Expand Down Expand Up @@ -916,20 +916,20 @@ public function testResolveValueAllowsModifyingObjectValueForUnionType(): void
$PetType = new UnionType([
'name' => 'Pet',
'types' => [$DogType, $CatType],
'resolveType' => static function (PetEntity $objectValue): string {
if ($objectValue->type === 'dog') {
return 'Dog';
}

return 'Cat';
},
'resolveValue' => static function (PetEntity $objectValue): object {
if ($objectValue->type === 'dog') {
return new Dog($objectValue->name, $objectValue->vocalizes);
}

return new Cat($objectValue->name, $objectValue->vocalizes);
},
'resolveType' => static function (object $objectValue): string {
if ($objectValue instanceof Dog) {
return 'Dog';
}

return 'Cat';
},
]);

$schema = new Schema([
Expand Down
Loading