Skip to content

Commit 5c1d375

Browse files
authored
Provide a custom name for Enum in PhpEnumType (#1368)
1 parent 8371e2e commit 5c1d375

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
### Added
13+
14+
- Provide a custom name for the Enum generated by `PhpEnumType`
15+
1216
## v15.2.5
1317

1418
### Fixed

src/Type/Definition/PhpEnumType.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ class PhpEnumType extends EnumType
1717
/** @var class-string<\UnitEnum> */
1818
protected string $enumClass;
1919

20-
/** @param class-string<\UnitEnum> $enum */
21-
public function __construct(string $enum)
20+
/**
21+
* @param class-string<\UnitEnum> $enum
22+
* @param string|null $name The name the enum will have in the schema, defaults to the basename of the given class
23+
*/
24+
public function __construct(string $enum, ?string $name = null)
2225
{
2326
$this->enumClass = $enum;
2427
$reflection = new \ReflectionEnum($enum);
@@ -36,7 +39,7 @@ public function __construct(string $enum)
3639
}
3740

3841
parent::__construct([
39-
'name' => $this->baseName($enum),
42+
'name' => $name ?? $this->baseName($enum),
4043
'values' => $enumDefinitions,
4144
'description' => $this->extractDescription($reflection),
4245
]);

tests/Type/PhpEnumTypeTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,23 @@ enum PhpEnum {
4545
);
4646
}
4747

48+
public function testConstructEnumTypeFromPhpEnumWithCustomName(): void
49+
{
50+
$enumType = new PhpEnumType(PhpEnum::class, 'CustomNamedPhpEnum');
51+
self::assertSame(
52+
<<<'GRAPHQL'
53+
"foo"
54+
enum CustomNamedPhpEnum {
55+
"bar"
56+
A
57+
B @deprecated
58+
C @deprecated(reason: "baz")
59+
}
60+
GRAPHQL,
61+
SchemaPrinter::printType($enumType)
62+
);
63+
}
64+
4865
public function testConstructEnumTypeFromPhpEnumWithDocBlockDescriptions(): void
4966
{
5067
$enumType = new PhpEnumType(DocBlockPhpEnum::class);

0 commit comments

Comments
 (0)