Skip to content

Commit f9220f3

Browse files
committed
ARGUMENT_DEFINITION support.
1 parent fc10a4d commit f9220f3

File tree

2 files changed

+94
-30
lines changed

2 files changed

+94
-30
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -268,37 +268,51 @@ protected static function printDescriptionWithComments(string $description, stri
268268
*/
269269
protected static function printArgs(array $options, array $args, string $indentation = ''): string
270270
{
271+
// Empty?
271272
if (count($args) === 0) {
272273
return '';
273274
}
274275

275-
// If every arg does not have a description, print them on one line.
276-
if (
277-
Utils::every(
278-
$args,
279-
static function ($arg): bool {
280-
return strlen($arg->description ?? '') === 0;
281-
}
282-
)
283-
) {
284-
return '(' . implode(', ', array_map('static::printInputValue', $args)) . ')';
285-
}
286-
287-
return sprintf(
288-
"(\n%s\n%s)",
289-
implode(
290-
"\n",
291-
array_map(
292-
static function (FieldArgument $arg, int $i) use ($indentation, $options): string {
293-
return static::printDescription($options, $arg, ' ' . $indentation, $i === 0) . ' ' . $indentation .
294-
static::printInputValue($arg);
295-
},
296-
$args,
297-
array_keys($args)
298-
)
299-
),
300-
$indentation
301-
);
276+
// Print arguments
277+
$length = 0;
278+
$arguments = [];
279+
$description = false;
280+
281+
foreach ($args as $i => $arg) {
282+
$value = static::printArg($arg, $options, ' ' . $indentation, $i === 0);
283+
$length = $length + mb_strlen($value);
284+
$description = $description || mb_strlen($arg->description ?? '') > 0;
285+
$arguments[] = $value;
286+
}
287+
288+
// Multiline?
289+
$serialized = '';
290+
291+
if ($length > static::LINE_LENGTH || $description) {
292+
$serialized = "(\n" . implode("\n", $arguments) . "\n{$indentation})";
293+
} else {
294+
$serialized = '(' . implode(', ', array_map('trim', $arguments)) . ')';
295+
}
296+
297+
return $serialized;
298+
}
299+
300+
/**
301+
* @param array<string, bool> $options
302+
* @phpstan-param Options $options
303+
*/
304+
protected static function printArg(FieldArgument $type, array $options, string $indentation = '', bool $firstInBlock = true): string
305+
{
306+
$field = static::printDescription($options, $type, $indentation, $firstInBlock) .
307+
$indentation .
308+
static::printInputValue($type) .
309+
static::printTypeDirectives($type, $options, $indentation);
310+
311+
if (!$firstInBlock && mb_strlen($field) > static::LINE_LENGTH) {
312+
$field = "\n".ltrim($field, "\n");
313+
}
314+
315+
return $field;
302316
}
303317

304318
/**
@@ -536,7 +550,7 @@ protected static function printBlock(array $items): string
536550
}
537551

538552
/**
539-
* @param Type|EnumValueDefinition|EnumType|InterfaceType|FieldDefinition|UnionType|InputObjectType|InputObjectField $type
553+
* @param Type|EnumValueDefinition|EnumType|InterfaceType|FieldDefinition|UnionType|InputObjectType|InputObjectField|FieldArgument $type
540554
* @param array<string, bool> $options
541555
* @phpstan-param Options $options
542556
*/

tests/Utils/SchemaPrinterTest.php

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,7 @@ public function testPrintDirectives(): void {
12811281
$text = str_pad('a', 80, 'a');
12821282
$schema = /** @lang GraphQL */ <<<GRAPHQL
12831283
directive @test(
1284-
value: String
1284+
value: String @test(value: "{$text}")
12851285
) on SCHEMA |
12861286
SCALAR |
12871287
OBJECT |
@@ -1352,9 +1352,26 @@ interface InterfaceB implements InterfaceA @test(value: "{$text}") {
13521352
input InputB @test(value: "{$text}") {
13531353
a: ID
13541354
}
1355+
1356+
type Query {
1357+
a(a: String, b: String, c: String): Boolean @test
1358+
b("desc" a: String): Boolean @test(value: "{$text}")
1359+
c(a: String @test(value: "{$text}")): Boolean
1360+
d(
1361+
a: String @test
1362+
b: String @test(value: "{$text}")
1363+
"{$text}"
1364+
c: String @test
1365+
"{$text}"
1366+
d: String = "123" @test(value: "{$text}")
1367+
): Boolean
1368+
}
13551369
GRAPHQL;
13561370
$expected = /** @lang GraphQL */ <<<'GRAPHQL'
1357-
directive @test(value: String) on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
1371+
directive @test(
1372+
value: String
1373+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1374+
) on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
13581375
13591376
enum EnumA @test {
13601377
a @test @deprecated
@@ -1427,6 +1444,39 @@ interface InterfaceB implements InterfaceA
14271444
a: ID
14281445
}
14291446
1447+
type Query {
1448+
a(a: String, b: String, c: String): Boolean @test
1449+
1450+
b(
1451+
"""desc"""
1452+
a: String
1453+
): Boolean
1454+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1455+
1456+
c(
1457+
a: String
1458+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1459+
): Boolean
1460+
1461+
d(
1462+
a: String @test
1463+
1464+
b: String
1465+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1466+
1467+
"""
1468+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1469+
"""
1470+
c: String @test
1471+
1472+
"""
1473+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1474+
"""
1475+
d: String = "123"
1476+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1477+
): Boolean
1478+
}
1479+
14301480
scalar ScalarA @test
14311481
14321482
scalar ScalarB

0 commit comments

Comments
 (0)