Skip to content

Commit 3fd4717

Browse files
authored
Fix handling of NonNull type (#6)
* fix non-null case * add test * cleanup * add type strength
1 parent 907d814 commit 3fd4717

File tree

2 files changed

+121
-6
lines changed

2 files changed

+121
-6
lines changed

src/Type/Definition/UserErrorsType.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ public function __construct(array $config, array $path, bool $isParentList = fal
3232

3333
$this->_addErrorCodes($config, $finalFields, $path);
3434

35-
$type = $config['type'];
35+
$type = $this->_getType($config);
3636
if ($type instanceof InputObjectType) {
37-
$this->_buildInputObjectType($config, $path, $finalFields);
37+
$this->_buildInputObjectType($type, $config, $path, $finalFields);
3838
} elseif ($type instanceof ListOfType) {
39-
$this->_buildListOfType($config, $path, $finalFields);
39+
$this->_buildListOfType($type, $config, $path, $finalFields);
4040
}
4141

4242
if ($isParentList) {
@@ -50,8 +50,15 @@ public function __construct(array $config, array $path, bool $isParentList = fal
5050
]);
5151
}
5252

53-
protected function _buildListOfType($config, $path, &$finalFields) {
53+
protected function _getType($config): Type {
5454
$type = $config['type'];
55+
if($type instanceof NonNull) {
56+
$type = $type->getWrappedType();
57+
}
58+
return $type;
59+
}
60+
61+
protected function _buildListOfType(ListOfType $type, $config, $path, &$finalFields) {
5562
$wrappedType = $type->getWrappedType(true);
5663
if (isset($config['validateItem'])) {
5764
$newType = static::create(
@@ -77,9 +84,8 @@ protected function _buildListOfType($config, $path, &$finalFields) {
7784
}
7885
}
7986

80-
protected function _buildInputObjectType($config, $path, &$finalFields) {
87+
protected function _buildInputObjectType(InputObjectType $type, $config, $path, &$finalFields) {
8188
$fields = [];
82-
$type = $config['type'];
8389
foreach ($type->getFields() as $key => $field) {
8490
$newType = static::create(
8591
$field->config + ['typeSetter' => $config['typeSetter'] ?? null],
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GraphQL\Tests\Type\UserErrorsType;
6+
7+
use GraphQL\Tests\Utils;
8+
use GraphQL\Type\Definition\InputObjectType;
9+
use GraphQL\Type\Definition\Type;
10+
use GraphQL\Type\Definition\UserErrorsType;
11+
use GraphQL\Type\Schema;
12+
use GraphQL\Utils\SchemaPrinter;
13+
use PHPUnit\Framework\TestCase;
14+
15+
final class NonNullTest extends TestCase
16+
{
17+
public function testStringWrappedType(): void
18+
{
19+
$type = new UserErrorsType([
20+
'errorCodes' => ['invalidColor'],
21+
'validate' => static function ($value) {
22+
return $value ? 0 : 1;
23+
},
24+
'type' => Type::nonNull(Type::string()),
25+
], ['authorId']);
26+
27+
self::assertEquals(Utils::nowdoc('
28+
schema {
29+
query: AuthorIdError
30+
}
31+
32+
"""User errors for AuthorId"""
33+
type AuthorIdError {
34+
"""An error code"""
35+
code: AuthorIdErrorCode
36+
37+
"""A natural language description of the issue"""
38+
msg: String
39+
}
40+
41+
"""Error code"""
42+
enum AuthorIdErrorCode {
43+
invalidColor
44+
}
45+
46+
'), SchemaPrinter::doPrint(new Schema(['query' => $type])));
47+
}
48+
49+
public function testInputObjectWrappedType(): void
50+
{
51+
$type = new UserErrorsType([
52+
'errorCodes' => ['invalidColor'],
53+
'validate' => static function ($value) {
54+
return $value ? 0 : 1;
55+
},
56+
'type' => Type::nonNull(new InputObjectType([
57+
'name' => 'bookInput',
58+
'fields' => [
59+
'authorId' => [
60+
'type' => Type::id(),
61+
'description' => 'An author Id',
62+
'validate' => static function($authorId) {
63+
return !empty($authorId);
64+
}
65+
],
66+
],
67+
])),
68+
], ['author']);
69+
70+
self::assertEquals(Utils::nowdoc('
71+
schema {
72+
query: AuthorError
73+
}
74+
75+
"""User errors for Author"""
76+
type AuthorError {
77+
"""An error code"""
78+
code: AuthorErrorCode
79+
80+
"""A natural language description of the issue"""
81+
msg: String
82+
83+
"""Suberrors for Author"""
84+
suberrors: Author_Suberrors
85+
}
86+
87+
"""Error code"""
88+
enum AuthorErrorCode {
89+
invalidColor
90+
}
91+
92+
"""User errors for AuthorId"""
93+
type Author_AuthorIdError {
94+
"""A numeric error code. 0 on success, non-zero on failure."""
95+
code: Int
96+
97+
"""An error message."""
98+
msg: String
99+
}
100+
101+
"""User Error"""
102+
type Author_Suberrors {
103+
"""Error for authorId"""
104+
authorId: Author_AuthorIdError
105+
}
106+
107+
'), SchemaPrinter::doPrint(new Schema(['query' => $type])));
108+
}
109+
}

0 commit comments

Comments
 (0)