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
20 changes: 20 additions & 0 deletions src/Executor/PromiseExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace GraphQL\Executor;

use GraphQL\Executor\Promise\Promise;

class PromiseExecutor implements ExecutorImplementation
{
private Promise $result;

public function __construct(Promise $result)
{
$this->result = $result;
}

public function doExecute(): Promise
{
return $this->result;
}
}
22 changes: 7 additions & 15 deletions src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,10 @@ public static function create(
);

if (is_array($exeContext)) {
return new class($promiseAdapter->createFulfilled(new ExecutionResult(null, $exeContext))) implements ExecutorImplementation {
private Promise $result;
$executionResult = new ExecutionResult(null, $exeContext);
$fulfilledPromise = $promiseAdapter->createFulfilled($executionResult);

public function __construct(Promise $result)
{
$this->result = $result;
}

public function doExecute(): Promise
{
return $this->result;
}
};
return new PromiseExecutor($fulfilledPromise);
}

return new static($exeContext);
Expand Down Expand Up @@ -1073,7 +1064,7 @@ protected function completeLeafValue(LeafType $returnType, &$result)
* @param \ArrayObject<int, FieldNode> $fieldNodes
* @param list<string|int> $path
* @param list<string|int> $unaliasedPath
* @param array<mixed> $result
* @param mixed $result
* @param mixed $contextValue
*
* @throws \Exception
Expand All @@ -1092,6 +1083,7 @@ protected function completeAbstractValue(
$contextValue
) {
$typeCandidate = $returnType->resolveType($result, $contextValue, $info);
$result = $returnType->resolveValue($result, $contextValue, $info);

if ($typeCandidate === null) {
$runtimeType = static::defaultTypeResolver($result, $contextValue, $info, $returnType);
Expand Down Expand Up @@ -1272,7 +1264,7 @@ protected function completeObjectValue(

/**
* @param \ArrayObject<int, FieldNode> $fieldNodes
* @param array<mixed> $result
* @param mixed $result
*/
protected function invalidReturnTypeError(
ObjectType $returnType,
Expand Down Expand Up @@ -1408,7 +1400,7 @@ protected function executeFields(ObjectType $parentType, $rootValue, array $path
*
* @param array<mixed>|mixed $results
*
* @return array<mixed>|\stdClass|mixed
* @return non-empty-array<mixed>|\stdClass|mixed
*/
protected static function fixResultsIfEmptyArray($results)
{
Expand Down
11 changes: 11 additions & 0 deletions src/Type/Definition/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @phpstan-type ResolveTypeReturn ObjectType|string|callable(): (ObjectType|string|null)|Deferred|null
* @phpstan-type ResolveType callable(mixed $objectValue, mixed $context, ResolveInfo $resolveInfo): ResolveTypeReturn
* @phpstan-type ResolveValue callable(mixed $objectValue, mixed $context, ResolveInfo $resolveInfo): mixed
*/
interface AbstractType
{
Expand All @@ -21,4 +22,14 @@ interface AbstractType
* @phpstan-return ResolveTypeReturn
*/
public function resolveType($objectValue, $context, ResolveInfo $info);

/**
* Receives the original resolved value and transforms it if necessary.
*
* @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
*/
public function resolveValue($objectValue, $context, ResolveInfo $info);
}
11 changes: 11 additions & 0 deletions src/Type/Definition/InterfaceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

/**
* @phpstan-import-type ResolveType from AbstractType
* @phpstan-import-type ResolveValue from AbstractType
* @phpstan-import-type FieldsConfig from FieldDefinition
*
* @phpstan-type InterfaceTypeReference InterfaceType|callable(): InterfaceType
Expand All @@ -19,6 +20,7 @@
* fields: FieldsConfig,
* interfaces?: iterable<InterfaceTypeReference>|callable(): iterable<InterfaceTypeReference>,
* resolveType?: ResolveType|null,
* resolveValue?: ResolveValue|null,
* astNode?: InterfaceTypeDefinitionNode|null,
* extensionASTNodes?: array<InterfaceTypeExtensionNode>|null
* }
Expand Down Expand Up @@ -76,6 +78,15 @@ public function resolveType($objectValue, $context, ResolveInfo $info)
return null;
}

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

return $objectValue;
}

/**
* @throws Error
* @throws InvariantViolation
Expand Down
11 changes: 11 additions & 0 deletions src/Type/Definition/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

/**
* @phpstan-import-type ResolveType from AbstractType
* @phpstan-import-type ResolveValue from AbstractType
*
* @phpstan-type ObjectTypeReference ObjectType|callable(): ObjectType
* @phpstan-type UnionConfig array{
* name?: string|null,
* description?: string|null,
* types: iterable<ObjectTypeReference>|callable(): iterable<ObjectTypeReference>,
* resolveType?: ResolveType|null,
* resolveValue?: ResolveValue|null,
* astNode?: UnionTypeDefinitionNode|null,
* extensionASTNodes?: array<UnionTypeExtensionNode>|null
* }
Expand Down Expand Up @@ -115,6 +117,15 @@ public function resolveType($objectValue, $context, ResolveInfo $info)
return null;
}

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

return $objectValue;
}

public function assertValid(): void
{
Utils::assertValidName($this->name);
Expand Down
Loading
Loading