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