|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Execution; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\CacheableDependencyInterface; |
|
6
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
|
7
|
|
|
use Drupal\graphql\GraphQL\SecureFieldInterface; |
|
8
|
|
|
use Drupal\graphql\GraphQL\ValueWrapperInterface; |
|
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
11
|
|
|
use Youshido\GraphQL\Execution\DeferredResolverInterface; |
|
12
|
|
|
use Youshido\GraphQL\Execution\DeferredResult; |
|
13
|
|
|
use Youshido\GraphQL\Execution\Processor as BaseProcessor; |
|
14
|
|
|
use Youshido\GraphQL\Field\FieldInterface; |
|
15
|
|
|
use Youshido\GraphQL\Parser\Ast\Interfaces\FieldInterface as AstFieldInterface; |
|
16
|
|
|
use Youshido\GraphQL\Parser\Ast\Query as AstQuery; |
|
17
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
|
18
|
|
|
|
|
19
|
|
|
class Processor extends BaseProcessor { |
|
20
|
|
|
/** |
|
21
|
|
|
* The dependency injection container. |
|
22
|
|
|
* |
|
23
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $container; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructs a Processor object. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container |
|
31
|
|
|
* The dependency injection container. |
|
32
|
|
|
* @param \Youshido\GraphQL\Schema\AbstractSchema $schema |
|
33
|
|
|
* The GraphQL schema. |
|
34
|
|
|
* @param boolean $secure |
|
35
|
|
|
* Indicate that this processor is executing trusted queries. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct(ContainerInterface $container, AbstractSchema $schema, $secure = FALSE) { |
|
38
|
|
|
parent::__construct($schema); |
|
|
|
|
|
|
39
|
|
|
$this->container = $container; |
|
40
|
|
|
|
|
41
|
|
|
$contexts = $this->executionContext->getContainer(); |
|
42
|
|
|
$contexts->set('secure', $secure); |
|
43
|
|
|
$contexts->set('metadata', new CacheableMetadata()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Return the collected cache metadata from the query. |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Drupal\Core\Cache\CacheableMetadata |
|
50
|
|
|
* The collected cache metadata from the query. |
|
51
|
|
|
*/ |
|
52
|
|
|
public function getQueryCacheMetadata() { |
|
53
|
|
|
$metadata = new CacheableMetadata(); |
|
54
|
|
|
|
|
55
|
|
|
$contexts = $this->executionContext->getContainer(); |
|
56
|
|
|
if ($contexts->has('metadata')) { |
|
57
|
|
|
$metadata->addCacheableDependency($contexts->get('metadata')); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Prevent caching if this is a mutation query. |
|
61
|
|
|
if (($request = $this->executionContext->getRequest()) && $request->hasMutations()) { |
|
62
|
|
|
$metadata->setCacheMaxAge(0); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $metadata; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = NULL) { |
|
72
|
|
|
$contexts = $this->executionContext->getContainer(); |
|
73
|
|
|
|
|
74
|
|
|
if ($field instanceof SecureFieldInterface) { |
|
75
|
|
|
// If not resolving in a trusted environment, check if the field is secure. |
|
76
|
|
|
if ($contexts->has('secure') && !$contexts->get('secure') && !$field->isSecure()) { |
|
77
|
|
|
throw new \Exception(sprintf("Unable to resolve insecure field '%s' (%s).", $field->getName(), get_class($field))); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$value = $this->doResolveValue($field, $ast, $parentValue); |
|
82
|
|
|
if ($value instanceof CacheableDependencyInterface && $contexts->has('metadata')) { |
|
|
|
|
|
|
83
|
|
|
// If the current resolved value returns cache metadata, keep it. |
|
84
|
|
|
$contexts->get('metadata')->addCacheableDependency($value); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// If it's a value wrapper, extract the real value to return. |
|
88
|
|
|
if ($value instanceof ValueWrapperInterface) { |
|
89
|
|
|
$value = $value->getValue(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $value; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Override deferred resolving to use our own DeferredResult class. |
|
97
|
|
|
* |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function deferredResolve($resolvedValue, callable $callback) { |
|
101
|
|
|
if ($resolvedValue instanceof DeferredResolverInterface) { |
|
|
|
|
|
|
102
|
|
|
$deferredResult = new DeferredResult($resolvedValue, function($result) use ($callback) { |
|
103
|
|
|
$contexts = $this->executionContext->getContainer(); |
|
104
|
|
|
|
|
105
|
|
|
if ($result instanceof CacheableDependencyInterface && $contexts->has('metadata')) { |
|
|
|
|
|
|
106
|
|
|
$contexts->get('metadata')->addCacheableDependency($result); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if ($result instanceof ValueWrapperInterface) { |
|
110
|
|
|
$result = $result->getValue(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return call_user_func($callback, $result); |
|
114
|
|
|
}); |
|
115
|
|
|
|
|
116
|
|
|
// Whenever we stumble upon a deferred resolver, append it to the |
|
117
|
|
|
// queue to be resolved later. |
|
118
|
|
|
$this->deferredResults[] = $deferredResult; |
|
|
|
|
|
|
119
|
|
|
return $deferredResult; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// For simple values, invoke the callback immediately. |
|
123
|
|
|
return $callback($resolvedValue); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Helper function to resolve a field value. |
|
128
|
|
|
* |
|
129
|
|
|
* @param \Youshido\GraphQL\Field\FieldInterface $field |
|
130
|
|
|
* @param \Youshido\GraphQL\Parser\Ast\Interfaces\FieldInterface $ast |
|
131
|
|
|
* @param null $parentValue |
|
132
|
|
|
* |
|
133
|
|
|
* @return mixed|null |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function doResolveValue(FieldInterface $field, AstFieldInterface $ast, $parentValue = NULL) { |
|
136
|
|
|
$arguments = $this->parseArgumentsValues($field, $ast); |
|
137
|
|
|
$astFields = $ast instanceof AstQuery ? $ast->getFields() : []; |
|
138
|
|
|
$resolveInfo = $this->createResolveInfo($field, $astFields); |
|
139
|
|
|
|
|
140
|
|
|
if ($field instanceof ContainerAwareInterface) { |
|
|
|
|
|
|
141
|
|
|
$field->setContainer($this->container); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $field->resolve($parentValue, $arguments, $resolveInfo); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: