Skip to content

Commit 51e8126

Browse files
committed
ENUM support.
1 parent 09c162d commit 51e8126

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Closure;
88
use GraphQL\Error\Error;
99
use GraphQL\Language\AST\DirectiveNode;
10+
use GraphQL\Language\AST\EnumTypeDefinitionNode;
1011
use GraphQL\Language\AST\EnumValueDefinitionNode;
1112
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
1213
use GraphQL\Language\BlockString;
@@ -437,6 +438,7 @@ static function (EnumValueDefinition $value, int $i) use ($options): string {
437438

438439
return static::printDescription($options, $type) .
439440
sprintf('enum %s', $type->name) .
441+
static::printTypeDirectives($type, $options) .
440442
static::printBlock($values);
441443
}
442444

@@ -489,7 +491,7 @@ protected static function printBlock(array $items): string
489491
}
490492

491493
/**
492-
* @param Type|EnumValueDefinition $type
494+
* @param Type|EnumValueDefinition|EnumType $type
493495
* @param array<string, bool> $options
494496
* @phpstan-param Options $options
495497
*/
@@ -508,7 +510,7 @@ protected static function printTypeDirectives($type, array $options, string $ind
508510
// AST Node available and has directives?
509511
$node = $type->astNode;
510512

511-
if (!($node instanceof ScalarTypeDefinitionNode || $node instanceof EnumValueDefinitionNode)) {
513+
if (!($node instanceof ScalarTypeDefinitionNode || $node instanceof EnumValueDefinitionNode || $node instanceof EnumTypeDefinitionNode)) {
512514
return '';
513515
}
514516

@@ -535,8 +537,12 @@ protected static function printTypeDirectives($type, array $options, string $ind
535537
}
536538

537539
// Multiline?
538-
$delimiter = $length > static::LINE_LENGTH ? "\n{$indentation}" : ' ';
539-
$serialized = $delimiter.implode($delimiter, $directives);
540+
$serialized = '';
541+
542+
if ($directives) {
543+
$delimiter = $length > static::LINE_LENGTH ? "\n{$indentation}" : ' ';
544+
$serialized = $delimiter.implode($delimiter, $directives);
545+
}
540546

541547
// Return
542548
return $serialized;

tests/Utils/SchemaPrinterTest.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ enum __TypeKind {
12781278
}
12791279

12801280
public function testPrintDirectives(): void {
1281-
$text = str_pad('a', 80, 'a');
1281+
$text = str_pad('a', 80, 'a');
12821282
$schema = /** @lang GraphQL */ <<<GRAPHQL
12831283
directive @test(
12841284
value: String
@@ -1297,19 +1297,23 @@ public function testPrintDirectives(): void {
12971297
scalar ScalarA @test
12981298
scalar ScalarB @test(value: "{$text}")
12991299
1300-
enum EnumA {
1300+
enum EnumA @test {
13011301
a @test @deprecated
13021302
b @test(value: "{$text}")
13031303
"{$text}"
13041304
c @test
13051305
"{$text}"
1306-
d @test(value: "{$text}")
1306+
d @test(value: "{$text}") @deprecated
1307+
}
1308+
1309+
enum EnumB @test(value: "{$text}") {
1310+
a
13071311
}
13081312
GRAPHQL;
1309-
$expected = <<<'GRAPHQL'
1313+
$expected = /** @lang GraphQL */ <<<'GRAPHQL'
13101314
directive @test(value: String) on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
13111315
1312-
enum EnumA {
1316+
enum EnumA @test {
13131317
a @test @deprecated
13141318
13151319
b
@@ -1325,6 +1329,12 @@ enum EnumA {
13251329
"""
13261330
d
13271331
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1332+
@deprecated
1333+
}
1334+
1335+
enum EnumB
1336+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") {
1337+
a
13281338
}
13291339
13301340
scalar ScalarA @test

0 commit comments

Comments
 (0)