Skip to content

Commit e07b172

Browse files
committed
Refactoring queryfield input field management
1 parent 216dc98 commit e07b172

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

src/QueryField.php

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use GraphQL\Type\Definition\FieldDefinition;
88
use GraphQL\Type\Definition\IDType;
99
use GraphQL\Type\Definition\InputObjectType;
10+
use GraphQL\Type\Definition\InputType;
1011
use GraphQL\Type\Definition\ListOfType;
1112
use GraphQL\Type\Definition\NonNull;
1213
use GraphQL\Type\Definition\OutputType;
1314
use GraphQL\Type\Definition\ScalarType;
1415
use GraphQL\Type\Definition\Type;
16+
use InvalidArgumentException;
17+
use function is_array;
1518
use TheCodingMachine\GraphQL\Controllers\Hydrators\HydratorInterface;
1619
use TheCodingMachine\GraphQL\Controllers\Types\DateTimeType;
1720
use TheCodingMachine\GraphQL\Controllers\Types\ID;
@@ -52,30 +55,7 @@ public function __construct(string $name, OutputType $type, array $arguments, ?c
5255
foreach ($arguments as $name => $arr) {
5356
$type = $arr['type'];
5457
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);
7959
} elseif (array_key_exists('defaultValue', $arr)) {
8060
$val = $arr['defaultValue'];
8161
} else {
@@ -106,4 +86,33 @@ private function stripNonNullType(Type $type): Type
10686
}
10787
return $type;
10888
}
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+
}
109118
}

src/Types/ID.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace TheCodingMachine\GraphQL\Controllers\Types;
33

44

5+
use InvalidArgumentException;
56
use TheCodingMachine\GraphQL\Controllers\GraphQLException;
67

78
/**
@@ -14,7 +15,7 @@ class ID
1415
public function __construct($value)
1516
{
1617
if (! is_scalar($value) && (! is_object($value) || ! method_exists($value, '__toString'))) {
17-
throw new GraphQLException('ID constructor cannot be passed a non scalar value.');
18+
throw new InvalidArgumentException('ID constructor cannot be passed a non scalar value.');
1819
}
1920
$this->value = $value;
2021
}

tests/Types/IDTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace TheCodingMachine\GraphQL\Controllers\Types;
4+
5+
use InvalidArgumentException;
6+
use PHPUnit\Framework\TestCase;
7+
use stdClass;
8+
9+
class IDTest extends TestCase
10+
{
11+
public function testConstructException()
12+
{
13+
$this->expectException(InvalidArgumentException::class);
14+
new ID(new stdClass());
15+
}
16+
17+
public function testVal()
18+
{
19+
$id = new ID(42);
20+
$this->assertSame(42, $id->val());
21+
$this->assertSame('42', (string) $id);
22+
}
23+
}

0 commit comments

Comments
 (0)