|
5 | 5 | namespace GraphQL\Utils;
|
6 | 6 |
|
7 | 7 | use GraphQL\Error\Error;
|
| 8 | +use GraphQL\Language\AST\ArgumentNode; |
| 9 | +use GraphQL\Language\AST\DirectiveNode; |
| 10 | +use GraphQL\Language\AST\NodeKind; |
8 | 11 | use GraphQL\Language\Printer;
|
9 | 12 | use GraphQL\Type\Definition\Directive;
|
10 | 13 | use GraphQL\Type\Definition\EnumType;
|
|
29 | 32 | use function count;
|
30 | 33 | use function explode;
|
31 | 34 | use function implode;
|
| 35 | +use function iterator_to_array; |
32 | 36 | use function ksort;
|
33 | 37 | use function mb_strlen;
|
34 | 38 | use function preg_match_all;
|
@@ -371,7 +375,7 @@ public static function printType(Type $type, array $options = []): string
|
371 | 375 | */
|
372 | 376 | protected static function printScalar(ScalarType $type, array $options): string
|
373 | 377 | {
|
374 |
| - return sprintf('%sscalar %s', static::printDescription($options, $type), $type->name); |
| 378 | + return sprintf('%sscalar %s', static::printDescription($options, $type), $type->name) . static::printFieldOrTypeDirectives($type); |
375 | 379 | }
|
376 | 380 |
|
377 | 381 | /**
|
@@ -410,14 +414,64 @@ protected static function printFields(array $options, $type): string
|
410 | 414 | static function (FieldDefinition $f, int $i) use ($options): string {
|
411 | 415 | return static::printDescription($options, $f, ' ', $i === 0) . ' ' .
|
412 | 416 | $f->name . static::printArgs($options, $f->args, ' ') . ': ' .
|
413 |
| - (string) $f->getType() . static::printDeprecated($f); |
| 417 | + (string) $f->getType() . static::printFieldOrTypeDirectives($f); |
414 | 418 | },
|
415 | 419 | $fields,
|
416 | 420 | array_keys($fields)
|
417 | 421 | )
|
418 | 422 | );
|
419 | 423 | }
|
420 | 424 |
|
| 425 | + /** |
| 426 | + * @param FieldDefinition|ScalarType|EnumValueDefinition $fieldOrEnumVal |
| 427 | + */ |
| 428 | + protected static function printFieldOrTypeDirectives($fieldOrEnumVal): string |
| 429 | + { |
| 430 | + $serialized = ''; |
| 431 | + |
| 432 | + if (($fieldOrEnumVal instanceof FieldDefinition || $fieldOrEnumVal instanceof EnumValueDefinition) && $fieldOrEnumVal->deprecationReason !== null) { |
| 433 | + $serialized .= static::printDeprecated($fieldOrEnumVal); |
| 434 | + } |
| 435 | + |
| 436 | + if ($fieldOrEnumVal->astNode !== null) { |
| 437 | + foreach ($fieldOrEnumVal->astNode->directives as $directive) { |
| 438 | + /** @var DirectiveNode $directive */ |
| 439 | + if ($directive->name->value === Directive::DEPRECATED_NAME && $fieldOrEnumVal->deprecationReason !== null) { |
| 440 | + continue; |
| 441 | + } |
| 442 | + |
| 443 | + $serialized .= ' @' . $directive->name->value; |
| 444 | + |
| 445 | + if ($directive->arguments->count() === 0) { |
| 446 | + continue; |
| 447 | + } |
| 448 | + |
| 449 | + $serialized .= '(' . implode(',', array_map(static function (ArgumentNode $argument): string { |
| 450 | + switch ($argument->value->kind) { |
| 451 | + case NodeKind::INT: |
| 452 | + $type = Type::int(); |
| 453 | + break; |
| 454 | + case NodeKind::FLOAT: |
| 455 | + $type = Type::float(); |
| 456 | + break; |
| 457 | + case NodeKind::STRING: |
| 458 | + $type = Type::string(); |
| 459 | + break; |
| 460 | + case NodeKind::BOOLEAN: |
| 461 | + $type = Type::boolean(); |
| 462 | + break; |
| 463 | + default: |
| 464 | + return ''; |
| 465 | + } |
| 466 | + |
| 467 | + return $argument->name->value . ': ' . Printer::doPrint(AST::astFromValue($argument->value->value, $type)); |
| 468 | + }, iterator_to_array($directive->arguments))) . ')'; |
| 469 | + } |
| 470 | + } |
| 471 | + |
| 472 | + return $serialized; |
| 473 | + } |
| 474 | + |
421 | 475 | /**
|
422 | 476 | * @param FieldArgument|EnumValueDefinition $fieldOrEnumVal
|
423 | 477 | */
|
@@ -487,7 +541,7 @@ protected static function printEnumValues(array $values, array $options): string
|
487 | 541 | array_map(
|
488 | 542 | static function (EnumValueDefinition $value, int $i) use ($options): string {
|
489 | 543 | return static::printDescription($options, $value, ' ', $i === 0) . ' ' .
|
490 |
| - $value->name . static::printDeprecated($value); |
| 544 | + $value->name . static::printFieldOrTypeDirectives($value); |
491 | 545 | },
|
492 | 546 | $values,
|
493 | 547 | array_keys($values)
|
|
0 commit comments