Skip to content

Commit c1c9e5b

Browse files
authored
Allow customizing PhpEnumType
1 parent 088f24d commit c1c9e5b

14 files changed

+112
-67
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.17.0
13+
14+
### Added
15+
16+
- Allow customizing PhpEnumType https://github.com/webonyx/graphql-php/pull/1623
17+
1218
## v15.16.1
1319

1420
### Fixed

src/Type/Definition/EnumType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* description?: string|null,
3131
* values: EnumValues|callable(): EnumValues,
3232
* astNode?: EnumTypeDefinitionNode|null,
33-
* extensionASTNodes?: array<int, EnumTypeExtensionNode>|null
33+
* extensionASTNodes?: array<EnumTypeExtensionNode>|null
3434
* }
3535
*/
3636
class EnumType extends Type implements InputType, OutputType, LeafType, NullableType, NamedType
@@ -39,7 +39,7 @@ class EnumType extends Type implements InputType, OutputType, LeafType, Nullable
3939

4040
public ?EnumTypeDefinitionNode $astNode;
4141

42-
/** @var array<int, EnumTypeExtensionNode> */
42+
/** @var array<EnumTypeExtensionNode> */
4343
public array $extensionASTNodes;
4444

4545
/** @phpstan-var EnumTypeConfig */
@@ -264,7 +264,7 @@ public function astNode(): ?EnumTypeDefinitionNode
264264
return $this->astNode;
265265
}
266266

267-
/** @return array<int, EnumTypeExtensionNode> */
267+
/** @return array<EnumTypeExtensionNode> */
268268
public function extensionASTNodes(): array
269269
{
270270
return $this->extensionASTNodes;

src/Type/Definition/InputObjectType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* fields: iterable<FieldConfig>|callable(): iterable<FieldConfig>,
2121
* parseValue?: callable(array<string, mixed>): mixed,
2222
* astNode?: InputObjectTypeDefinitionNode|null,
23-
* extensionASTNodes?: array<int, InputObjectTypeExtensionNode>|null
23+
* extensionASTNodes?: array<InputObjectTypeExtensionNode>|null
2424
* }
2525
*/
2626
class InputObjectType extends Type implements InputType, NullableType, NamedType
@@ -29,7 +29,7 @@ class InputObjectType extends Type implements InputType, NullableType, NamedType
2929

3030
public ?InputObjectTypeDefinitionNode $astNode;
3131

32-
/** @var array<int, InputObjectTypeExtensionNode> */
32+
/** @var array<InputObjectTypeExtensionNode> */
3333
public array $extensionASTNodes;
3434

3535
/** @phpstan-var InputObjectConfig */
@@ -203,7 +203,7 @@ public function astNode(): ?InputObjectTypeDefinitionNode
203203
return $this->astNode;
204204
}
205205

206-
/** @return array<int, InputObjectTypeExtensionNode> */
206+
/** @return array<InputObjectTypeExtensionNode> */
207207
public function extensionASTNodes(): array
208208
{
209209
return $this->extensionASTNodes;

src/Type/Definition/InterfaceType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* interfaces?: iterable<InterfaceTypeReference>|callable(): iterable<InterfaceTypeReference>,
2121
* resolveType?: ResolveType|null,
2222
* astNode?: InterfaceTypeDefinitionNode|null,
23-
* extensionASTNodes?: array<int, InterfaceTypeExtensionNode>|null
23+
* extensionASTNodes?: array<InterfaceTypeExtensionNode>|null
2424
* }
2525
*/
2626
class InterfaceType extends Type implements AbstractType, OutputType, CompositeType, NullableType, HasFieldsType, NamedType, ImplementingType
@@ -31,7 +31,7 @@ class InterfaceType extends Type implements AbstractType, OutputType, CompositeT
3131

3232
public ?InterfaceTypeDefinitionNode $astNode;
3333

34-
/** @var array<int, InterfaceTypeExtensionNode> */
34+
/** @var array<InterfaceTypeExtensionNode> */
3535
public array $extensionASTNodes;
3636

3737
/** @phpstan-var InterfaceConfig */
@@ -99,7 +99,7 @@ public function astNode(): ?InterfaceTypeDefinitionNode
9999
return $this->astNode;
100100
}
101101

