Skip to content

Commit d6c965e

Browse files
authored
Allow enum instances in PhpEnumType::parseValue()
1 parent f32970a commit d6c965e

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
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.9.1
13+
14+
### Fixed
15+
16+
- Allow enum instances in `PhpEnumType::parseValue()` https://github.com/webonyx/graphql-php/pull/1519
17+
1218
## v15.9.0
1319

1420
### Added

src/Type/Definition/PhpEnumType.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,24 @@ public function __construct(string $enum, string $name = null)
4747

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

5555
return $value->name;
5656
}
5757

58+
public function parseValue($value)
59+
{
60+
// Can happen when variable values undergo a serialization cycle before execution
61+
if ($value instanceof $this->enumClass) {
62+
return $value;
63+
}
64+
65+
return parent::parseValue($value);
66+
}
67+
5868
/** @param class-string $class */
5969
protected function baseName(string $class): string
6070
{

tests/Type/PhpEnumTypeTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use GraphQL\Tests\Type\PhpEnumType\PhpEnum;
1515
use GraphQL\Type\Definition\ObjectType;
1616
use GraphQL\Type\Definition\PhpEnumType;
17+
use GraphQL\Type\Definition\ResolveInfo;
1718
use GraphQL\Type\Definition\Type;
1819
use GraphQL\Type\Schema;
1920
use GraphQL\Utils\SchemaPrinter;
@@ -135,6 +136,62 @@ public function testExecutesWithEnumTypeFromPhpEnum(): void
135136
], GraphQL::executeQuery($schema, '{ foo(bar: A) }')->toArray());
136137
}
137138

139+
public function testAcceptsEnumFromVariableValues(): void
140+
{
141+
$enumType = new PhpEnumType(PhpEnum::class);
142+
143+
$schema = null;
144+
$schema = new Schema([
145+
'query' => new ObjectType([
146+
'name' => 'Query',
147+
'fields' => [
148+
'foo' => [
149+
'type' => Type::nonNull($enumType),
150+
'args' => [
151+
'bar' => [
152+
'type' => Type::nonNull($enumType),
153+
],
154+
],
155+
'resolve' => static function (bool $executeAgain, array $args, $context, ResolveInfo $resolveInfo) use (&$schema): PhpEnum {
156+
$bar = $args['bar'];
157+
assert($bar === PhpEnum::A);
158+
159+
if ($executeAgain) {
160+
$executionResult = GraphQL::executeQuery(
161+
$schema,
162+
'query ($bar: PhpEnum!) { foo(bar: $bar) }',
163+
false,
164+
null,
165+
$resolveInfo->variableValues
166+
);
167+
self::assertSame([
168+
'data' => [
169+
'foo' => 'A',
170+
],
171+
], $executionResult->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS));
172+
}
173+
174+
return $bar;
175+
},
176+
],
177+
],
178+
]),
179+
]);
180+
181+
$executionResult = GraphQL::executeQuery(
182+
$schema,
183+
'query ($bar: PhpEnum!) { foo(bar: $bar) }',
184+
true,
185+
null,
186+
['bar' => 'A']
187+
);
188+
self::assertSame([
189+
'data' => [
190+
'foo' => 'A',
191+
],
192+
], $executionResult->toArray(DebugFlag::RETHROW_INTERNAL_EXCEPTIONS));
193+
}
194+
138195
public function testFailsToSerializeNonEnum(): void
139196
{
140197
$enumType = new PhpEnumType(PhpEnum::class);

0 commit comments

Comments
 (0)