|
| 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