7
7
use Closure ;
8
8
use GraphQL \Error \Error ;
9
9
use GraphQL \Language \AST \DirectiveNode ;
10
+ use GraphQL \Language \AST \EnumValueDefinitionNode ;
10
11
use GraphQL \Language \AST \ScalarTypeDefinitionNode ;
11
12
use GraphQL \Language \BlockString ;
12
13
use GraphQL \Language \Printer ;
35
36
use function implode ;
36
37
use function is_callable ;
37
38
use function ksort ;
39
+ use function ltrim ;
38
40
use function mb_strlen ;
39
41
use function self ;
40
42
use function sprintf ;
54
56
*/
55
57
class SchemaPrinter
56
58
{
57
- protected const LINE_LENGTH = 80 ;
59
+ protected const LINE_LENGTH = 70 ;
58
60
59
61
/**
60
62
* @param array<string, bool> $options
@@ -235,7 +237,7 @@ protected static function printDescription(array $options, $def, string $indenta
235
237
return static ::printDescriptionWithComments ($ description , $ indentation , $ firstInBlock );
236
238
}
237
239
238
- $ preferMultipleLines = mb_strlen ($ description ) > 70 ;
240
+ $ preferMultipleLines = mb_strlen ($ description ) > static :: LINE_LENGTH ;
239
241
$ blockString = BlockString::print ($ description , '' , $ preferMultipleLines );
240
242
$ prefix = $ indentation !== '' && ! $ firstInBlock
241
243
? "\n" . $ indentation
@@ -427,10 +429,7 @@ protected static function printEnum(EnumType $type, array $options): string
427
429
$ values = $ type ->getValues ();
428
430
$ values = array_map (
429
431
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 );
434
433
},
435
434
$ values ,
436
435
array_keys ($ values )
@@ -441,6 +440,24 @@ static function (EnumValueDefinition $value, int $i) use ($options): string {
441
440
static ::printBlock ($ values );
442
441
}
443
442
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
+
444
461
/**
445
462
* @param array<string, bool> $options
446
463
* @phpstan-param Options $options
@@ -472,21 +489,26 @@ protected static function printBlock(array $items): string
472
489
}
473
490
474
491
/**
492
+ * @param Type|EnumValueDefinition $type
475
493
* @param array<string, bool> $options
476
494
* @phpstan-param Options $options
477
495
*/
478
- protected static function printTypeDirectives (Type $ type , array $ options , string $ indentation = '' ): string {
496
+ protected static function printTypeDirectives ($ type , array $ options , string $ indentation = '' ): string {
479
497
// Enabled?
480
498
$ filter = $ options ['printDirectives ' ] ?? null ;
481
499
482
500
if (!$ filter || !is_callable ($ filter )) {
501
+ if ($ type instanceof EnumValueDefinition) {
502
+ return static ::printDeprecated ($ type );
503
+ }
504
+
483
505
return '' ;
484
506
}
485
507
486
508
// AST Node available and has directives?
487
509
$ node = $ type ->astNode ;
488
510
489
- if (!($ node instanceof ScalarTypeDefinitionNode)) {
511
+ if (!($ node instanceof ScalarTypeDefinitionNode || $ node instanceof EnumValueDefinitionNode )) {
490
512
return '' ;
491
513
}
492
514
@@ -496,8 +518,17 @@ protected static function printTypeDirectives(Type $type, array $options, string
496
518
$ directives = [];
497
519
498
520
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 )) {
500
526
$ string = static ::printTypeDirective ($ directive , $ options , $ indentation );
527
+ } else {
528
+ // empty
529
+ }
530
+
531
+ if ($ string ) {
501
532
$ length = $ length + mb_strlen ($ string );
502
533
$ directives [] = $ string ;
503
534
}
0 commit comments