Skip to content

Commit bd48680

Browse files
committed
Long list passed to directive will be split to multiple lines.
1 parent af9d6c3 commit bd48680

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
use GraphQL\Language\AST\DirectiveNode;
1111
use GraphQL\Language\AST\EnumTypeDefinitionNode;
1212
use GraphQL\Language\AST\EnumValueDefinitionNode;
13+
use GraphQL\Language\AST\ListValueNode;
14+
use GraphQL\Language\AST\Node;
1315
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
16+
use GraphQL\Language\AST\ValueNode;
1417
use GraphQL\Language\BlockString;
1518
use GraphQL\Language\Printer;
1619
use GraphQL\Type\Definition\Directive;
@@ -602,7 +605,24 @@ protected static function printTypeDirective(DirectiveNode $directive, array $op
602605
*/
603606
protected static function printArgument(ArgumentNode $argument, array $options, string $indentation): string
604607
{
605-
return "{$indentation}{$argument->name->value}: " . Printer::doPrint($argument->value);
608+
$value = $argument->value;
609+
610+
if ($value instanceof ListValueNode) {
611+
$length = 0;
612+
$values = [];
613+
614+
foreach ($value->values as $item) {
615+
$string = ' ' . $indentation . Printer::doPrint($item);
616+
$length = $length + mb_strlen($string);
617+
$values[] = $string;
618+
}
619+
620+
$value = static::printLines($values, '[', ']', static::isLineTooLong($length), $indentation);
621+
} else {
622+
$value = Printer::doPrint($value);
623+
}
624+
625+
return "{$indentation}{$argument->name->value}: {$value}";
606626
}
607627

608628
/**

tests/Utils/SchemaPrinterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,6 +1281,7 @@ public function testPrintDirectives(): void {
12811281
$text = str_pad('a', 80, 'a');
12821282
$schema = /** @lang GraphQL */ <<<GRAPHQL
12831283
directive @test(
1284+
values: [String!]
12841285
value: String @test(value: "{$text}")
12851286
text: String
12861287
) on SCHEMA |
@@ -1366,10 +1367,13 @@ interface InterfaceB implements InterfaceA @test(value: "{$text}") {
13661367
"{$text}"
13671368
d: String = "123" @test(value: "{$text}", text: "{$text}")
13681369
): Boolean
1370+
e: String @test(values: ["{$text}", "{$text}", "{$text}"])
13691371
}
13701372
GRAPHQL;
13711373
$expected = /** @lang GraphQL */ <<<'GRAPHQL'
13721374
directive @test(
1375+
values: [String!]
1376+
13731377
value: String
13741378
@test(
13751379
value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
@@ -1507,6 +1511,15 @@ interface InterfaceB implements InterfaceA
15071511
text: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
15081512
)
15091513
): Boolean
1514+
1515+
e: String
1516+
@test(
1517+
values: [
1518+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1519+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1520+
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
1521+
]
1522+
)
15101523
}
15111524
15121525
scalar ScalarA @test

0 commit comments

Comments
 (0)