-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathProcessor.php
More file actions
163 lines (133 loc) · 5.92 KB
/
Processor.php
File metadata and controls
163 lines (133 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace Youshido\GraphQLBundle\Execution;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\HttpKernel\Kernel;
use Youshido\GraphQL\Execution\Context\ExecutionContextInterface;
use Youshido\GraphQL\Execution\Processor as BaseProcessor;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQL\Field\AbstractField;
use Youshido\GraphQL\Field\Field;
use Youshido\GraphQL\Field\FieldInterface;
use Youshido\GraphQL\Parser\Ast\Field as AstField;
use Youshido\GraphQL\Parser\Ast\Interfaces\FieldInterface as AstFieldInterface;
use Youshido\GraphQL\Parser\Ast\Query;
use Youshido\GraphQL\Parser\Ast\Query as AstQuery;
use Youshido\GraphQL\Type\TypeService;
use Youshido\GraphQL\Exception\ResolveException;
use Youshido\GraphQLBundle\Event\ResolveEvent;
use Youshido\GraphQLBundle\Security\Manager\SecurityManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class Processor extends BaseProcessor
{
/** @var LoggerInterface */
private $logger;
/** @var SecurityManagerInterface */
private $securityManager;
/** @var EventDispatcherInterface */
private $eventDispatcher;
/**
* Constructor.
*
* @param ExecutionContextInterface $executionContext
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(ExecutionContextInterface $executionContext, EventDispatcherInterface $eventDispatcher)
{
$this->executionContext = $executionContext;
$this->eventDispatcher = $eventDispatcher;
parent::__construct($executionContext->getSchema());
}
/**
* @param SecurityManagerInterface $securityManger
*
* @return Processor
*/
public function setSecurityManager(SecurityManagerInterface $securityManger)
{
$this->securityManager = $securityManger;
return $this;
}
public function processPayload($payload, $variables = [], $reducers = [])
{
if ($this->logger) {
$this->logger->debug(sprintf('GraphQL query: %s', $payload), (array)$variables);
}
parent::processPayload($payload, $variables);
}
protected function resolveQuery(Query $query)
{
$this->assertClientHasOperationAccess($query);
return parent::resolveQuery($query);
}
private function dispatchResolveEvent(ResolveEvent $event, $name){
$major = Kernel::MAJOR_VERSION;
$minor = Kernel::MINOR_VERSION;
if($major > 4 || ($major === 4 && $minor >= 3)){
$this->eventDispatcher->dispatch($event, $name);
}else{
$this->eventDispatcher->dispatch($name, $event);
}
}
protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = null)
{
/** @var AstQuery|AstField $ast */
$arguments = $this->parseArgumentsValues($field, $ast);
$astFields = $ast instanceof AstQuery ? $ast->getFields() : [];
$event = new ResolveEvent($field, $astFields);
$this->dispatchResolveEvent($event, 'graphql.pre_resolve');
$resolveInfo = $this->createResolveInfo($field, $astFields);
$this->assertClientHasFieldAccess($resolveInfo);
if (in_array('Symfony\Component\DependencyInjection\ContainerAwareInterface', class_implements($field))) {
/** @var $field ContainerAwareInterface */
$field->setContainer($this->executionContext->getContainer()->getSymfonyContainer());
}
if (($field instanceof AbstractField) && ($resolveFunc = $field->getConfig()->getResolveFunction())) {
if ($this->isServiceReference($resolveFunc)) {
$service = substr($resolveFunc[0], 1);
$method = $resolveFunc[1];
if (!$this->executionContext->getContainer()->has($service)) {
throw new ResolveException(sprintf('Resolve service "%s" not found for field "%s"', $service, $field->getName()));
}
$serviceInstance = $this->executionContext->getContainer()->get($service);
if (!method_exists($serviceInstance, $method)) {
throw new ResolveException(sprintf('Resolve method "%s" not found in "%s" service for field "%s"', $method, $service, $field->getName()));
}
$result = $serviceInstance->$method($parentValue, $arguments, $resolveInfo);
} else {
$result = $resolveFunc($parentValue, $arguments, $resolveInfo);
}
} elseif ($field instanceof Field) {
$result = TypeService::getPropertyValue($parentValue, $field->getName());
} else {
$result = $field->resolve($parentValue, $arguments, $resolveInfo);
}
$event = new ResolveEvent($field, $astFields, $result);
$this->dispatchResolveEvent($event, 'graphql.post_resolve');
return $event->getResolvedValue();
}
private function assertClientHasOperationAccess(Query $query)
{
if ($this->securityManager->isSecurityEnabledFor(SecurityManagerInterface::RESOLVE_ROOT_OPERATION_ATTRIBUTE)
&& !$this->securityManager->isGrantedToOperationResolve($query)
) {
throw $this->securityManager->createNewOperationAccessDeniedException($query);
}
}
private function assertClientHasFieldAccess(ResolveInfo $resolveInfo)
{
if ($this->securityManager->isSecurityEnabledFor(SecurityManagerInterface::RESOLVE_FIELD_ATTRIBUTE)
&& !$this->securityManager->isGrantedToFieldResolve($resolveInfo)
) {
throw $this->securityManager->createNewFieldAccessDeniedException($resolveInfo);
}
}
private function isServiceReference($resolveFunc)
{
return is_array($resolveFunc) && count($resolveFunc) == 2 && strpos($resolveFunc[0], '@') === 0;
}
public function setLogger($logger = null)
{
$this->logger = $logger;
}
}