|
7 | 7 | use GraphQL\Type\Definition\FieldDefinition;
|
8 | 8 | use GraphQL\Type\Definition\IDType;
|
9 | 9 | use GraphQL\Type\Definition\InputObjectType;
|
| 10 | +use GraphQL\Type\Definition\InputType; |
10 | 11 | use GraphQL\Type\Definition\ListOfType;
|
11 | 12 | use GraphQL\Type\Definition\NonNull;
|
12 | 13 | use GraphQL\Type\Definition\OutputType;
|
13 | 14 | use GraphQL\Type\Definition\ScalarType;
|
14 | 15 | use GraphQL\Type\Definition\Type;
|
| 16 | +use InvalidArgumentException; |
| 17 | +use function is_array; |
15 | 18 | use TheCodingMachine\GraphQL\Controllers\Hydrators\HydratorInterface;
|
16 | 19 | use TheCodingMachine\GraphQL\Controllers\Types\DateTimeType;
|
17 | 20 | use TheCodingMachine\GraphQL\Controllers\Types\ID;
|
@@ -52,30 +55,7 @@ public function __construct(string $name, OutputType $type, array $arguments, ?c
|
52 | 55 | foreach ($arguments as $name => $arr) {
|
53 | 56 | $type = $arr['type'];
|
54 | 57 | if (isset($args[$name])) {
|
55 |
| - $val = $args[$name]; |
56 |
| - |
57 |
| - $type = $this->stripNonNullType($type); |
58 |
| - if ($type instanceof ListOfType) { |
59 |
| - $subtype = $this->stripNonNullType($type->getWrappedType()); |
60 |
| - $val = array_map(function ($item) use ($subtype, $hydrator) { |
61 |
| - if ($subtype instanceof DateTimeType) { |
62 |
| - return new \DateTimeImmutable($item); |
63 |
| - } elseif ($subtype instanceof ID) { |
64 |
| - return new ID($item); |
65 |
| - } elseif ($subtype instanceof InputObjectType) { |
66 |
| - return $hydrator->hydrate($item, $subtype); |
67 |
| - } |
68 |
| - return $item; |
69 |
| - }, $val); |
70 |
| - } elseif ($type instanceof DateTimeType) { |
71 |
| - $val = new \DateTimeImmutable($val); |
72 |
| - } elseif ($type instanceof IDType) { |
73 |
| - $val = new ID($val); |
74 |
| - } elseif ($type instanceof InputObjectType) { |
75 |
| - $val = $hydrator->hydrate($val, $type); |
76 |
| - } elseif (!$type instanceof ScalarType) { |
77 |
| - throw new \RuntimeException('Unexpected type: '.get_class($type)); |
78 |
| - } |
| 58 | + $val = $this->castVal($args[$name], $type, $hydrator); |
79 | 59 | } elseif (array_key_exists('defaultValue', $arr)) {
|
80 | 60 | $val = $arr['defaultValue'];
|
81 | 61 | } else {
|
@@ -106,4 +86,33 @@ private function stripNonNullType(Type $type): Type
|
106 | 86 | }
|
107 | 87 | return $type;
|
108 | 88 | }
|
| 89 | + |
| 90 | + /** |
| 91 | + * Casts a value received from GraphQL into an argument passed to a method. |
| 92 | + * |
| 93 | + * @param mixed $val |
| 94 | + * @param InputType $type |
| 95 | + * @return mixed |
| 96 | + */ |
| 97 | + private function castVal($val, InputType $type, HydratorInterface $hydrator) |
| 98 | + { |
| 99 | + $type = $this->stripNonNullType($type); |
| 100 | + if ($type instanceof ListOfType) { |
| 101 | + if (!is_array($val)) { |
| 102 | + throw new InvalidArgumentException('Expected GraphQL List but value passed is not an array.'); |
| 103 | + } |
| 104 | + return array_map(function($item) use ($type, $hydrator) { |
| 105 | + return $this->castVal($item, $type->getWrappedType(), $hydrator); |
| 106 | + }, $val); |
| 107 | + } elseif ($type instanceof DateTimeType) { |
| 108 | + return new \DateTimeImmutable($val); |
| 109 | + } elseif ($type instanceof IDType) { |
| 110 | + return new ID($val); |
| 111 | + } elseif ($type instanceof InputObjectType) { |
| 112 | + return $hydrator->hydrate($val, $type); |
| 113 | + } elseif (!$type instanceof ScalarType) { |
| 114 | + throw new \RuntimeException('Unexpected type: '.get_class($type)); |
| 115 | + } |
| 116 | + return $val; |
| 117 | + } |
109 | 118 | }
|
0 commit comments