1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Resolver; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Collections\Collection; |
14
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareTrait; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
19
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
20
|
|
|
use Ynlo\GraphQLBundle\Definition\FieldsAwareDefinitionInterface; |
21
|
|
|
use Ynlo\GraphQLBundle\Definition\QueryDefinition; |
22
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionManager; |
23
|
|
|
use Ynlo\GraphQLBundle\Model\ID; |
24
|
|
|
use Ynlo\GraphQLBundle\Type\DefinitionManagerAwareInterface; |
25
|
|
|
use Ynlo\GraphQLBundle\Type\DefinitionManagerAwareTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Default resolver for all object fields |
29
|
|
|
*/ |
30
|
|
|
class ObjectFieldResolver implements ContainerAwareInterface, DefinitionManagerAwareInterface |
31
|
|
|
{ |
32
|
|
|
use ContainerAwareTrait; |
33
|
|
|
use DefinitionManagerAwareTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var FieldsAwareDefinitionInterface |
37
|
|
|
*/ |
38
|
|
|
protected $definition; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* ObjectFieldResolver constructor. |
42
|
|
|
* |
43
|
|
|
* @param ContainerInterface $container |
44
|
|
|
* @param DefinitionManager $definitionManager |
45
|
|
|
* @param FieldsAwareDefinitionInterface $definition |
46
|
|
|
*/ |
47
|
14 |
|
public function __construct(ContainerInterface $container, DefinitionManager $definitionManager, FieldsAwareDefinitionInterface $definition) |
48
|
|
|
{ |
49
|
14 |
|
$this->definition = $definition; |
50
|
14 |
|
$this->container = $container; |
51
|
14 |
|
$this->manager = $definitionManager; |
52
|
14 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param mixed $root |
56
|
|
|
* @param array $args |
57
|
|
|
* @param mixed $context |
58
|
|
|
* @param ResolveInfo $info |
59
|
|
|
* |
60
|
|
|
* @return mixed|null|string |
61
|
|
|
*/ |
62
|
14 |
|
public function __invoke($root, array $args, $context, ResolveInfo $info) |
63
|
|
|
{ |
64
|
14 |
|
$value = null; |
65
|
14 |
|
$fieldDefinition = $this->definition->getField($info->fieldName); |
66
|
|
|
|
67
|
|
|
//when use external resolver or use a object method with arguments |
68
|
14 |
|
if ($fieldDefinition->getResolver() || $fieldDefinition->getArguments()) { |
|
|
|
|
69
|
1 |
|
$queryDefinition = new QueryDefinition(); |
70
|
1 |
|
$queryDefinition->setName($fieldDefinition->getName()); |
71
|
1 |
|
$queryDefinition->setType($fieldDefinition->getType()); |
72
|
1 |
|
$queryDefinition->setArguments($fieldDefinition->getArguments()); |
73
|
1 |
|
$queryDefinition->setList($fieldDefinition->isList()); |
74
|
1 |
|
$queryDefinition->setMetas($fieldDefinition->getMetas()); |
75
|
|
|
|
76
|
1 |
|
if (!$fieldDefinition->getResolver()) { |
77
|
|
|
if ($fieldDefinition->getOriginType() === \ReflectionMethod::class) { |
78
|
|
|
$queryDefinition->setResolver($fieldDefinition->getOriginName()); |
79
|
|
|
} |
80
|
|
|
} else { |
81
|
1 |
|
$queryDefinition->setResolver($fieldDefinition->getResolver()); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
$resolver = new ResolverExecutor($this->container, $this->manager, $queryDefinition); |
85
|
1 |
|
$value = $resolver($root, $args, $context, $info); |
86
|
|
|
} else { |
87
|
14 |
|
$accessor = new PropertyAccessor(true); |
88
|
14 |
|
$originName = $fieldDefinition->getOriginName(); |
89
|
14 |
|
$value = $accessor->getValue($root, $originName); |
90
|
|
|
} |
91
|
|
|
|
92
|
14 |
|
if (null !== $value && Type::ID === $fieldDefinition->getType()) { |
93
|
|
|
//ID are formed with base64 representation of the Types and real database ID |
94
|
|
|
//in order to create a unique and global identifier for each resource |
95
|
|
|
//@see https://facebook.github.io/relay/docs/graphql-object-identification.html |
96
|
12 |
|
if ($value instanceof ID) { |
97
|
2 |
|
$value = (string) $value; |
98
|
|
|
} else { |
99
|
11 |
|
$value = (string) new ID($this->definition->getName(), $value); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
14 |
|
if ($value instanceof Collection) { |
104
|
|
|
$value = $value->toArray(); |
105
|
|
|
} |
106
|
|
|
|
107
|
14 |
|
return $value; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: