|
15 | 15 | use GraphQL\Language\AST\ScalarTypeDefinitionNode;
|
16 | 16 | use GraphQL\Language\AST\ValueNode;
|
17 | 17 | use GraphQL\Language\BlockString;
|
| 18 | +use GraphQL\Language\Parser; |
18 | 19 | use GraphQL\Language\Printer;
|
19 | 20 | use GraphQL\Type\Definition\Directive;
|
20 | 21 | use GraphQL\Type\Definition\EnumType;
|
|
42 | 43 | use function implode;
|
43 | 44 | use function is_callable;
|
44 | 45 | use function is_string;
|
| 46 | +use function json_encode; |
45 | 47 | use function ksort;
|
46 | 48 | use function ltrim;
|
47 | 49 | use function mb_strlen;
|
@@ -383,12 +385,12 @@ protected static function printField(FieldDefinition $type, array $options, stri
|
383 | 385 | */
|
384 | 386 | protected static function printDeprecated($fieldOrEnumVal): string
|
385 | 387 | {
|
386 |
| - $reason = $fieldOrEnumVal->deprecationReason; |
| 388 | + $reason = static::getDeprecatedReason($fieldOrEnumVal); |
387 | 389 | if ($reason === null) {
|
388 | 390 | return '';
|
389 | 391 | }
|
390 | 392 |
|
391 |
| - if ($reason === '' || $reason === Directive::DEFAULT_DEPRECATION_REASON) { |
| 393 | + if ($reason === '') { |
392 | 394 | return ' @deprecated';
|
393 | 395 | }
|
394 | 396 |
|
@@ -529,41 +531,47 @@ protected static function printBlock(array $items): string
|
529 | 531 | protected static function printTypeDirectives($type, array $options, string $indentation = ''): string {
|
530 | 532 | // Enabled?
|
531 | 533 | $filter = $options['printDirectives'] ?? null;
|
| 534 | + $deprecatable = $type instanceof EnumValueDefinition || $type instanceof FieldDefinition; |
532 | 535 |
|
533 | 536 | if (!is_callable($filter)) {
|
534 |
| - if ($type instanceof EnumValueDefinition || $type instanceof FieldDefinition) { |
| 537 | + if ($deprecatable) { |
535 | 538 | return static::printDeprecated($type);
|
536 | 539 | }
|
537 | 540 |
|
538 | 541 | return '';
|
539 | 542 | }
|
540 | 543 |
|
541 |
| - // AST Node available and has directives? |
| 544 | + // Collect directives |
542 | 545 | $node = $type->astNode;
|
| 546 | + $nodeDirectives = []; |
| 547 | + |
| 548 | + if ($node !== null) { |
| 549 | + $nodeDirectives = $node->directives; |
| 550 | + } elseif ($deprecatable && $type->deprecationReason !== null) { |
| 551 | + // TODO Is there a better way to create directive node? |
| 552 | + $name = Directive::DEPRECATED_NAME; |
| 553 | + $reason = json_encode(static::getDeprecatedReason($type)); |
| 554 | + $nodeDirectives[] = Parser::directive("@{$name}(reason: {$reason})"); |
| 555 | + } else { |
| 556 | + // empty |
| 557 | + } |
543 | 558 |
|
544 |
| - if ($node === null) { |
| 559 | + if (count($nodeDirectives) === 0) { |
545 | 560 | return '';
|
546 | 561 | }
|
547 | 562 |
|
548 | 563 | // Print
|
549 | 564 | $length = 0;
|
550 | 565 | $directives = [];
|
551 | 566 |
|
552 |
| - foreach ($node->directives as $directive) { |
553 |
| - $string = null; |
554 |
| - |
555 |
| - if ($directive->name === Directive::DEPRECATED_NAME && ($type instanceof EnumValueDefinition)) { |
556 |
| - $string = static::printDeprecated($type); |
557 |
| - } elseif ($filter($directive)) { |
558 |
| - $string = static::printTypeDirective($directive, $options, $indentation); |
559 |
| - } else { |
560 |
| - // empty |
| 567 | + foreach ($nodeDirectives as $nodeDirective) { |
| 568 | + if (!$filter($nodeDirective)) { |
| 569 | + continue; |
561 | 570 | }
|
562 | 571 |
|
563 |
| - if ($string !== null) { |
564 |
| - $length = $length + mb_strlen($string); |
565 |
| - $directives[] = $string; |
566 |
| - } |
| 572 | + $directive = static::printTypeDirective($nodeDirective, $options, $indentation); |
| 573 | + $length = $length + mb_strlen($directive); |
| 574 | + $directives[] = $directive; |
567 | 575 | }
|
568 | 576 |
|
569 | 577 | // Multiline?
|
@@ -637,7 +645,7 @@ protected static function printLines(array $lines, string $begin, string $end, b
|
637 | 645 | $wrapped = false;
|
638 | 646 |
|
639 | 647 | for ($i = 0, $c = count($lines); $i < $c; $i++) {
|
640 |
| - // If line too long and contains LF we wrap it by empty lines |
| 648 | + // If line contains LF we wrap it by empty lines |
641 | 649 | $line = trim($lines[$i], "\n");
|
642 | 650 | $wrap = mb_strpos($line, "\n") !== false;
|
643 | 651 |
|
@@ -670,4 +678,18 @@ protected static function printLines(array $lines, string $begin, string $end, b
|
670 | 678 | protected static function isLineTooLong($string): bool {
|
671 | 679 | return (is_string($string) ? mb_strlen($string) : $string) > static::LINE_LENGTH;
|
672 | 680 | }
|
| 681 | + |
| 682 | + /** |
| 683 | + * @param FieldDefinition|EnumValueDefinition $fieldOrEnumVal |
| 684 | + */ |
| 685 | + protected static function getDeprecatedReason($fieldOrEnumVal): ?string |
| 686 | + { |
| 687 | + $reason = $fieldOrEnumVal->deprecationReason; |
| 688 | + |
| 689 | + if ($reason === '' || $reason === Directive::DEFAULT_DEPRECATION_REASON) { |
| 690 | + $reason = ''; |
| 691 | + } |
| 692 | + |
| 693 | + return $reason; |
| 694 | + } |
673 | 695 | }
|
0 commit comments