Skip to content

Commit dc502b5

Browse files
authored
Fix generated type for nullable custom scalars (#36)
1 parent bb3b688 commit dc502b5

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Generator/AbstractGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function dumpPHPType(Type $type, callable $importer) : string
3636
if ($type instanceof Type\NullableType) {
3737
$wrappedType = $type->getWrappedType();
3838

39-
if ($wrappedType instanceof Type\UnionType) {
39+
if ($wrappedType instanceof Type\UnionType || $wrappedType instanceof ScalarType) {
4040
return sprintf('null|%s', $this->dumpPHPType($wrappedType, $importer));
4141
}
4242

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ruudk\GraphQLCodeGenerator\Generator;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Ruudk\GraphQLCodeGenerator\Config\Config;
9+
use Ruudk\GraphQLCodeGenerator\Type\ScalarType;
10+
use Symfony\Component\TypeInfo\Type as SymfonyType;
11+
12+
final class AbstractGeneratorTest extends TestCase
13+
{
14+
public function testNullableCustomScalarUsesExplicitNullUnionInsteadOfNullableShorthand() : void
15+
{
16+
$generator = new class(Config::create(schema: '', projectDir: '', queriesDir: '', outputDir: '', namespace: 'Test\\Generated', client: 'Test\\Client')) extends AbstractGenerator {
17+
public function dump(SymfonyType $type) : string
18+
{
19+
return $this->dumpPHPType($type, static fn(string $className) => $className);
20+
}
21+
};
22+
23+
self::assertSame(
24+
'null|int|string|float|bool',
25+
$generator->dump(SymfonyType::nullable(new ScalarType())),
26+
'Nullable custom scalar output should not use ? with a multi-type union.',
27+
);
28+
}
29+
}

0 commit comments

Comments
 (0)