1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Execution; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\Cache; |
6
|
|
|
use Drupal\Core\Cache\CacheableDependencyInterface; |
7
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
8
|
|
|
use Drupal\Core\Cache\CacheBackendInterface; |
9
|
|
|
use Drupal\Core\Cache\Context\CacheContextsManager; |
10
|
|
|
use Drupal\Core\Session\AccountProxyInterface; |
11
|
|
|
use Drupal\graphql\GraphQL\Execution\Visitor\CacheMetadataVisitor; |
12
|
|
|
use Drupal\graphql\GraphQL\Execution\Visitor\MaxComplexityVisitor; |
13
|
|
|
use Drupal\graphql\GraphQL\Schema\SchemaLoader; |
14
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
15
|
|
|
use Youshido\GraphQL\Exception\ResolveException; |
16
|
|
|
|
17
|
|
|
class QueryProcessor { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The current user account. |
21
|
|
|
* |
22
|
|
|
* @var \Drupal\Core\Session\AccountProxyInterface |
23
|
|
|
*/ |
24
|
|
|
protected $currentUser; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The schema loader service. |
28
|
|
|
* |
29
|
|
|
* @var \Drupal\graphql\GraphQL\Schema\SchemaLoader |
30
|
|
|
*/ |
31
|
|
|
protected $schemaLoader; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The cache contexts manager service. |
35
|
|
|
* |
36
|
|
|
* @var \Drupal\Core\Cache\Context\CacheContextsManager |
37
|
|
|
*/ |
38
|
|
|
protected $contextsManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The cache backend for caching responses. |
42
|
|
|
* |
43
|
|
|
* @var \Drupal\Core\Cache\CacheBackendInterface |
44
|
|
|
*/ |
45
|
|
|
protected $cacheBackend; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The request stack. |
49
|
|
|
* |
50
|
|
|
* @var \Symfony\Component\HttpFoundation\RequestStack |
51
|
|
|
*/ |
52
|
|
|
protected $requestStack; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* QueryProcessor constructor. |
56
|
|
|
* |
57
|
|
|
* @param \Drupal\graphql\GraphQL\Schema\SchemaLoader $schemaLoader |
58
|
|
|
* The schema loader service. |
59
|
|
|
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
60
|
|
|
* The current user. |
61
|
|
|
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
62
|
|
|
* @param \Drupal\Core\Cache\Context\CacheContextsManager $contextsManager |
63
|
|
|
* The cache contexts manager service. |
64
|
|
|
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend |
65
|
|
|
* The cache backend for caching response. |
66
|
|
|
*/ |
67
|
|
|
public function __construct( |
68
|
|
|
SchemaLoader $schemaLoader, |
69
|
|
|
AccountProxyInterface $currentUser, |
70
|
|
|
RequestStack $requestStack, |
71
|
|
|
CacheBackendInterface $cacheBackend, |
72
|
|
|
CacheContextsManager $contextsManager |
73
|
|
|
) { |
74
|
|
|
$this->currentUser = $currentUser; |
75
|
|
|
$this->schemaLoader = $schemaLoader; |
76
|
|
|
$this->requestStack = $requestStack; |
77
|
|
|
$this->cacheBackend = $cacheBackend; |
78
|
|
|
$this->contextsManager = $contextsManager; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Processes a graphql query. |
83
|
|
|
* |
84
|
|
|
* @param string $id |
85
|
|
|
* The name of the schema to process the query against. |
86
|
|
|
* @param string $query |
87
|
|
|
* The GraphQL query. |
88
|
|
|
* @param array $variables |
89
|
|
|
* The query variables. |
90
|
|
|
* @param bool $useCache |
91
|
|
|
* Whether to use caching. |
92
|
|
|
* @param int|null $maxComplexity |
93
|
|
|
* The maximum complexity of the query or NULL if any complexity is allowed. |
94
|
|
|
* @param bool $bypassSecurity |
95
|
|
|
* Bypass field security |
96
|
|
|
* |
97
|
|
|
* @return \Drupal\graphql\GraphQL\Execution\QueryResult . |
98
|
|
|
* The query result. |
99
|
|
|
*/ |
100
|
|
|
public function processQuery($id, $query, array $variables = [], $useCache = TRUE, $maxComplexity = NULL, $bypassSecurity = FALSE) { |
101
|
|
|
if (!$schema = $this->schemaLoader->getSchema($id)) { |
102
|
|
|
throw new \InvalidArgumentException(sprintf('Could not load schema %s', [$id])); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// The processor isolates the parsing and execution of the query. |
106
|
|
|
$processor = new Processor($schema, $query, $variables); |
107
|
|
|
$context = $processor->getExecutionContext(); |
108
|
|
|
$container = $context->getContainer(); |
109
|
|
|
|
110
|
|
|
// Set up some parameters in the container. |
111
|
|
|
$secure = $bypassSecurity || $this->currentUser->hasPermission('bypass graphql field security'); |
112
|
|
|
$metadata = new CacheableMetadata(); |
113
|
|
|
$container->set('secure', $secure); |
114
|
|
|
$container->set('metadata', $metadata); |
115
|
|
|
|
116
|
|
|
$visitor = new CacheMetadataVisitor(); |
117
|
|
|
/** @var \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata */ |
118
|
|
|
$metadata = $processor->reduceRequest($visitor, function ($result) { |
119
|
|
|
$metadata = new CacheableMetadata(); |
120
|
|
|
$metadata->addCacheableDependency($result); |
121
|
|
|
// TODO: Use a cache context that includes the AST instead of the raw values. |
122
|
|
|
$metadata->addCacheContexts(['gql']); |
123
|
|
|
$metadata->addCacheTags(['graphql_response']); |
124
|
|
|
|
125
|
|
|
return $metadata; |
126
|
|
|
}) ?: new CacheableMetadata(); |
127
|
|
|
|
128
|
|
|
// The cache identifier will later be re-used for writing the cache entry. |
129
|
|
|
$cid = $this->getCacheIdentifier($metadata); |
130
|
|
|
if (!empty($useCache) && $metadata->getCacheMaxAge() !== 0) { |
131
|
|
|
if (($cache = $this->cacheBackend->get($cid)) && $result = $cache->data) { |
132
|
|
|
return $result; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if (!empty($maxComplexity)) { |
137
|
|
|
$visitor = new MaxComplexityVisitor(); |
138
|
|
|
$processor->reduceRequest($visitor, function ($result) use ($maxComplexity) { |
139
|
|
|
if ($result > $maxComplexity) { |
140
|
|
|
throw new ResolveException('Maximum complexity exceeded.'); |
141
|
|
|
} |
142
|
|
|
}); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Retrieve the result from the processor. |
146
|
|
|
$data = $processor->resolveRequest(); |
147
|
|
|
|
148
|
|
|
// Add collected cache metadata from the query processor. |
149
|
|
|
if ($container->has('metadata') && ($collected = $container->get('metadata'))) { |
150
|
|
|
if ($collected instanceof CacheableDependencyInterface) { |
|
|
|
|
151
|
|
|
$metadata->addCacheableDependency($collected); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// If we encountered any errors, do not cache anything. |
156
|
|
|
if ($context->hasErrors()) { |
157
|
|
|
$metadata->setCacheMaxAge(0); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Build the result object. The keys are only set if they contain data. |
161
|
|
|
$result = new QueryResult(array_filter([ |
162
|
|
|
'data' => $data, |
163
|
|
|
'errors' => $processor->getExecutionContext()->getErrorsArray(), |
164
|
|
|
]), $metadata); |
165
|
|
|
|
166
|
|
|
// Write the query result into the cache if the cache metadata permits. |
167
|
|
|
if (!empty($useCache) && $metadata->getCacheMaxAge() !== 0) { |
168
|
|
|
$tags = $metadata->getCacheTags(); |
169
|
|
|
$expire = $this->maxAgeToExpire($metadata->getCacheMaxAge()); |
170
|
|
|
|
171
|
|
|
// The cache identifier for the cache entry is built based on the |
172
|
|
|
// previously extracted cache contexts from the query visitor. That |
173
|
|
|
// means, that dynamically returned cache contexts have no effect. |
174
|
|
|
$this->cacheBackend->set($cid, $result, $expire, $tags); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return $result; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Maps a max age value to an "expire" value for the Cache API. |
182
|
|
|
* |
183
|
|
|
* @param int $maxAge |
184
|
|
|
* A max age value. |
185
|
|
|
* |
186
|
|
|
* @return int |
187
|
|
|
* A corresponding "expire" value. |
188
|
|
|
* |
189
|
|
|
* @see \Drupal\Core\Cache\CacheBackendInterface::set() |
190
|
|
|
*/ |
191
|
|
View Code Duplication |
protected function maxAgeToExpire($maxAge) { |
|
|
|
|
192
|
|
|
if ($maxAge === Cache::PERMANENT) { |
193
|
|
|
return Cache::PERMANENT; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return (int) $this->requestStack->getMasterRequest()->server->get('REQUEST_TIME') + $maxAge; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Generates a cache identifier for the passed cache contexts. |
201
|
|
|
* |
202
|
|
|
* @param \Drupal\Core\Cache\CacheableDependencyInterface $metadata |
203
|
|
|
* Optional array of cache context tokens. |
204
|
|
|
* |
205
|
|
|
* @return string The generated cache identifier. |
206
|
|
|
* The generated cache identifier. |
207
|
|
|
*/ |
208
|
|
|
protected function getCacheIdentifier(CacheableDependencyInterface $metadata) { |
209
|
|
|
$tokens = $metadata->getCacheContexts(); |
210
|
|
|
$keys = $this->contextsManager->convertTokensToKeys($tokens)->getKeys(); |
211
|
|
|
return implode(':', array_merge(['graphql'], array_values($keys))); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.