102-
/** @return array<int, InterfaceTypeExtensionNode> */
102+
/** @return array<InterfaceTypeExtensionNode> */
103103
public function extensionASTNodes(): array
104104
{
105105
return $this->extensionASTNodes;

src/Type/Definition/NamedType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @property string $name
2020
* @property string|null $description
2121
* @property (Node&TypeDefinitionNode)|null $astNode
22-
* @property array<int, Node&TypeExtensionNode> $extensionASTNodes
22+
* @property array<Node&TypeExtensionNode> $extensionASTNodes
2323
*/
2424
interface NamedType
2525
{
@@ -36,6 +36,6 @@ public function description(): ?string;
3636
/** @return (Node&TypeDefinitionNode)|null */
3737
public function astNode(): ?Node;
3838

39-
/** @return array<int, Node&TypeExtensionNode> */
39+
/** @return array<Node&TypeExtensionNode> */
4040
public function extensionASTNodes(): array;
4141
}

src/Type/Definition/ObjectType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
* interfaces?: iterable<InterfaceTypeReference>|callable(): iterable<InterfaceTypeReference>,
6363
* isTypeOf?: (callable(mixed $objectValue, mixed $context, ResolveInfo $resolveInfo): (bool|Deferred|null))|null,
6464
* astNode?: ObjectTypeDefinitionNode|null,
65-
* extensionASTNodes?: array<int, ObjectTypeExtensionNode>|null
65+
* extensionASTNodes?: array<ObjectTypeExtensionNode>|null
6666
* }
6767
*/
6868
class ObjectType extends Type implements OutputType, CompositeType, NullableType, HasFieldsType, NamedType, ImplementingType
@@ -73,7 +73,7 @@ class ObjectType extends Type implements OutputType, CompositeType, NullableType
7373

7474
public ?ObjectTypeDefinitionNode $astNode;
7575

76-
/** @var array<int, ObjectTypeExtensionNode> */
76+
/** @var array<ObjectTypeExtensionNode> */
7777
public array $extensionASTNodes;
7878

7979
/**
@@ -172,7 +172,7 @@ public function astNode(): ?ObjectTypeDefinitionNode
172172
return $this->astNode;
173173
}
174174

175-
/** @return array<int, ObjectTypeExtensionNode> */
175+
/** @return array<ObjectTypeExtensionNode> */
176176
public function extensionASTNodes(): array
177177
{
178178
return $this->extensionASTNodes;

src/Type/Definition/PhpEnumType.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace GraphQL\Type\Definition;
44

55
use GraphQL\Error\SerializationError;
6+
use GraphQL\Language\AST\EnumTypeDefinitionNode;
7+
use GraphQL\Language\AST\EnumTypeExtensionNode;
68
use GraphQL\Utils\PhpDoc;
79
use GraphQL\Utils\Utils;
810

@@ -16,16 +18,23 @@ class PhpEnumType extends EnumType
1618
protected string $enumClass;
1719

1820
/**
19-
* @param class-string<\UnitEnum> $enum
21+
* @param class-string<\UnitEnum> $enumClass The fully qualified class name of a native PHP enum
2022
* @param string|null $name The name the enum will have in the schema, defaults to the basename of the given class
23+
* @param string|null $description The description the enum will have in the schema, defaults to PHPDoc of the given class
24+
* @param array<EnumTypeExtensionNode>|null $extensionASTNodes
2125
*
2226
* @throws \Exception
2327
* @throws \ReflectionException
2428
*/
25-
public function __construct(string $enum, ?string $name = null)
26-
{
27-
$this->enumClass = $enum;
28-
$reflection = new \ReflectionEnum($enum);
29+
public function __construct(
30+
string $enumClass,
31+
?string $name = null,
32+
?string $description = null,
33+
?EnumTypeDefinitionNode $astNode = null,
34+
?array $extensionASTNodes = null
35+
) {
36+
$this->enumClass = $enumClass;
37+
$reflection = new \ReflectionEnum($enumClass);
2938

3039
/**
3140
* @var array<string, PartialEnumValueConfig> $enumDefinitions
@@ -40,9 +49,11 @@ public function __construct(string $enum, ?string $name = null)
4049
}
4150

4251
parent::__construct([
43-
'name' => $name ?? $this->baseName($enum),
52+
'name' => $name ?? $this->baseName($enumClass),
4453
'values' => $enumDefinitions,
45-
'description' => $this->extractDescription($reflection),
54+
'description' => $description ?? $this->extractDescription($reflection),
55+
'astNode' => $astNode,
56+
'extensionASTNodes' => $extensionASTNodes,
4657
]);
4758
}
4859

src/Type/Definition/ScalarType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function astNode(): ?ScalarTypeDefinitionNode
6969
return $this->astNode;
7070
}
7171

72-
/** @return array<int, ScalarTypeExtensionNode> */
72+
/** @return array<ScalarTypeExtensionNode> */
7373
public function extensionASTNodes(): array
7474
{
7575
return $this->extensionASTNodes;

src/Type/Definition/UnionType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public function astNode(): ?UnionTypeDefinitionNode
132132
return $this->astNode;
133133
}
134134

135-
/** @return array<int, UnionTypeExtensionNode> */
135+
/** @return array<UnionTypeExtensionNode> */
136136
public function extensionASTNodes(): array
137137
{
138138
return $this->extensionASTNodes;

src/Type/Schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class Schema
7171

7272
public ?SchemaDefinitionNode $astNode;
7373

74-
/** @var array<int, SchemaExtensionNode> */
74+
/** @var array<SchemaExtensionNode> */
7575
public array $extensionASTNodes = [];
7676

7777
/**

0 commit comments

Comments
 (0)