1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arthem\GraphQLMapper\Mapping; |
4
|
|
|
|
5
|
|
|
use Arthem\GraphQLMapper\Utils\TypeParser; |
6
|
|
|
|
7
|
|
|
class MappingNormalizer |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Validates mapping and fixes missing definitions |
11
|
|
|
* |
12
|
|
|
* @param SchemaContainer $schemaContainer |
13
|
|
|
*/ |
14
|
|
|
public function normalize(SchemaContainer $schemaContainer) |
15
|
|
|
{ |
16
|
|
|
$this->fixNames($schemaContainer); |
17
|
|
|
|
18
|
|
|
foreach ($schemaContainer->getTypes() as $type) { |
19
|
|
|
foreach ($type->getFields() as $field) { |
20
|
|
|
$this->normalizeField($schemaContainer, $field); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (null !== $querySchema = $schemaContainer->getQuerySchema()) { |
25
|
|
|
foreach ($querySchema->getFields() as $field) { |
26
|
|
|
$this->normalizeField($schemaContainer, $field); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if (null !== $mutationSchema = $schemaContainer->getMutationSchema()) { |
31
|
|
|
foreach ($mutationSchema->getFields() as $field) { |
32
|
|
|
$this->normalizeField($schemaContainer, $field); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param SchemaContainer $schemaContainer |
39
|
|
|
*/ |
40
|
|
|
private function fixNames(SchemaContainer $schemaContainer) |
41
|
|
|
{ |
42
|
|
|
if (null !== $query = $schemaContainer->getQuerySchema()) { |
43
|
|
|
$query->setName('Query'); |
44
|
|
|
if (null === $query->getDescription()) { |
45
|
|
|
$query->setDescription('The query root of this schema'); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (null !== $mutation = $schemaContainer->getMutationSchema()) { |
50
|
|
|
$mutation->setName('Mutation'); |
51
|
|
|
if (null === $mutation->getDescription()) { |
52
|
|
|
$mutation->setDescription('The mutation root of this schema'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param SchemaContainer $schemaContainer |
59
|
|
|
* @param Field $field |
60
|
|
|
*/ |
61
|
|
|
private function normalizeField(SchemaContainer $schemaContainer, Field $field) |
62
|
|
|
{ |
63
|
|
|
$config = $field->getResolveConfig(); |
64
|
|
|
|
65
|
|
|
// TODO tranform to a Guesser |
66
|
|
|
if (isset($config['function']) && !isset($config['handler'])) { |
67
|
|
|
$field->mergeRevolveConfig(['handler' => 'callable']); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->mergeResolveConfig($schemaContainer, $field); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param SchemaContainer $schemaContainer |
75
|
|
|
* @param Field $field |
76
|
|
|
*/ |
77
|
|
|
private function mergeResolveConfig(SchemaContainer $schemaContainer, Field $field) |
78
|
|
|
{ |
79
|
|
|
$typeName = TypeParser::getFinalType($field->getType()); |
80
|
|
|
|
81
|
|
|
if (!$schemaContainer->hasType($typeName)) { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$typeConfig = $schemaContainer |
86
|
|
|
->getType($typeName) |
87
|
|
|
->getResolveConfig(); |
88
|
|
|
|
89
|
|
|
$field->mergeResolveConfig($typeConfig); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|