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

+6
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

+3-3
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

+3-3
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

+3-3
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

+2-2
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

+3-3
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

+18-7
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

+1-1
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

+1-1
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

+1-1
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
/**

src/Type/SchemaValidationContext.php

+17-19
Original file line numberDiff line numberDiff line change
@@ -386,36 +386,38 @@ private function validateFields(Type $type): void
386386
/**
387387
* @param Schema|ObjectType|InterfaceType|UnionType|EnumType|InputObjectType|Directive $obj
388388
*
389-
* @return array<int, SchemaDefinitionNode|SchemaExtensionNode>|array<int, ObjectTypeDefinitionNode|ObjectTypeExtensionNode>|array<int, InterfaceTypeDefinitionNode|InterfaceTypeExtensionNode>|array<int, UnionTypeDefinitionNode|UnionTypeExtensionNode>|array<int, EnumTypeDefinitionNode|EnumTypeExtensionNode>|array<int, InputObjectTypeDefinitionNode|InputObjectTypeExtensionNode>|array<int, DirectiveDefinitionNode>
389+
* @return list<SchemaDefinitionNode|SchemaExtensionNode>|list<ObjectTypeDefinitionNode|ObjectTypeExtensionNode>|list<InterfaceTypeDefinitionNode|InterfaceTypeExtensionNode>|list<UnionTypeDefinitionNode|UnionTypeExtensionNode>|list< EnumTypeDefinitionNode|EnumTypeExtensionNode>|list<InputObjectTypeDefinitionNode|InputObjectTypeExtensionNode>|list<DirectiveDefinitionNode>
390390
*/
391391
private function getAllNodes(object $obj): array
392392
{
393+
$astNode = $obj->astNode;
394+
393395
if ($obj instanceof Schema) {
394-
$astNode = $obj->astNode;
395396
$extensionNodes = $obj->extensionASTNodes;
396397
} elseif ($obj instanceof Directive) {
397-
$astNode = $obj->astNode;
398398
$extensionNodes = [];
399399
} else {
400-
$astNode = $obj->astNode;
401400
$extensionNodes = $obj->extensionASTNodes;
402401
}
403402

404-
return $astNode !== null
405-
? \array_merge([$astNode], $extensionNodes)
406-
: $extensionNodes;
403+
$allNodes = $astNode === null
404+
? []
405+
: [$astNode];
406+
foreach ($extensionNodes as $extensionNode) {
407+
$allNodes[] = $extensionNode;
408+
}
409+
410+
return $allNodes;
407411
}
408412

