Skip to content

Commit 09c162d

Browse files
committed
ENUM_VALUE support.
1 parent 5f1b2b3 commit 09c162d

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 40 additions & 9 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\EnumValueDefinitionNode;
1011
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
1112
use GraphQL\Language\BlockString;
1213
use GraphQL\Language\Printer;
@@ -35,6 +36,7 @@
3536
use function implode;
3637
use function is_callable;
3738
use function ksort;
39+
use function ltrim;
3840
use function mb_strlen;
3941
use function self;
4042
use function sprintf;
@@ -54,7 +56,7 @@
5456
*/
5557
class SchemaPrinter
5658
{
57-
protected const LINE_LENGTH = 80;
59+
protected const LINE_LENGTH = 70;
5860

5961
/**
6062
* @param array<string, bool> $options
@@ -235,7 +237,7 @@ protected static function printDescription(array $options, $def, string $indenta
235237
return static::printDescriptionWithComments($description, $indentation, $firstInBlock);
236238
}
237239

238-
$preferMultipleLines = mb_strlen($description) > 70;
240+
$preferMultipleLines = mb_strlen($description) > static::LINE_LENGTH;
239241
$blockString = BlockString::print($description, '', $preferMultipleLines);
240242
$prefix = $indentation !== '' && ! $firstInBlock
241243
? "\n" . $indentation
@@ -427,10 +429,7 @@ protected static function printEnum(EnumType $type, array $options): string
427429
$values = $type->getValues();
428430
$values = array_map(
429431
static function (EnumValueDefinition $value, int $i) use ($options): string {
430-
return static::printDescription($options, $value, ' ', $i === 0) .
431-
' ' .
432-
$value->name .
433-
static::printDeprecated($value);
432+
return static::printEnumValue($value, $options, ' ', $i === 0);
434433
},
435434
$values,
436435
array_keys($values)
@@ -441,6 +440,24 @@ static function (EnumValueDefinition $value, int $i) use ($options): string {
441440
static::printBlock($values);
442441
}
443442

443+
/**
444+
* @param array<string, bool> $options
445+
* @phpstan-param Options $options
446+
*/
447+
protected static function printEnumValue(EnumValueDefinition $type, array $options, string $indentation = '', bool $firstInBlock = false): string
448+
{
449+
$value = static::printDescription($options, $type, $indentation, $firstInBlock) .
450+
' ' .
451+
$type->name .
452+
static::printTypeDirectives($type, $options, $indentation);
453+
454+
if (!$firstInBlock && mb_strlen($value) > static::LINE_LENGTH) {
455+
$value = "\n".ltrim($value, "\n");
456+
}
457+
458+
return $value;
459+
}
460+
444461
/**
445462
* @param array<string, bool> $options
446463
* @phpstan-param Options $options
@@ -472,21 +489,26 @@ protected static function printBlock(array $items): string
472489
}
473490

474491
/**
492+
* @param Type|EnumValueDefinition $type
475493
* @param array<string, bool> $options
476494
* @phpstan-param Options $options
477495
*/
478-
protected static function printTypeDirectives(Type $type, array $options, string $indentation = ''): string {
496+
protected static function printTypeDirectives($type, array $options, string $indentation = ''): string {
479497
// Enabled?
480498
$filter = $options['printDirectives'] ?? null;
481499

482500
if (!$filter || !is_callable($filter)) {
501+
if ($type instanceof EnumValueDefinition) {
502+
return static::printDeprecated($type);
503+
}
504+
483505
return '';
484506
}
485507

486508
// AST Node available and has directives?
487509
$node = $type->astNode;
488510

489-
if (!($node instanceof ScalarTypeDefinitionNode)) {
511+
if (!($node instanceof ScalarTypeDefinitionNode || $node instanceof EnumValueDefinitionNode)) {
490512
return '';
491513
}
492514

@@ -496,8 +518,17 @@ protected static function printTypeDirectives(Type $type, array $options, string
496518
$directives = [];
497519

498520
foreach ($node->directives as $directive) {
499-
if ($filter($directive)) {
521+
$string = null;
522+
523+
if ($directive->name === Directive::DEPRECATED_NAME && ($type instanceof EnumValueDefinition)) {
524+
$string = static::printDeprecated($type);
525+
} elseif ($filter($directive)) {
500526
$string = static::printTypeDirective($directive, $options, $indentation);
527+
} else {
528+
// empty
529+
}
530+
531+
if ($string) {
501532
$length = $length + mb_strlen($string);
502533
$directives[] = $string;
503534
}

tests/Utils/SchemaPrinterTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,10 +1296,37 @@ public function testPrintDirectives(): void {
12961296
12971297
scalar ScalarA @test
12981298
scalar ScalarB @test(value: "{$text}")
1299+
1300+
enum EnumA {
1301+
a @test @deprecated
1302+
b @test(value: "{$text}")
1303+
"{$text}"
1304+
c @test
1305+
"{$text}"
1306+
d @test(value: "{$text}")
1307+
}
12991308
GRAPHQL;
13001309
$expected = <<<'GRAPHQL'
13011310
directive @test(value: String) on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
13021311
1312+
enum EnumA {
1313+
a @test @deprecated
1314+
1315+
b
1316+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1317+
1318+
"""
1319+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1320+
"""
1321+
c @test
1322+
1323+
"""
1324+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1325+
"""
1326+
d
1327+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1328+
}
1329+
13031330
scalar ScalarA @test
13041331
13051332
scalar ScalarB

0 commit comments

Comments
 (0)