Skip to content

Commit 3cd4367

Browse files
committed
FIELD_DEFINITION support.
1 parent d60dea8 commit 3cd4367

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,7 @@ protected static function printFields(array $options, $type): string
347347
$fields = array_values($type->getFields());
348348
$fields = array_map(
349349
static function (FieldDefinition $f, int $i) use ($options): string {
350-
return static::printDescription($options, $f, ' ', $i === 0) .
351-
' ' .
352-
$f->name .
353-
static::printArgs($options, $f->args, ' ') .
354-
': ' .
355-
(string) $f->getType() .
356-
static::printDeprecated($f);
350+
return static::printField($f, $options, ' ', $i === 0);
357351
},
358352
$fields,
359353
array_keys($fields)
@@ -362,6 +356,27 @@ static function (FieldDefinition $f, int $i) use ($options): string {
362356
return self::printBlock($fields);
363357
}
364358

359+
/**
360+
* @param array<string, bool> $options
361+
* @phpstan-param Options $options
362+
*/
363+
protected static function printField(FieldDefinition $type, array $options, string $indentation = '', bool $firstInBlock = true): string
364+
{
365+
$field = static::printDescription($options, $type, $indentation, $firstInBlock) .
366+
' ' .
367+
$type->name .
368+
static::printArgs($options, $type->args, ' ') .
369+
': ' .
370+
(string) $type->getType() .
371+
static::printTypeDirectives($type, $options, $indentation);
372+
373+
if (!$firstInBlock && mb_strlen($field) > static::LINE_LENGTH) {
374+
$field = "\n".ltrim($field, "\n");
375+
}
376+
377+
return $field;
378+
}
379+
365380
/**
366381
* @param FieldDefinition|EnumValueDefinition $fieldOrEnumVal
367382
*/
@@ -492,7 +507,7 @@ protected static function printBlock(array $items): string
492507
}
493508

494509
/**
495-
* @param Type|EnumValueDefinition|EnumType|InterfaceType $type
510+
* @param Type|EnumValueDefinition|EnumType|InterfaceType|FieldDefinition $type
496511
* @param array<string, bool> $options
497512
* @phpstan-param Options $options
498513
*/
@@ -501,7 +516,7 @@ protected static function printTypeDirectives($type, array $options, string $ind
501516
$filter = $options['printDirectives'] ?? null;
502517

503518
if (!$filter || !is_callable($filter)) {
504-
if ($type instanceof EnumValueDefinition) {
519+
if ($type instanceof EnumValueDefinition || $type instanceof FieldDefinition) {
505520
return static::printDeprecated($type);
506521
}
507522

tests/Utils/SchemaPrinterTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,12 +1311,30 @@ enum EnumB @test(value: "{$text}") {
13111311
}
13121312
13131313
interface InterfaceA @test {
1314-
a: ID
1314+
a: Int @test @deprecated
1315+
b: Int @test(value: "{$text}")
1316+
"{$text}"
1317+
c: Int @test
1318+
"{$text}"
1319+
d: Int @test(value: "{$text}") @deprecated
13151320
}
13161321
13171322
interface InterfaceB @test(value: "{$text}") {
13181323
a: ID
13191324
}
1325+
1326+
type TypeA {
1327+
a: Int @test @deprecated
1328+
b: Int @test(value: "{$text}")
1329+
"{$text}"
1330+
c: Int @test
1331+
"{$text}"
1332+
d: Int @test(value: "{$text}") @deprecated
1333+
}
1334+
1335+
type TypeB {
1336+
a: ID
1337+
}
13201338
GRAPHQL;
13211339
$expected = /** @lang GraphQL */ <<<'GRAPHQL'
13221340
directive @test(value: String) on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
@@ -1346,7 +1364,22 @@ enum EnumB
13461364
}
13471365
13481366
interface InterfaceA @test {
1349-
a: ID
1367+
a: Int @test @deprecated
1368+
1369+
b: Int
1370+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1371+
1372+
"""
1373+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1374+
"""
1375+
c: Int @test
1376+
1377+
"""
1378+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1379+
"""
1380+
d: Int
1381+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1382+
@deprecated
13501383
}
13511384
13521385
interface InterfaceB
@@ -1359,6 +1392,29 @@ interface InterfaceB
13591392
scalar ScalarB
13601393
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13611394
1395+
type TypeA {
1396+
a: Int @test @deprecated
1397+
1398+
b: Int
1399+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1400+
1401+
"""
1402+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1403+
"""
1404+
c: Int @test
1405+
1406+
"""
1407+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1408+
"""
1409+
d: Int
1410+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1411+
@deprecated
1412+
}
1413+
1414+
type TypeB {
1415+
a: ID
1416+
}
1417+
13621418
GRAPHQL;
13631419
$actual = SchemaPrinter::doPrint(BuildSchema::build($schema), [
13641420
'printDirectives' => static function(): bool {

0 commit comments

Comments
 (0)