Skip to content

Commit 7327a77

Browse files
committed
UNION support.
1 parent a1898dc commit 7327a77

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/Utils/SchemaPrinter.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,17 @@ protected static function printUnion(UnionType $type, array $options): string
434434
$types = count($types) > 0
435435
? ' = ' . implode(' | ', $types)
436436
: '';
437+
$directives = static::printTypeDirectives($type, $options, '');
437438

438-
return static::printDescription($options, $type) . 'union ' . $type->name . $types;
439+
if (mb_strlen($directives) > static::LINE_LENGTH) {
440+
$types = ltrim($types);
441+
$directives .= "\n";
442+
}
443+
444+
return static::printDescription($options, $type) .
445+
'union ' . $type->name .
446+
$directives .
447+
$types;
439448
}
440449

441450
/**
@@ -508,7 +517,7 @@ protected static function printBlock(array $items): string
508517
}
509518

510519
/**
511-
* @param Type|EnumValueDefinition|EnumType|InterfaceType|FieldDefinition $type
520+
* @param Type|EnumValueDefinition|EnumType|InterfaceType|FieldDefinition|UnionType $type
512521
* @param array<string, bool> $options
513522
* @phpstan-param Options $options
514523
*/

tests/Utils/SchemaPrinterTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,10 @@ interface InterfaceB implements InterfaceA @test(value: "{$text}") {
13351335
type TypeB implements InterfaceB @test(value: "{$text}") {
13361336
a: ID
13371337
}
1338+
1339+
union UnionA @test = TypeA | TypeB
1340+
1341+
union UnionB @test(value: "{$text}") = TypeA | TypeB
13381342
GRAPHQL;
13391343
$expected = /** @lang GraphQL */ <<<'GRAPHQL'
13401344
directive @test(value: String) on SCHEMA | SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
@@ -1416,6 +1420,12 @@ interface InterfaceB implements InterfaceA
14161420
a: ID
14171421
}
14181422
1423+
union UnionA @test = TypeA | TypeB
1424+
1425+
union UnionB
1426+
@test(value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
1427+
= TypeA | TypeB
1428+
14191429
GRAPHQL;
14201430
$actual = SchemaPrinter::doPrint(BuildSchema::build($schema), [
14211431
'printDirectives' => static function(): bool {

0 commit comments

Comments
 (0)