1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Tests\Type\ValidatedFieldDefinition; |
6
|
|
|
|
7
|
|
|
use GraphQL\GraphQL; |
8
|
|
|
use GraphQL\Tests\Utils; |
9
|
|
|
use GraphQL\Type\Definition\ObjectType; |
10
|
|
|
use GraphQL\Type\Definition\Type; |
11
|
|
|
use GraphQL\Type\Definition\ValidatedFieldDefinition; |
12
|
|
|
use GraphQL\Type\Schema; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
final class GeneratedCodeTypeTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** @var Type */ |
18
|
|
|
|
19
|
|
|
public function testIntCodeType() : void |
20
|
|
|
{ |
21
|
|
|
$schema = new Schema([ |
22
|
|
|
'query' => new ObjectType(['name' => 'Query']), |
23
|
|
|
'mutation' => new ObjectType([ |
24
|
|
|
'name' => 'Mutation', |
25
|
|
|
'fields' => function () { |
26
|
|
|
return [ |
27
|
|
|
'updateBook' => new ValidatedFieldDefinition([ |
28
|
|
|
'name' => 'updateBook', |
29
|
|
|
'type' => Type::boolean(), |
30
|
|
|
'args' => [ |
31
|
|
|
'bookId' => [ |
32
|
|
|
'type' => Type::id(), |
33
|
|
|
'validate' => function ($bookId) { |
34
|
|
|
return empty($bookId) ? 1 : 0; |
35
|
|
|
}, |
36
|
|
|
], |
37
|
|
|
], |
38
|
|
|
'resolve' => static function ($value) : bool { |
39
|
|
|
return !!$value; |
40
|
|
|
}, |
41
|
|
|
]), |
42
|
|
|
]; |
43
|
|
|
}, |
44
|
|
|
]), |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
$res = GraphQL::executeQuery( |
48
|
|
|
$schema, |
49
|
|
|
Utils::nowdoc(' |
50
|
|
|
mutation UpdateBook( |
51
|
|
|
$bookId:ID |
52
|
|
|
) { |
53
|
|
|
updateBook (bookId: $bookId) { |
54
|
|
|
valid |
55
|
|
|
fields { |
56
|
|
|
bookId { |
57
|
|
|
code |
58
|
|
|
msg |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
result |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
'), |
65
|
|
|
[], |
66
|
|
|
null, |
67
|
|
|
['bookId' => null] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
static::assertEmpty($res->errors); |
71
|
|
|
static::assertEquals($res->data['updateBook']['fields']['bookId']['code'], 1); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function testStringCodeType() : void |
75
|
|
|
{ |
76
|
|
|
$schema = new Schema([ |
77
|
|
|
'query' => new ObjectType(['name' => 'Query']), |
78
|
|
|
'mutation' => new ObjectType([ |
79
|
|
|
'name' => 'Mutation', |
80
|
|
|
'fields' => function () { |
81
|
|
|
return [ |
82
|
|
|
'updateBook' => new ValidatedFieldDefinition([ |
83
|
|
|
'name' => 'updateBook', |
84
|
|
|
'type' => Type::boolean(), |
85
|
|
|
'args' => [ |
86
|
|
|
'bookId' => [ |
87
|
|
|
'type' => Type::id(), |
88
|
|
|
'errorCodes' => [ |
89
|
|
|
'invalidBookId' |
90
|
|
|
], |
91
|
|
|
'validate' => function ($bookId) { |
92
|
|
|
return empty($bookId) ? ['invalidBookId', "Invalid Book Id"] : 0; |
93
|
|
|
}, |
94
|
|
|
], |
95
|
|
|
], |
96
|
|
|
'resolve' => static function ($value) : bool { |
97
|
|
|
return !!$value; |
98
|
|
|
}, |
99
|
|
|
]), |
100
|
|
|
]; |
101
|
|
|
}, |
102
|
|
|
]), |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$res = GraphQL::executeQuery( |
106
|
|
|
$schema, |
107
|
|
|
Utils::nowdoc(' |
108
|
|
|
mutation UpdateBook( |
109
|
|
|
$bookId:ID |
110
|
|
|
) { |
111
|
|
|
updateBook (bookId: $bookId) { |
112
|
|
|
valid |
113
|
|
|
fields { |
114
|
|
|
bookId { |
115
|
|
|
code |
116
|
|
|
msg |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
result |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
'), |
123
|
|
|
[], |
124
|
|
|
null, |
125
|
|
|
['bookId' => null] |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
static::assertEmpty($res->errors); |
129
|
|
|
static::assertEquals($res->data['updateBook']['fields']['bookId']['code'], "invalidBookId"); |
130
|
|
|
static::assertEquals($res->data['updateBook']['fields']['bookId']['msg'], "Invalid Book Id"); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|