|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HansOtt\GraphQL\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use HansOtt\GraphQL\Shared\ScannerTokens; |
|
6
|
|
|
use HansOtt\GraphQL\Shared\ScannerGeneric; |
|
7
|
|
|
use HansOtt\GraphQL\Shared\Parser as ParserShared; |
|
8
|
|
|
|
|
9
|
|
|
final class Parser extends ParserShared |
|
10
|
|
|
{ |
|
11
|
|
|
private $objects; |
|
12
|
|
|
private $enums; |
|
13
|
|
|
private $unions; |
|
14
|
|
|
private $interfaces; |
|
15
|
|
|
private $inputObjects; |
|
16
|
|
|
|
|
17
|
3 |
|
protected function getNameFor($tokenType) |
|
18
|
|
|
{ |
|
19
|
3 |
|
return Token::getNameFor($tokenType); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
6 |
|
private function parseListType() |
|
23
|
|
|
{ |
|
24
|
6 |
|
$location = $this->expect(Token::T_BRACKET_LEFT)->location; |
|
25
|
6 |
|
$type = $this->parseType(); |
|
26
|
6 |
|
$this->accept(Token::T_BRACKET_RIGHT); |
|
27
|
|
|
|
|
28
|
6 |
|
return new TypeList($type, $location); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
9 |
|
private function parseNamedType() |
|
32
|
|
|
{ |
|
33
|
9 |
|
$nameToken = $this->expect(Token::T_NAME); |
|
34
|
9 |
|
$type = new TypeNamed($nameToken->value, $nameToken->location); |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
9 |
|
return $type; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
9 |
|
private function maybeParseNullType(Type $type) |
|
40
|
|
|
{ |
|
41
|
9 |
|
if ($this->accept(Token::T_EXCLAMATION)) { |
|
42
|
3 |
|
return new TypeNonNull($type, $type->getLocation()); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
9 |
|
return $type; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
9 |
View Code Duplication |
private function parseType() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
9 |
|
if ($this->is(Token::T_BRACKET_LEFT)) { |
|
51
|
6 |
|
return $this->maybeParseNullType( |
|
52
|
6 |
|
$this->parseListType() |
|
53
|
6 |
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
9 |
|
if ($this->is(Token::T_NAME)) { |
|
57
|
9 |
|
return $this->maybeParseNullType( |
|
58
|
9 |
|
$this->parseNamedType() |
|
59
|
9 |
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$message = 'Expected a type'; |
|
63
|
|
|
|
|
64
|
|
|
if ($this->scanner->eof()) { |
|
65
|
|
|
throw $this->getParseError($message . ' but instead reached end'); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$token = $this->scanner->peek(); |
|
69
|
|
|
throw $this->getParseError($message . " but instead found \"{$token->getName()}\" with value \"{$token->value}\""); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
View Code Duplication |
private function parseObject() |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
3 |
|
$location = $this->expect(Token::T_BRACE_LEFT)->location; |
|
75
|
3 |
|
$fields = array(); |
|
76
|
3 |
|
while ($this->scanner->eof() === false) { |
|
77
|
3 |
|
if ($this->accept(Token::T_BRACE_RIGHT)) { |
|
78
|
3 |
|
return new ValueObject($fields, $location); |
|
|
|
|
|
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
$nameToken = $this->expect(Token::T_NAME); |
|
82
|
3 |
|
$this->expect(Token::T_COLON); |
|
83
|
3 |
|
$fields[] = new ValueObjectField($nameToken->value, $this->parseValue(), $nameToken->location); |
|
|
|
|
|
|
84
|
3 |
|
$this->accept(Token::T_COMMA); |
|
85
|
3 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
throw $this->getParseError('Unclosed brace of object value'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
3 |
View Code Duplication |
private function parseList() |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
3 |
|
$location = $this->expect(Token::T_BRACKET_LEFT)->location; |
|
93
|
3 |
|
$items = array(); |
|
94
|
3 |
|
while ($this->scanner->eof() === false) { |
|
95
|
3 |
|
if ($this->accept(Token::T_BRACKET_RIGHT)) { |
|
96
|
3 |
|
return new ValueList($items, $location); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
$items[] = $this->parseValue(); |
|
100
|
3 |
|
$this->accept(Token::T_COMMA); |
|
101
|
3 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
throw $this->getParseError('Unclosed bracket of list'); |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
6 |
|
private function parseValue() |
|
108
|
|
|
{ |
|
109
|
6 |
|
if ($string = $this->accept(Token::T_STRING)) { |
|
110
|
3 |
|
return new ValueString($string->value, $string->location); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
6 |
|
if ($true = $this->accept(Token::T_TRUE)) { |
|
114
|
|
|
return new ValueBoolean(true, $true->location); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
6 |
|
if ($false = $this->accept(Token::T_FALSE)) { |
|
118
|
3 |
|
return new ValueBoolean(false, $false->location); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
6 |
|
if ($null = $this->accept(Token::T_NULL)) { |
|
122
|
|
|
return new ValueNull($null->location); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
6 |
|
if ($int = $this->accept(Token::T_INT)) { |
|
126
|
3 |
|
return new ValueInt($int->value, $int->location); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
6 |
|
if ($float = $this->accept(Token::T_FLOAT)) { |
|
130
|
3 |
|
return new ValueFloat($float->value, $float->location); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
6 |
|
if ($name = $this->accept(Token::T_NAME)) { |
|
134
|
3 |
|
return new ValueEnum($name->value, $name->location); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
6 |
|
if ($this->is(Token::T_BRACKET_LEFT)) { |
|
138
|
3 |
|
return $this->parseList(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
3 |
|
if ($this->is(Token::T_BRACE_LEFT)) { |
|
142
|
3 |
|
return $this->parseObject(); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
$message = 'Expected a value'; |
|
146
|
|
|
|
|
147
|
|
|
if ($this->scanner->eof()) { |
|
148
|
|
|
throw $this->getParseError($message . ' but instead reached end'); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$token = $this->scanner->peek(); |
|
152
|
|
|
throw $this->getParseError($message . " but instead found \"{$token->getName()}\" with value \"{$token->value}\""); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
9 |
|
private function parseFieldArguments() |
|
156
|
|
|
{ |
|
157
|
9 |
|
if ($this->accept(Token::T_PAREN_LEFT) === false) { |
|
158
|
6 |
|
return array(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
6 |
|
$arguments = array(); |
|
162
|
6 |
|
while ($this->scanner->eof() === false) { |
|
163
|
6 |
|
if ($this->accept(Token::T_PAREN_RIGHT)) { |
|
164
|
6 |
|
return $arguments; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
6 |
|
$nameToken = $this->expect(Token::T_NAME); |
|
168
|
6 |
|
$this->expect(Token::T_COLON); |
|
169
|
6 |
|
$type = $this->parseType(); |
|
170
|
6 |
|
$defaultValue = null; |
|
171
|
|
|
|
|
172
|
6 |
|
if ($this->accept(Token::T_EQUAL)) { |
|
173
|
6 |
|
$defaultValue = $this->parseValue(); |
|
174
|
6 |
|
} |
|
175
|
|
|
|
|
176
|
6 |
|
$arguments[] = new Argument( |
|
177
|
6 |
|
$nameToken->value, |
|
178
|
6 |
|
$type, |
|
179
|
6 |
|
$defaultValue, |
|
180
|
6 |
|
$nameToken->location |
|
|
|
|
|
|
181
|
6 |
|
); |
|
182
|
|
|
|
|
183
|
6 |
|
$this->accept(Token::T_COMMA); |
|
184
|
6 |
|
} |
|
185
|
|
|
|
|
186
|
|
|
return $arguments; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
12 |
View Code Duplication |
private function parseField($forInputObject) |
|
|
|
|
|
|
190
|
|
|
{ |
|
191
|
12 |
|
$nameToken = $this->expect(Token::T_NAME); |
|
192
|
|
|
|
|
193
|
12 |
|
$arguments = array(); |
|
194
|
12 |
|
if ($forInputObject === false) { |
|
195
|
9 |
|
$arguments = $this->parseFieldArguments(); |
|
196
|
9 |
|
} |
|
197
|
|
|
|
|
198
|
12 |
|
$this->expect(Token::T_COLON); |
|
199
|
9 |
|
$type = $this->parseType(); |
|
200
|
|
|
|
|
201
|
9 |
|
return new Field( |
|
202
|
9 |
|
$nameToken->value, |
|
203
|
9 |
|
$type, |
|
204
|
9 |
|
$arguments, |
|
205
|
9 |
|
$nameToken->location |
|
|
|
|
|
|
206
|
9 |
|
); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
12 |
|
private function parseObjectDeclaration() |
|
210
|
|
|
{ |
|
211
|
9 |
|
$location = $this->expect(Token::T_NAME)->location; |
|
212
|
9 |
|
$name = $this->expect(Token::T_NAME)->value; |
|
213
|
|
|
|
|
214
|
9 |
|
$interface = null; |
|
215
|
9 |
|
if ($implementsToken = $this->accept(Token::T_NAME)) { |
|
216
|
3 |
|
if ($implementsToken->value !== 'implements') { |
|
217
|
|
|
throw $this->getParseError("Expected \"implements\" but instead found \"{$implementsToken->value}\""); |
|
218
|
|
|
} |
|
219
|
3 |
|
$interface = $this->expect(Token::T_NAME)->value; |
|
220
|
3 |
|
} |
|
221
|
|
|
|
|
222
|
9 |
|
$this->expect(Token::T_BRACE_LEFT); |
|
223
|
12 |
|
$fields = array(); |
|
224
|
9 |
|
while ($this->scanner->eof() === false) { |
|
225
|
9 |
|
if ($this->accept(Token::T_BRACE_RIGHT)) { |
|
226
|
9 |
|
return new DeclarationObject( |
|
227
|
9 |
|
$name, |
|
228
|
9 |
|
$fields, |
|
229
|
9 |
|
$interface, |
|
230
|
|
|
$location |
|
|
|
|
|
|
231
|
9 |
|
); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
9 |
|
$fields[] = $this->parseField(false); |
|
235
|
9 |
|
$this->accept(Token::T_COMMA); |
|
236
|
9 |
|
} |
|
237
|
|
|
|
|
238
|
|
|
throw $this->getParseError('Expected an object field but instead reached end'); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
6 |
View Code Duplication |
private function parseInputObjectDeclaration() |
|
|
|
|
|
|
242
|
|
|
{ |
|
243
|
6 |
|
$location = $this->expect(Token::T_NAME)->location; |
|
244
|
6 |
|
$name = $this->expect(Token::T_NAME)->value; |
|
245
|
6 |
|
$this->expect(Token::T_BRACE_LEFT); |
|
246
|
6 |
|
$fields = array(); |
|
247
|
6 |
|
while ($this->scanner->eof() === false) { |
|
248
|
6 |
|
if ($this->accept(Token::T_BRACE_RIGHT)) { |
|
249
|
3 |
|
return new DeclarationInputObject( |
|
250
|
3 |
|
$name, |
|
251
|
3 |
|
$fields, |
|
252
|
|
|
$location |
|
|
|
|
|
|
253
|
3 |
|
); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
6 |
|
$fields[] = $this->parseField(true); |
|
257
|
3 |
|
$this->accept(Token::T_COMMA); |
|
258
|
3 |
|
} |
|
259
|
|
|
|
|
260
|
|
|
throw $this->getParseError('Expected an input object field but instead reached end'); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
3 |
View Code Duplication |
private function parseEnumDeclaration() |
|
|
|
|
|
|
264
|
|
|
{ |
|
265
|
3 |
|
$location = $this->expect(Token::T_NAME)->location; |
|
266
|
3 |
|
$name = $this->expect(Token::T_NAME)->value; |
|
267
|
3 |
|
$this->expect(Token::T_BRACE_LEFT); |
|
268
|
3 |
|
$values = array(); |
|
269
|
3 |
|
while ($this->scanner->eof() === false) { |
|
270
|
3 |
|
if ($this->accept(Token::T_BRACE_RIGHT)) { |
|
271
|
3 |
|
return new DeclarationEnum( |
|
272
|
3 |
|
$name, |
|
273
|
3 |
|
$values, |
|
274
|
|
|
$location |
|
|
|
|
|
|
275
|
3 |
|
); |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
3 |
|
$values[] = $this->expect(Token::T_NAME)->value; |
|
279
|
3 |
|
$this->accept(Token::T_COMMA); |
|
280
|
3 |
|
} |
|
281
|
|
|
|
|
282
|
|
|
throw $this->getParseError('Expected an enumeration value but instead reached end'); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
3 |
View Code Duplication |
private function parseInterfaceDeclaration() |
|
|
|
|
|
|
286
|
|
|
{ |
|
287
|
3 |
|
$location = $this->expect(Token::T_NAME)->location; |
|
288
|
3 |
|
$name = $this->expect(Token::T_NAME)->value; |
|
289
|
3 |
|
$this->expect(Token::T_BRACE_LEFT); |
|
290
|
3 |
|
$fields = array(); |
|
291
|
3 |
|
while ($this->scanner->eof() === false) { |
|
292
|
3 |
|
if ($this->accept(Token::T_BRACE_RIGHT)) { |
|
293
|
3 |
|
return new DeclarationInterface( |
|
294
|
3 |
|
$name, |
|
295
|
3 |
|
$fields, |
|
296
|
|
|
$location |
|
|
|
|
|
|
297
|
3 |
|
); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
3 |
|
$fields[] = $this->parseField(false); |
|
301
|
3 |
|
$this->accept(Token::T_COMMA); |
|
302
|
3 |
|
} |
|
303
|
|
|
|
|
304
|
|
|
throw $this->getParseError('Expected an interface field but instead reached end'); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
6 |
|
private function parseUnionDeclaration() |
|
308
|
|
|
{ |
|
309
|
6 |
|
$location = $this->expect(Token::T_NAME)->location; |
|
310
|
6 |
|
$name = $this->expect(Token::T_NAME)->value; |
|
311
|
6 |
|
$this->expect(Token::T_EQUAL); |
|
312
|
6 |
|
$members = array(); |
|
313
|
6 |
|
while ($this->scanner->eof() === false) { |
|
314
|
6 |
|
if ($this->is(Token::T_NAME) === false) { |
|
315
|
|
|
return new DeclarationUnion( |
|
316
|
|
|
$name, |
|
317
|
|
|
$members, |
|
318
|
|
|
$location |
|
|
|
|
|
|
319
|
|
|
); |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
6 |
|
$members[] = $this->expect(Token::T_NAME)->value; |
|
323
|
|
|
|
|
324
|
6 |
|
if ($this->is(Token::T_PIPE) === false) { |
|
325
|
6 |
|
return new DeclarationUnion( |
|
326
|
6 |
|
$name, |
|
327
|
6 |
|
$members, |
|
328
|
|
|
$location |
|
|
|
|
|
|
329
|
6 |
|
); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
6 |
|
$this->expect(Token::T_PIPE); |
|
333
|
6 |
|
} |
|
334
|
|
|
|
|
335
|
|
|
throw $this->getParseError('Expected an interface field but instead reached end'); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
15 |
|
private function parseDeclaration() |
|
339
|
|
|
{ |
|
340
|
15 |
|
$message = 'Expected a type declaration'; |
|
341
|
|
|
|
|
342
|
15 |
|
if ($this->scanner->eof()) { |
|
343
|
|
|
throw $this->getParseError("{$message} but instead reached end"); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
15 |
|
$token = $this->scanner->peek(); |
|
347
|
15 |
|
switch ($token->value) { |
|
348
|
15 |
|
case 'type': |
|
349
|
9 |
|
return $this->parseObjectDeclaration(); |
|
350
|
9 |
|
case 'input': |
|
351
|
6 |
|
return $this->parseInputObjectDeclaration(); |
|
352
|
6 |
|
case 'enum': |
|
353
|
3 |
|
return $this->parseEnumDeclaration(); |
|
354
|
6 |
|
case 'interface': |
|
355
|
3 |
|
return $this->parseInterfaceDeclaration(); |
|
356
|
6 |
|
case 'union': |
|
357
|
6 |
|
return $this->parseUnionDeclaration(); |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
throw $this->getParseError("{$message} but instead found {$token->getName()} with value \"{$token->value}\""); |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
15 |
|
private function parseSchema() |
|
364
|
|
|
{ |
|
365
|
15 |
|
$declarations = array(); |
|
366
|
15 |
|
while ($this->scanner->eof() === false) { |
|
367
|
15 |
|
$declarations[] = $this->parseDeclaration(); |
|
368
|
12 |
|
} |
|
369
|
|
|
|
|
370
|
12 |
|
return new Schema($declarations); |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* @param string $schema |
|
375
|
|
|
* |
|
376
|
|
|
* @return Schema |
|
377
|
|
|
*/ |
|
378
|
15 |
|
public function parse($schema) |
|
379
|
|
|
{ |
|
380
|
15 |
|
$tokens = $this->lexer->lex($schema); |
|
381
|
15 |
|
$scanner = new ScannerGeneric($tokens); |
|
382
|
15 |
|
$this->scanner = new ScannerTokens($scanner); |
|
383
|
15 |
|
$this->objects = array(); |
|
384
|
15 |
|
$this->enums = array(); |
|
385
|
15 |
|
$this->unions = array(); |
|
386
|
15 |
|
$this->interfaces = array(); |
|
387
|
15 |
|
$this->inputObjects = array(); |
|
388
|
15 |
|
$declaration = $this->parseSchema(); |
|
389
|
|
|
|
|
390
|
12 |
|
return $declaration; |
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: