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
|
|
|
$fields = []; |
19
|
|
|
|
20
|
|
|
foreach ($schemaContainer->getTypes() as $type) { |
21
|
|
|
$fields = array_merge($fields, $type->getFields()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (null !== $querySchema = $schemaContainer->getQuerySchema()) { |
25
|
|
|
$fields = array_merge($fields, $querySchema->getFields()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (null !== $mutationSchema = $schemaContainer->getMutationSchema()) { |
29
|
|
|
$fields = array_merge($fields, $mutationSchema->getFields()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
foreach ($fields as $field) { |
33
|
|
|
$this->mergeResolveConfig($schemaContainer, $field); |
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
|
|
|
* Apply the resolve config of types that are used by query fields |
59
|
|
|
* |
60
|
|
|
* @param SchemaContainer $schemaContainer |
61
|
|
|
* @param Field $field |
62
|
|
|
*/ |
63
|
|
|
private function mergeResolveConfig(SchemaContainer $schemaContainer, Field $field) |
64
|
|
|
{ |
65
|
|
|
$typeName = TypeParser::getFinalType($field->getType()); |
66
|
|
|
|
67
|
|
|
if ($schemaContainer->hasType($typeName)) { |
68
|
|
|
$typeConfig = $schemaContainer |
69
|
|
|
->getType($typeName) |
70
|
|
|
->getResolveConfig(); |
71
|
|
|
} elseif ($schemaContainer->hasInterface($typeName)) { |
72
|
|
|
$typeConfig = $schemaContainer |
73
|
|
|
->getInterface($typeName) |
74
|
|
|
->getResolveConfig(); |
75
|
|
|
} else { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$field->mergeResolveConfig($typeConfig); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|