409413
/**
410414
* @param ObjectType|InterfaceType $type
411415
*
412-
* @return array<int, FieldDefinitionNode>
416+
* @return list<FieldDefinitionNode>
413417
*/
414418
private function getAllFieldNodes(Type $type, string $fieldName): array
415419
{
416-
$allNodes = $type->astNode !== null
417-
? \array_merge([$type->astNode], $type->extensionASTNodes)
418-
: $type->extensionASTNodes;
420+
$allNodes = array_filter([$type->astNode, ...$type->extensionASTNodes]);
419421

420422
$matchingFieldNodes = [];
421423

@@ -574,13 +576,11 @@ private function getImplementsInterfaceNode(ImplementingType $type, NamedType $s
574576
* @param ObjectType|InterfaceType $type
575577
* @param Type&NamedType $shouldBeInterface
576578
*
577-
* @return array<int, NamedTypeNode>
579+
* @return list<NamedTypeNode>
578580
*/
579581
private function getAllImplementsInterfaceNodes(ImplementingType $type, NamedType $shouldBeInterface): array
580582
{
581-
$allNodes = $type->astNode !== null
582-
? \array_merge([$type->astNode], $type->extensionASTNodes)
583-
: $type->extensionASTNodes;
583+
$allNodes = array_filter([$type->astNode, ...$type->extensionASTNodes]);
584584

585585
$shouldBeInterfaceName = $shouldBeInterface->name;
586586
$matchingInterfaceNodes = [];
@@ -735,12 +735,10 @@ private function validateUnionMembers(UnionType $union): void
735735
}
736736
}
737737

738-
/** @return array<int, NamedTypeNode> */
738+
/** @return list<NamedTypeNode> */
739739
private function getUnionMemberTypeNodes(UnionType $union, string $typeName): array
740740
{
741-
$allNodes = $union->astNode !== null
742-
? \array_merge([$union->astNode], $union->extensionASTNodes)
743-
: $union->extensionASTNodes;
741+
$allNodes = array_filter([$union->astNode, ...$union->extensionASTNodes]);
744742

745743
$types = [];
746744
foreach ($allNodes as $node) {

src/Utils/ASTDefinitionBuilder.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ private function makeSchemaDef(Node $def): Type
339339
private function makeTypeDef(ObjectTypeDefinitionNode $def): ObjectType
340340
{
341341
$name = $def->name->value;
342-
/** @var array<int, ObjectTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
342+
/** @var array<ObjectTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
343343
$extensionASTNodes = $this->typeExtensionsMap[$name] ?? [];
344344
$allNodes = [$def, ...$extensionASTNodes];
345345

@@ -453,7 +453,7 @@ private function makeImplementedInterfaces(array $nodes): array
453453
private function makeInterfaceDef(InterfaceTypeDefinitionNode $def): InterfaceType
454454
{
455455
$name = $def->name->value;
456-
/** @var array<int, InterfaceTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
456+
/** @var array<InterfaceTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
457457
$extensionASTNodes = $this->typeExtensionsMap[$name] ?? [];
458458
$allNodes = [$def, ...$extensionASTNodes];
459459

@@ -475,7 +475,7 @@ private function makeInterfaceDef(InterfaceTypeDefinitionNode $def): InterfaceTy
475475
private function makeEnumDef(EnumTypeDefinitionNode $def): EnumType
476476
{
477477
$name = $def->name->value;
478-
/** @var array<int, EnumTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
478+
/** @var array<EnumTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
479479
$extensionASTNodes = $this->typeExtensionsMap[$name] ?? [];
480480

481481
$values = [];
@@ -502,7 +502,7 @@ private function makeEnumDef(EnumTypeDefinitionNode $def): EnumType
502502
private function makeUnionDef(UnionTypeDefinitionNode $def): UnionType
503503
{
504504
$name = $def->name->value;
505-
/** @var array<int, UnionTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
505+
/** @var array<UnionTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
506506
$extensionASTNodes = $this->typeExtensionsMap[$name] ?? [];
507507

508508
return new UnionType([
@@ -531,23 +531,23 @@ private function makeUnionDef(UnionTypeDefinitionNode $def): UnionType
531531
private function makeScalarDef(ScalarTypeDefinitionNode $def): CustomScalarType
532532
{
533533
$name = $def->name->value;
534-
/** @var array<int, ScalarTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
534+
/** @var array<ScalarTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
535535
$extensionASTNodes = $this->typeExtensionsMap[$name] ?? [];
536536

537537
return new CustomScalarType([
538538
'name' => $name,
539539
'description' => $def->description->value ?? null,
540+
'serialize' => static fn ($value) => $value,
540541
'astNode' => $def,
541542
'extensionASTNodes' => $extensionASTNodes,
542-
'serialize' => static fn ($value) => $value,
543543
]);
544544
}
545545

546546
/** @throws InvariantViolation */
547547
private function makeInputObjectDef(InputObjectTypeDefinitionNode $def): InputObjectType
548548
{
549549
$name = $def->name->value;
550-
/** @var array<int, InputObjectTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
550+
/** @var array<InputObjectTypeExtensionNode> $extensionASTNodes (proven by schema validation) */
551551
$extensionASTNodes = $this->typeExtensionsMap[$name] ?? [];
552552

553553
return new InputObjectType([

0 commit comments

Comments
 (0)