-
-
Notifications
You must be signed in to change notification settings - Fork 575
Expand file tree
/
Copy pathEagerTypeLoaderTest.php
More file actions
232 lines (191 loc) · 6.91 KB
/
Copy pathEagerTypeLoaderTest.php
File metadata and controls
232 lines (191 loc) · 6.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php declare(strict_types=1);
namespace GraphQL\Tests\Type;
use GraphQL\Error\InvariantViolation;
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\InterfaceType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
/**
* @see LazyTypeLoaderTest
*/
final class EagerTypeLoaderTest extends TypeLoaderTestCaseBase
{
private InterfaceType $node;
private InterfaceType $content;
private ObjectType $blogStory;
private ObjectType $postStoryMutation;
private InputObjectType $postStoryMutationInput;
protected function setUp(): void
{
parent::setUp();
$this->node = new InterfaceType([
'name' => 'Node',
'fields' => function (): array {
$this->calls[] = 'Node.fields';
return [
'id' => Type::string(),
];
},
'resolveType' => static fn (): ?ObjectType => null,
]);
$this->content = new InterfaceType([
'name' => 'Content',
'fields' => function (): array {
$this->calls[] = 'Content.fields';
return [
'title' => Type::string(),
'body' => Type::string(),
];
},
'resolveType' => static fn (): ?ObjectType => null,
]);
$this->blogStory = new ObjectType([
'name' => 'BlogStory',
'interfaces' => [
$this->node,
$this->content,
],
'fields' => function (): array {
$this->calls[] = 'BlogStory.fields';
return [
$this->node->getField('id'),
$this->content->getField('title'),
$this->content->getField('body'),
];
},
]);
$this->query = new ObjectType([
'name' => 'Query',
'fields' => function (): array {
$this->calls[] = 'Query.fields';
return [
'latestContent' => $this->content,
'node' => $this->node,
];
},
]);
$this->mutation = new ObjectType([
'name' => 'Mutation',
'fields' => function (): array {
$this->calls[] = 'Mutation.fields';
return [
'postStory' => [
'type' => $this->postStoryMutation,
'args' => [
'input' => Type::nonNull($this->postStoryMutationInput),
'clientRequestId' => Type::string(),
],
],
];
},
]);
$this->postStoryMutation = new ObjectType([
'name' => 'PostStoryMutation',
'fields' => [
'story' => $this->blogStory,
],
]);
$this->postStoryMutationInput = new InputObjectType([
'name' => 'PostStoryMutationInput',
'fields' => [
'title' => Type::string(),
'body' => Type::string(),
'author' => Type::id(),
'category' => Type::id(),
],
]);
$this->typeLoader = function (string $name): ?Type {
$this->calls[] = $name;
switch ($name) {
case 'Query':
return $this->query;
case 'Mutation':
return $this->mutation;
case 'Node':
return $this->node;
case 'Content':
return $this->content;
case 'BlogStory':
return $this->blogStory;
case 'PostStoryMutation':
return $this->postStoryMutation;
case 'PostStoryMutationInput':
return $this->postStoryMutationInput;
default:
return null;
}
};
}
public function testWorksWithoutTypeLoader(): void
{
$schema = new Schema([
'query' => $this->query,
'mutation' => $this->mutation,
'types' => [$this->blogStory],
]);
$schema->assertValid();
self::assertSame([
'Node.fields',
'Content.fields',
'BlogStory.fields',
'Query.fields',
'Mutation.fields',
], $this->calls);
self::assertSame($this->query, $schema->getType('Query'));
self::assertSame($this->mutation, $schema->getType('Mutation'));
self::assertSame($this->node, $schema->getType('Node'));
self::assertSame($this->content, $schema->getType('Content'));
self::assertSame($this->blogStory, $schema->getType('BlogStory'));
self::assertSame($this->postStoryMutation, $schema->getType('PostStoryMutation'));
self::assertSame($this->postStoryMutationInput, $schema->getType('PostStoryMutationInput'));
self::assertArraySubset([
'Query' => $this->query,
'Mutation' => $this->mutation,
'Node' => $this->node,
'String' => Type::string(),
'Content' => $this->content,
'BlogStory' => $this->blogStory,
'PostStoryMutationInput' => $this->postStoryMutationInput,
], $schema->getTypeMap());
}
public function testWorksWithTypeLoader(): void
{
$schema = new Schema([
'query' => $this->query,
'mutation' => $this->mutation,
'typeLoader' => $this->typeLoader,
]);
self::assertSame([], $this->calls);
$node = $schema->getType('Node');
self::assertSame($this->node, $node);
self::assertSame(['Node'], $this->calls);
$content = $schema->getType('Content');
self::assertSame($this->content, $content);
self::assertSame(['Node', 'Content'], $this->calls);
$input = $schema->getType('PostStoryMutationInput');
self::assertSame($this->postStoryMutationInput, $input);
self::assertSame(['Node', 'Content', 'PostStoryMutationInput'], $this->calls);
$result = $schema->isSubType($this->node, $this->blogStory);
self::assertTrue($result);
self::assertSame(
[
'Node',
'Content',
'PostStoryMutationInput',
],
$this->calls
);
}
public function testFailsOnInvalidLoad(): void
{
$schema = new Schema([
'query' => $this->query,
'typeLoader' => fn (): Type => $this->content,
]);
$expectedType = 'Node';
$this->expectException(InvariantViolation::class);
$this->expectExceptionMessage(Schema::typeLoaderWrongTypeName($expectedType, 'Content'));
$schema->getType($expectedType);
}
}