1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overblog\GraphQLBundle\Config\Parser; |
4
|
|
|
|
5
|
|
|
use GraphQL\Language\AST\DefinitionNode; |
6
|
|
|
use GraphQL\Language\AST\FieldDefinitionNode; |
7
|
|
|
use GraphQL\Language\AST\InputValueDefinitionNode; |
8
|
|
|
use GraphQL\Language\AST\NameNode; |
9
|
|
|
use GraphQL\Language\AST\Node; |
10
|
|
|
use GraphQL\Language\AST\NodeKind; |
11
|
|
|
use GraphQL\Language\AST\NodeList; |
12
|
|
|
use GraphQL\Language\AST\TypeNode; |
13
|
|
|
use GraphQL\Language\AST\ValueNode; |
14
|
|
|
use GraphQL\Language\Parser; |
15
|
|
|
use GraphQL\Type\Definition\Directive; |
16
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
class GraphQLParser implements ParserInterface |
21
|
|
|
{ |
22
|
|
|
/** @var self */ |
23
|
|
|
private static $parser; |
24
|
|
|
|
25
|
|
|
const DEFINITION_TYPE_MAPPING = [ |
26
|
|
|
NodeKind::OBJECT_TYPE_DEFINITION => 'object', |
27
|
|
|
NodeKind::INTERFACE_TYPE_DEFINITION => 'interface', |
28
|
|
|
NodeKind::ENUM_TYPE_DEFINITION => 'enum', |
29
|
|
|
NodeKind::UNION_TYPE_DEFINITION => 'union', |
30
|
|
|
NodeKind::INPUT_OBJECT_TYPE_DEFINITION => 'input-object', |
31
|
|
|
NodeKind::SCALAR_TYPE_DEFINITION => 'custom-scalar', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
5 |
|
public static function parse(\SplFileInfo $file, ContainerBuilder $container) |
38
|
|
|
{ |
39
|
5 |
|
$container->addResource(new FileResource($file->getRealPath())); |
40
|
5 |
|
$content = \trim(\file_get_contents($file->getPathname())); |
41
|
5 |
|
$typesConfig = []; |
42
|
|
|
|
43
|
|
|
// allow empty files |
44
|
5 |
|
if (empty($content)) { |
45
|
1 |
|
return []; |
46
|
|
|
} |
47
|
4 |
|
if (!self::$parser) { |
48
|
1 |
|
self::$parser = new static(); |
49
|
|
|
} |
50
|
|
|
try { |
51
|
4 |
|
$ast = Parser::parse($content); |
52
|
1 |
|
} catch (\Exception $e) { |
53
|
1 |
|
throw new InvalidArgumentException(\sprintf('An error occurred while parsing the file "%s".', $file), $e->getCode(), $e); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
foreach ($ast->definitions as $typeDef) { |
57
|
3 |
|
if (isset($typeDef->name) && $typeDef->name instanceof NameNode) { |
58
|
2 |
|
$typeConfig = self::$parser->typeDefinitionToConfig($typeDef); |
59
|
2 |
|
$typesConfig[$typeDef->name->value] = $typeConfig; |
60
|
|
|
} else { |
61
|
3 |
|
self::throwUnsupportedDefinitionNode($typeDef); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
return $typesConfig; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public static function mustOverrideConfig() |
69
|
|
|
{ |
70
|
1 |
|
throw new \RuntimeException('Config entry must be override with ResolverMap to be used.'); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
protected function typeDefinitionToConfig(DefinitionNode $typeDef) |
74
|
|
|
{ |
75
|
2 |
|
if (isset($typeDef->kind)) { |
|
|
|
|
76
|
2 |
|
switch ($typeDef->kind) { |
|
|
|
|
77
|
2 |
|
case NodeKind::OBJECT_TYPE_DEFINITION: |
78
|
2 |
|
case NodeKind::INTERFACE_TYPE_DEFINITION: |
79
|
2 |
|
case NodeKind::INPUT_OBJECT_TYPE_DEFINITION: |
80
|
2 |
|
case NodeKind::ENUM_TYPE_DEFINITION: |
81
|
2 |
|
case NodeKind::UNION_TYPE_DEFINITION: |
82
|
2 |
|
$config = []; |
83
|
2 |
|
$this->addTypeFields($typeDef, $config); |
84
|
2 |
|
$this->addDescription($typeDef, $config); |
|
|
|
|
85
|
2 |
|
$this->addInterfaces($typeDef, $config); |
86
|
2 |
|
$this->addTypes($typeDef, $config); |
87
|
2 |
|
$this->addValues($typeDef, $config); |
88
|
|
|
|
89
|
|
|
return [ |
90
|
2 |
|
'type' => self::DEFINITION_TYPE_MAPPING[$typeDef->kind], |
|
|
|
|
91
|
2 |
|
'config' => $config, |
92
|
|
|
]; |
93
|
|
|
|
94
|
2 |
|
case NodeKind::SCALAR_TYPE_DEFINITION: |
95
|
2 |
|
$mustOverride = [__CLASS__, 'mustOverrideConfig']; |
96
|
|
|
$config = [ |
97
|
2 |
|
'serialize' => $mustOverride, |
98
|
2 |
|
'parseValue' => $mustOverride, |
99
|
2 |
|
'parseLiteral' => $mustOverride, |
100
|
|
|
]; |
101
|
2 |
|
$this->addDescription($typeDef, $config); |
|
|
|
|
102
|
|
|
|
103
|
|
|
return [ |
104
|
2 |
|
'type' => self::DEFINITION_TYPE_MAPPING[$typeDef->kind], |
|
|
|
|
105
|
2 |
|
'config' => $config, |
106
|
|
|
]; |
107
|
|
|
break; |
|
|
|
|
108
|
|
|
|
109
|
|
|
default: |
110
|
|
|
self::throwUnsupportedDefinitionNode($typeDef); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
self::throwUnsupportedDefinitionNode($typeDef); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
private static function throwUnsupportedDefinitionNode(DefinitionNode $typeDef) |
118
|
|
|
{ |
119
|
1 |
|
$path = \explode('\\', \get_class($typeDef)); |
120
|
1 |
|
throw new InvalidArgumentException( |
121
|
1 |
|
\sprintf( |
122
|
1 |
|
'%s definition is not supported right now.', |
123
|
1 |
|
\preg_replace('@DefinitionNode$@', '', \array_pop($path)) |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param DefinitionNode $typeDef |
130
|
|
|
* @param array $config |
131
|
|
|
*/ |
132
|
2 |
|
private function addTypeFields(DefinitionNode $typeDef, array &$config) |
133
|
|
|
{ |
134
|
2 |
|
if (!empty($typeDef->fields)) { |
|
|
|
|
135
|
2 |
|
$fields = []; |
136
|
|
|
/** @var FieldDefinitionNode|InputValueDefinitionNode $fieldDef */ |
137
|
2 |
|
foreach ($typeDef->fields as $fieldDef) { |
|
|
|
|
138
|
2 |
|
$fieldName = $fieldDef->name->value; |
139
|
2 |
|
$fields[$fieldName] = []; |
140
|
2 |
|
$this->addType($fieldDef, $fields[$fieldName]); |
141
|
2 |
|
$this->addDescription($fieldDef, $fields[$fieldName]); |
142
|
2 |
|
$this->addDefaultValue($fieldDef, $fields[$fieldName]); |
143
|
2 |
|
$this->addFieldArguments($fieldDef, $fields[$fieldName]); |
144
|
2 |
|
$this->addFromDirectives($fieldDef->directives, $fields[$fieldName]); |
145
|
|
|
} |
146
|
2 |
|
$config['fields'] = $fields; |
147
|
|
|
} |
148
|
2 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param Node $fieldDef |
152
|
|
|
* @param array $fieldConf |
153
|
|
|
*/ |
154
|
2 |
|
private function addFieldArguments(Node $fieldDef, array &$fieldConf) |
155
|
|
|
{ |
156
|
2 |
|
if (!empty($fieldDef->arguments)) { |
157
|
2 |
|
$arguments = []; |
158
|
2 |
|
foreach ($fieldDef->arguments as $definition) { |
|
|
|
|
159
|
2 |
|
$name = $definition->name->value; |
160
|
2 |
|
$arguments[$name] = []; |
161
|
2 |
|
$this->addType($definition, $arguments[$name]); |
162
|
2 |
|
$this->addDescription($definition, $arguments[$name]); |
163
|
2 |
|
$this->addDefaultValue($definition, $arguments[$name]); |
164
|
|
|
} |
165
|
2 |
|
$fieldConf['args'] = $arguments; |
166
|
|
|
} |
167
|
2 |
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param DefinitionNode $typeDef |
171
|
|
|
* @param array $config |
172
|
|
|
*/ |
173
|
2 |
View Code Duplication |
private function addInterfaces(DefinitionNode $typeDef, array &$config) |
|
|
|
|
174
|
|
|
{ |
175
|
2 |
|
if (!empty($typeDef->interfaces)) { |
|
|
|
|
176
|
2 |
|
$interfaces = []; |
177
|
2 |
|
foreach ($typeDef->interfaces as $interface) { |
|
|
|
|
178
|
2 |
|
$interfaces[] = $this->astTypeNodeToString($interface); |
179
|
|
|
} |
180
|
2 |
|
$config['interfaces'] = $interfaces; |
181
|
|
|
} |
182
|
2 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param DefinitionNode $typeDef |
186
|
|
|
* @param array $config |
187
|
|
|
*/ |
188
|
2 |
View Code Duplication |
private function addTypes(DefinitionNode $typeDef, array &$config) |
|
|
|
|
189
|
|
|
{ |
190
|
2 |
|
if (!empty($typeDef->types)) { |
|
|
|
|
191
|
1 |
|
$types = []; |
192
|
1 |
|
foreach ($typeDef->types as $type) { |
|
|
|
|
193
|
1 |
|
$types[] = $this->astTypeNodeToString($type); |
194
|
|
|
} |
195
|
1 |
|
$config['types'] = $types; |
196
|
|
|
} |
197
|
2 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param DefinitionNode $typeDef |
201
|
|
|
* @param array $config |
202
|
|
|
*/ |
203
|
2 |
|
private function addValues(DefinitionNode $typeDef, array &$config) |
204
|
|
|
{ |
205
|
2 |
|
if (!empty($typeDef->values)) { |
|
|
|
|
206
|
2 |
|
$values = []; |
207
|
2 |
|
foreach ($typeDef->values as $value) { |
|
|
|
|
208
|
2 |
|
$values[$value->name->value] = ['value' => $value->name->value]; |
209
|
2 |
|
$this->addDescription($value, $values[$value->name->value]); |
210
|
2 |
|
$this->addFromDirectives($value->directives, $values[$value->name->value]); |
211
|
|
|
} |
212
|
2 |
|
$config['values'] = $values; |
213
|
|
|
} |
214
|
2 |
|
} |
215
|
|
|
|
216
|
2 |
|
private function addFromDirectives(NodeList $directives, array &$config) |
217
|
|
|
{ |
218
|
2 |
|
if ($directives->count()) { |
219
|
1 |
|
foreach ($directives as $directiveDef) { |
220
|
1 |
|
if ('deprecated' === $directiveDef->name->value) { |
221
|
1 |
|
$reason = $directiveDef->arguments->count() ? |
222
|
1 |
|
$directiveDef->arguments[0]->value->value : Directive::DEFAULT_DEPRECATION_REASON; |
223
|
|
|
|
224
|
1 |
|
$config['deprecationReason'] = $reason; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
2 |
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param Node $definition |
232
|
|
|
* @param array $config |
233
|
|
|
*/ |
234
|
2 |
|
private function addType(Node $definition, array &$config) |
235
|
|
|
{ |
236
|
2 |
|
if (!empty($definition->type)) { |
237
|
2 |
|
$config['type'] = $this->astTypeNodeToString($definition->type); |
|
|
|
|
238
|
|
|
} |
239
|
2 |
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param Node $definition |
243
|
|
|
* @param array $config |
244
|
|
|
*/ |
245
|
2 |
|
private function addDescription(Node $definition, array &$config) |
246
|
|
|
{ |
247
|
|
|
if ( |
248
|
2 |
|
!empty($definition->description) |
|
|
|
|
249
|
2 |
|
&& $description = $this->cleanAstDescription($definition->description) |
250
|
|
|
) { |
251
|
1 |
|
$config['description'] = $description; |
252
|
|
|
} |
253
|
2 |
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param Node $definition |
257
|
|
|
* @param array $config |
258
|
|
|
*/ |
259
|
2 |
|
private function addDefaultValue(Node $definition, array &$config) |
260
|
|
|
{ |
261
|
2 |
|
if (!empty($definition->defaultValue)) { |
262
|
1 |
|
$config['defaultValue'] = $this->astValueNodeToConfig($definition->defaultValue); |
|
|
|
|
263
|
|
|
} |
264
|
2 |
|
} |
265
|
|
|
|
266
|
2 |
|
private function astTypeNodeToString(TypeNode $typeNode) |
267
|
|
|
{ |
268
|
2 |
|
$type = ''; |
269
|
2 |
|
switch ($typeNode->kind) { |
|
|
|
|
270
|
2 |
|
case NodeKind::NAMED_TYPE: |
271
|
2 |
|
$type = $typeNode->name->value; |
|
|
|
|
272
|
2 |
|
break; |
273
|
|
|
|
274
|
2 |
|
case NodeKind::NON_NULL_TYPE: |
275
|
2 |
|
$type = $this->astTypeNodeToString($typeNode->type).'!'; |
|
|
|
|
276
|
2 |
|
break; |
277
|
|
|
|
278
|
2 |
|
case NodeKind::LIST_TYPE: |
279
|
2 |
|
$type = '['.$this->astTypeNodeToString($typeNode->type).']'; |
|
|
|
|
280
|
2 |
|
break; |
281
|
|
|
} |
282
|
|
|
|
283
|
2 |
|
return $type; |
284
|
|
|
} |
285
|
|
|
|
286
|
1 |
|
private function astValueNodeToConfig(ValueNode $valueNode) |
287
|
|
|
{ |
288
|
1 |
|
$config = null; |
289
|
1 |
|
switch ($valueNode->kind) { |
|
|
|
|
290
|
1 |
|
case NodeKind::INT: |
291
|
1 |
|
case NodeKind::FLOAT: |
292
|
1 |
|
case NodeKind::STRING: |
293
|
1 |
|
case NodeKind::BOOLEAN: |
294
|
1 |
|
case NodeKind::ENUM: |
295
|
1 |
|
$config = $valueNode->value; |
|
|
|
|
296
|
1 |
|
break; |
297
|
|
|
|
298
|
1 |
|
case NodeKind::LST: |
299
|
1 |
|
$config = []; |
300
|
1 |
|
foreach ($valueNode->values as $node) { |
|
|
|
|
301
|
1 |
|
$config[] = $this->astValueNodeToConfig($node); |
302
|
|
|
} |
303
|
1 |
|
break; |
304
|
|
|
|
305
|
1 |
|
case NodeKind::NULL: |
306
|
1 |
|
$config = null; |
307
|
1 |
|
break; |
308
|
|
|
} |
309
|
|
|
|
310
|
1 |
|
return $config; |
311
|
|
|
} |
312
|
|
|
|
313
|
1 |
|
private function cleanAstDescription($description) |
314
|
|
|
{ |
315
|
1 |
|
if (\property_exists($description, 'value')) { |
316
|
1 |
|
$description = $description->value; |
317
|
|
|
} |
318
|
1 |
|
$description = \trim($description); |
319
|
|
|
|
320
|
1 |
|
return empty($description) ? null : $description; |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: