1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Tdbm\GraphQL; |
5
|
|
|
|
6
|
|
|
use Psr\Container\ContainerInterface; |
7
|
|
|
use TheCodingMachine\GraphQL\Controllers\TypeMapperInterface; |
8
|
|
|
use Youshido\GraphQL\Type\InputObject\InputObjectType; |
9
|
|
|
use Youshido\GraphQL\Type\InputTypeInterface; |
10
|
|
|
use Youshido\GraphQL\Type\ListType\ListType; |
11
|
|
|
use Youshido\GraphQL\Type\NonNullType; |
12
|
|
|
use Youshido\GraphQL\Type\Object\AbstractObjectType; |
13
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
14
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
15
|
|
|
|
16
|
|
|
abstract class AbstractTdbmGraphQLTypeMapper implements TypeMapperInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ContainerInterface |
20
|
|
|
*/ |
21
|
|
|
private $container; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \SplObjectStorage |
25
|
|
|
*/ |
26
|
|
|
private $typeToInputTypes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns an array mapping PHP classes to GraphQL types. |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
abstract protected function getMap(): array; |
34
|
|
|
|
35
|
|
|
public function __construct(ContainerInterface $container) |
36
|
|
|
{ |
37
|
|
|
$this->container = $container; |
38
|
|
|
$this->typeToInputTypes = new \SplObjectStorage(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Maps a PHP fully qualified class name to a GraphQL type. |
43
|
|
|
* |
44
|
|
|
* @param string $className |
45
|
|
|
* @return TypeInterface |
46
|
|
|
* @throws GraphQLException |
47
|
|
|
*/ |
48
|
|
|
public function mapClassToType(string $className): TypeInterface |
49
|
|
|
{ |
50
|
|
|
$map = $this->getMap(); |
51
|
|
|
if (!isset($map[$className])) { |
52
|
|
|
throw new GraphQLException("Unable to map class $className to any known GraphQL type."); |
53
|
|
|
} |
54
|
|
|
return $this->container->get($map[$className]); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Maps a PHP fully qualified class name to a GraphQL input type. |
59
|
|
|
* |
60
|
|
|
* @param string $className |
61
|
|
|
* @return InputTypeInterface |
62
|
|
|
* @throws GraphQLException |
63
|
|
|
*/ |
64
|
|
|
public function mapClassToInputType(string $className): InputTypeInterface |
65
|
|
|
{ |
66
|
|
|
// Let's create the input type "on the fly"! |
67
|
|
|
$type = $this->mapClassToType($className); |
68
|
|
|
if (!$type instanceof AbstractObjectType) { |
69
|
|
|
throw new GraphQLException('Cannot map a type to input type if it does not extend the AbstractObjectType class. Type passed: '.get_class($type)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->mapTypeToInputType($type); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
private function mapTypeToInputType(TypeInterface $type): InputTypeInterface |
77
|
|
|
{ |
78
|
|
|
if ($type instanceof ListType) { |
79
|
|
|
return new ListType($this->mapTypeToInputType($type->getItemType())); |
80
|
|
|
} |
81
|
|
|
/*if ($type instanceof NonNullType) { |
|
|
|
|
82
|
|
|
return new NonNullType($this->mapTypeToInputType($type->getNullableType())); |
83
|
|
|
}*/ |
84
|
|
|
// We drop the non null in the input fields |
85
|
|
|
if ($type instanceof NonNullType) { |
86
|
|
|
return $this->mapTypeToInputType($type->getNullableType()); |
87
|
|
|
} |
88
|
|
|
if (!$type instanceof AbstractObjectType) { |
89
|
|
|
return $type; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (isset($this->typeToInputTypes[$type])) { |
93
|
|
|
return $this->typeToInputTypes[$type]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$inputType = new InputObjectType([ |
97
|
|
|
'name' => $type->getName().'Input' |
98
|
|
|
]); |
99
|
|
|
|
100
|
|
|
$this->typeToInputTypes->attach($type, $inputType); |
101
|
|
|
|
102
|
|
|
$inputFields = array_map(function(\Youshido\GraphQL\Field\FieldInterface $field) { |
103
|
|
|
$type = $field->getType(); |
104
|
|
|
/*if ($type instanceof NonNullType) { |
|
|
|
|
105
|
|
|
return new NonNullType($this->mapTypeToInputType($type->getNullableType())); |
106
|
|
|
} |
107
|
|
|
if ($type instanceof ListType) { |
108
|
|
|
return new ListType($this->mapTypeToInputType($type->getItemType())); |
109
|
|
|
} |
110
|
|
|
if ($type->getKind() !== TypeMap::KIND_OBJECT) { |
111
|
|
|
return $field; |
112
|
|
|
}*/ |
113
|
|
|
|
114
|
|
|
return new \Youshido\GraphQL\Field\Field([ |
115
|
|
|
'name' => $field->getName(), |
116
|
|
|
'type' => $this->mapTypeToInputType($type), |
117
|
|
|
]); |
118
|
|
|
}, $type->getFields()); |
119
|
|
|
|
120
|
|
|
$inputType->addFields($inputFields); |
121
|
|
|
return $inputType; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.