Skip to content

Commit d6c7a2f

Browse files
author
Jeremiah VALERIE
committed
- Default Resolver retrieve property values using only PropertyAccess.
- Refactor TypeResolver
1 parent bf56296 commit d6c7a2f

File tree

4 files changed

+80
-64
lines changed

4 files changed

+80
-64
lines changed

Resolver/Resolver.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,28 @@ class Resolver
2525
public static function defaultResolveFn($source, $args, ResolveInfo $info)
2626
{
2727
$fieldName = $info->fieldName;
28-
$property = null;
28+
$value = null;
2929

3030
$index = sprintf('[%s]', $fieldName);
3131

3232
if (self::getAccessor()->isReadable($source, $index)) {
33-
$property = self::getAccessor()->getValue($source, $index);
33+
$value = self::getAccessor()->getValue($source, $index);
3434
} elseif (is_object($source)) {
35-
$property = self::propertyValueFromObject($source, $fieldName);
35+
$value = self::propertyValueFromObject($source, $fieldName);
3636
}
3737

38-
return $property instanceof \Closure ? $property($source, $args, $info) : $property;
38+
return $value instanceof \Closure ? $value($source, $args, $info) : $value;
3939
}
4040

4141
private static function propertyValueFromObject($object, $fieldName)
4242
{
43-
$property = null;
43+
$value = null;
4444

45-
// accessor try to access the value using methods
46-
// first before using public property directly
47-
// not what we wont here!
48-
if (isset($object->{$fieldName})) {
49-
$property = $object->{$fieldName};
50-
} elseif (self::getAccessor()->isReadable($object, $fieldName)) {
51-
$property = self::getAccessor()->getValue($object, $fieldName);
45+
if (self::getAccessor()->isReadable($object, $fieldName)) {
46+
$value = self::getAccessor()->getValue($object, $fieldName);
5247
}
5348

54-
return $property;
49+
return $value;
5550
}
5651

5752
private static function getAccessor()

Resolver/TypeResolver.php

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,35 +53,32 @@ public function setMapping($mapping)
5353
*/
5454
public function resolve($alias)
5555
{
56+
if (null === $alias) {
57+
return;
58+
}
59+
5660
if (null !== $type = $this->cache->fetch($alias)) {
5761
return $type;
5862
}
5963

60-
$type = $this->getType($alias);
64+
$type = $this->string2Type($alias);
6165

6266
$this->cache->save($alias, $type);
6367

6468
return $type;
6569
}
6670

67-
private function getType($alias)
71+
private function string2Type($alias)
6872
{
69-
if (!is_string($alias)) {
70-
return $alias;
71-
}
72-
// Non-Null
73-
if ('!' === $alias[strlen($alias) - 1]) {
74-
return Type::nonNull($this->getType(substr($alias, 0, -1)));
73+
if (false !== ($type = $this->wrapTypeIfNeeded($alias))) {
74+
return $type;
7575
}
76-
// List
77-
if ('[' === $alias[0]) {
78-
if (']' !== $alias[strlen($alias) - 1]) {
79-
throw new UnresolvableException(sprintf('Invalid type "%s"', $alias));
80-
}
8176

82-
return Type::listOf($this->getType(substr($alias, 1, -1)));
83-
}
77+
return $this->baseType($alias);
78+
}
8479

80+
private function baseType($alias)
81+
{
8582
$type = $this->getSolution($alias);
8683
if (null !== $type) {
8784
return $type;
@@ -98,6 +95,36 @@ private function getType($alias)
9895
);
9996
}
10097

98+
private function wrapTypeIfNeeded($alias)
99+
{
100+
// Non-Null
101+
if ('!' === $alias[strlen($alias) - 1]) {
102+
return Type::nonNull($this->string2Type(substr($alias, 0, -1)));
103+
}
104+
// List
105+
if ($this->hasNeedListOfWrapper($alias)) {
106+
return Type::listOf($this->string2Type(substr($alias, 1, -1)));
107+
}
108+
109+
return false;
110+
}
111+
112+
private function hasNeedListOfWrapper($alias)
113+
{
114+
if ('[' === $alias[0]) {
115+
$got = $alias[strlen($alias) - 1];
116+
if (']' !== $got) {
117+
throw new UnresolvableException(
118+
sprintf('Malformed ListOf wrapper type "%s" expected "]" but got .', $alias, json_encode($got))
119+
);
120+
}
121+
122+
return true;
123+
}
124+
125+
return false;
126+
}
127+
101128
protected function supportedSolutionClass()
102129
{
103130
return 'GraphQL\\Type\\Definition\\Type';

Tests/Resolver/ResolverTest.php

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,6 @@
1616

1717
class ResolverTest extends \PHPUnit_Framework_TestCase
1818
{
19-
private $privatePropertyWithoutGetter = 'ImNotAccessibleFromOutside:D';
20-
21-
private $privatePropertyWithGetter = 'IfYouWantMeUseMyGetter';
22-
23-
private $private_property_with_getter2 = 'IfYouWantMeUseMyGetter2';
24-
25-
public $name = 'public';
26-
27-
public $closureProperty;
28-
29-
public function setUp()
30-
{
31-
$this->closureProperty = function () {
32-
return $this->privatePropertyWithoutGetter;
33-
};
34-
}
35-
3619
/**
3720
* @param $fieldName
3821
* @param $source
@@ -49,30 +32,17 @@ public function testDefaultResolveFn($fieldName, $source, $expected)
4932

5033
public function resolverProvider()
5134
{
35+
$object = new Toto();
36+
5237
return [
5338
['key', ['key' => 'toto'], 'toto'],
5439
['fake', ['coco'], null],
55-
['privatePropertyWithoutGetter', $this, null],
56-
['privatePropertyWithGetter', $this, $this->privatePropertyWithGetter],
57-
['private_property_with_getter2', $this, $this->private_property_with_getter2],
40+
['privatePropertyWithoutGetter', $object, null],
41+
['privatePropertyWithGetter', $object, Toto::PRIVATE_PROPERTY_WITH_GETTER_VALUE],
42+
['private_property_with_getter2', $object, Toto::PRIVATE_PROPERTY_WITH_GETTER2_VALUE],
5843
['not_object_or_array', 'String', null],
59-
['name', $this, $this->name],
44+
['name', $object, $object->name],
6045
];
6146
}
6247

63-
/**
64-
* @return string
65-
*/
66-
public function getPrivatePropertyWithGetter()
67-
{
68-
return $this->privatePropertyWithGetter;
69-
}
70-
71-
/**
72-
* @return string
73-
*/
74-
public function getPrivatePropertyWithGetter2()
75-
{
76-
return $this->private_property_with_getter2;
77-
}
7848
}

Tests/Resolver/Toto.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@
1313

1414
class Toto
1515
{
16+
const PRIVATE_PROPERTY_WITH_GETTER_VALUE = 'IfYouWantMeUseMyGetter';
17+
const PRIVATE_PROPERTY_WITH_GETTER2_VALUE = 'IfYouWantMeUseMyGetter2';
18+
19+
private $privatePropertyWithoutGetter = 'ImNotAccessibleFromOutside:D';
20+
private $privatePropertyWithGetter = self::PRIVATE_PROPERTY_WITH_GETTER_VALUE;
21+
private $private_property_with_getter2 = self::PRIVATE_PROPERTY_WITH_GETTER2_VALUE;
22+
public $name = 'public';
23+
24+
/**
25+
* @return string
26+
*/
27+
public function getPrivatePropertyWithGetter()
28+
{
29+
return $this->privatePropertyWithGetter;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getPrivatePropertyWithGetter2()
36+
{
37+
return $this->private_property_with_getter2;
38+
}
39+
1640
public function resolve()
1741
{
1842
return func_get_args();

0 commit comments

Comments
 (0)