Skip to content

Commit

Permalink
Allow enum instances in PhpEnumType::parseValue()
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia committed Jan 25, 2024
1 parent f32970a commit d6c965e
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
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.9.1

### Fixed

- Allow enum instances in `PhpEnumType::parseValue()` https://github.com/webonyx/graphql-php/pull/1519

## v15.9.0

### Added
Expand Down
12 changes: 11 additions & 1 deletion src/Type/Definition/PhpEnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,24 @@ public function __construct(string $enum, string $name = null)

public function serialize($value): string
{
if (! is_a($value, $this->enumClass)) {
if (! ($value instanceof $this->enumClass)) {
$notEnum = Utils::printSafe($value);
throw new SerializationError("Cannot serialize value as enum: {$notEnum}, expected instance of {$this->enumClass}.");
}

return $value->name;
}

public function parseValue($value)
{
// Can happen when variable values undergo a serialization cycle before execution
if ($value instanceof $this->enumClass) {
return $value;
}

return parent::parseValue($value);
}

/** @param class-string $class */
protected function baseName(string $class): string
{
Expand Down
57 changes: 57 additions & 0 deletions tests/Type/PhpEnumTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use GraphQL\Tests\Type\PhpEnumType\PhpEnum;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\PhpEnumType;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\Utils\SchemaPrinter;
Expand Down Expand Up @@ -135,6 +136,62 @@ public function testExecutesWithEnumTypeFromPhpEnum(): void
], GraphQL::executeQuery($schema, '{ foo(bar: A) }')->toArray());
}

public function testAcceptsEnumFromVariableValues(): void
{
$enumType = new PhpEnumType(PhpEnum::class);

$schema = null;
$schema = new Schema([
'query' => new ObjectType([
'name' => 'Query',
'fields' => [
'foo' => [
'type' => Type::nonNull($enumType),
'args' => [
'bar' => [
'type' => Type::nonNull($enumType),
],
],
'resolve' => static function (bool $executeAgain, array $args, $context, ResolveInfo $resolveInfo) use (&$schema): PhpEnum {
$bar = $args['bar'];
assert($bar === PhpEnum::A);

if ($executeAgain) {
$executionResult = GraphQL::executeQuery(
$schema,
'query ($bar: PhpEnum!) { foo(bar: $bar) }',
false,
null,
$resolveInfo->variableValues
);
self::assertSame([
'data' => [
'foo' => 'A',
],
], $executionResult->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS));
}

return $bar;
},
],
],
]),
]);

$executionResult = GraphQL::executeQuery(
$schema,
'query ($bar: PhpEnum!) { foo(bar: $bar) }',
true,
null,
['bar' => 'A']
);
self::assertSame([
'data' => [
'foo' => 'A',
],
], $executionResult->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS));
}

public function testFailsToSerializeNonEnum(): void
{
$enumType = new PhpEnumType(PhpEnum::class);
Expand Down

0 comments on commit d6c965e

Please sign in to comment.