Skip to content

Commit d103a3d

Browse files
authored
Early return when passing NonNull to Type::nonNull (#1757)
This improves developer experience. If the intent was non null, and it already is non null, just return it.
1 parent a4e9d26 commit d103a3d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

docs/class-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ static function listOf($type): GraphQL\Type\Definition\ListOfType
246246
/**
247247
* Wraps the given type in a non-null type.
248248
*
249-
* @param (NullableType&Type)|callable():(NullableType&Type) $type
249+
* @param NonNull|(NullableType&Type)|callable():(NullableType&Type) $type
250250
*
251251
* @api
252252
*/

src/Type/Definition/Type.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,16 @@ public static function listOf($type): ListOfType
105105
/**
106106
* Wraps the given type in a non-null type.
107107
*
108-
* @param (NullableType&Type)|callable():(NullableType&Type) $type
108+
* @param NonNull|(NullableType&Type)|callable():(NullableType&Type) $type
109109
*
110110
* @api
111111
*/
112112
public static function nonNull($type): NonNull
113113
{
114+
if ($type instanceof NonNull) {
115+
return $type;
116+
}
117+
114118
return new NonNull($type);
115119
}
116120

tests/Type/Definition/TypeTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace GraphQL\Tests\Type\Definition;
4+
5+
use GraphQL\Type\Definition\Type;
6+
use PHPUnit\Framework\TestCase;
7+
8+
final class TypeTest extends TestCase
9+
{
10+
public function testWrappingNonNullableTypeWithNonNull(): void
11+
{
12+
$nonNullableString = Type::nonNull(Type::string());
13+
14+
self::assertSame($nonNullableString, Type::nonNull($nonNullableString));
15+
}
16+
}

0 commit comments

Comments
 (0)