1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\GraphQL\Controllers; |
5
|
|
|
|
6
|
|
|
use TheCodingMachine\GraphQL\Controllers\Registry\Registry; |
7
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
8
|
|
|
use Youshido\GraphQL\Field\AbstractField; |
9
|
|
|
use Youshido\GraphQL\Type\AbstractType; |
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\Scalar\DateTimeType; |
14
|
|
|
use Youshido\GraphQL\Type\TypeInterface; |
15
|
|
|
use Youshido\GraphQL\Type\TypeMap; |
16
|
|
|
|
17
|
|
|
class QueryField extends AbstractField |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var HydratorInterface |
21
|
|
|
*/ |
22
|
|
|
private $hydrator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* QueryField constructor. |
26
|
|
|
* @param string $name |
27
|
|
|
* @param TypeInterface $type |
28
|
|
|
* @param TypeInterface[] $arguments Indexed by argument name. |
29
|
|
|
* @param callable $resolve |
30
|
|
|
* @param array $additionalConfig |
31
|
|
|
*/ |
32
|
|
|
public function __construct(string $name, TypeInterface $type, array $arguments, callable $resolve, HydratorInterface $hydrator, array $additionalConfig = []) |
33
|
|
|
{ |
34
|
|
|
$this->hydrator = $hydrator; |
35
|
|
|
$config = [ |
36
|
|
|
'name' => $name, |
37
|
|
|
'type' => $type, |
38
|
|
|
'args' => $arguments |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
$config['resolve'] = function ($source, array $args, ResolveInfo $info) use ($resolve, $arguments) { |
|
|
|
|
42
|
|
|
$toPassArgs = []; |
43
|
|
|
foreach ($arguments as $name => $type) { |
44
|
|
|
// FIXME: this is not ok for default values! We need to take the default value of the reflected argument. |
45
|
|
|
$val = $args[$name] ?? null; |
46
|
|
|
|
47
|
|
|
$type = $this->stripNonNullType($type); |
48
|
|
|
if ($type instanceof ListType) { |
49
|
|
|
$subtype = $this->stripNonNullType($type->getItemType()); |
50
|
|
|
$val = array_map(function ($item) use ($subtype) { |
51
|
|
|
if ($subtype instanceof DateTimeType) { |
52
|
|
|
return new \DateTimeImmutable($item); |
53
|
|
|
} elseif ($subtype->getKind() === TypeMap::KIND_OBJECT) { |
54
|
|
|
return $this->hydrator->hydrate($item, $subtype); |
55
|
|
|
}; |
56
|
|
|
return $item; |
57
|
|
|
}, $val); |
58
|
|
|
} elseif ($type instanceof DateTimeType) { |
59
|
|
|
$val = new \DateTimeImmutable($val); |
60
|
|
|
} elseif ($type->getKind() === TypeMap::KIND_OBJECT) { |
61
|
|
|
$val = $this->hydrator->hydrate($val, $type); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$toPassArgs[] = $val; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $resolve(...$toPassArgs); |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$config += $additionalConfig; |
71
|
|
|
parent::__construct($config); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return AbstractObjectType|AbstractType |
76
|
|
|
*/ |
77
|
|
|
public function getType() |
78
|
|
|
{ |
79
|
|
|
return $this->config->getType(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function stripNonNullType(TypeInterface $type): TypeInterface |
83
|
|
|
{ |
84
|
|
|
if ($type instanceof NonNullType) { |
85
|
|
|
return $this->stripNonNullType($type->getTypeOf()); |
86
|
|
|
} |
87
|
|
|
return $type; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.