|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\graphql\GraphQL\Execution; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Cache\Cache; |
|
6
|
|
|
use Drupal\Core\Cache\CacheableMetadata; |
|
7
|
|
|
use Drupal\Core\Session\AccountProxyInterface; |
|
8
|
|
|
use Drupal\graphql\GraphQL\Reducers\ReducerManager; |
|
9
|
|
|
use Drupal\graphql\GraphQL\Schema\SchemaLoader; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
11
|
|
|
use Youshido\GraphQL\Schema\AbstractSchema; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Drupal service for executing GraphQL queries. |
|
15
|
|
|
*/ |
|
16
|
|
|
class QueryProcessor { |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The dependency injection container. |
|
20
|
|
|
* |
|
21
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $container; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The reducer manager service. |
|
27
|
|
|
* |
|
28
|
|
|
* @var \Drupal\graphql\GraphQL\Reducers\ReducerManager |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $reducerManager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The current user account. |
|
34
|
|
|
* |
|
35
|
|
|
* @var \Drupal\Core\Session\AccountProxyInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $currentUser; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The schema loader service. |
|
41
|
|
|
* |
|
42
|
|
|
* @var \Drupal\graphql\GraphQL\Schema\SchemaLoader |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $schemaLoader; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The graphql container parameters. |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $parameters; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* QueryProcessor constructor. |
|
55
|
|
|
* |
|
56
|
|
|
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container |
|
57
|
|
|
* The dependency injection container. |
|
58
|
|
|
* @param \Drupal\graphql\GraphQL\Reducers\ReducerManager $reducerManager |
|
59
|
|
|
* The schema reducer manager service. |
|
60
|
|
|
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser |
|
61
|
|
|
* The current user. |
|
62
|
|
|
* @param \Drupal\graphql\GraphQL\Schema\SchemaLoader $schemaLoader |
|
63
|
|
|
* The schema loader service. |
|
64
|
|
|
* @param array $parameters |
|
65
|
|
|
* The graphql container parameters. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct( |
|
68
|
|
|
ContainerInterface $container, |
|
69
|
|
|
ReducerManager $reducerManager, |
|
70
|
|
|
SchemaLoader $schemaLoader, |
|
71
|
|
|
AccountProxyInterface $currentUser, |
|
72
|
|
|
array $parameters |
|
73
|
|
|
) { |
|
74
|
|
|
$this->container = $container; |
|
75
|
|
|
$this->currentUser = $currentUser; |
|
76
|
|
|
$this->parameters = $parameters; |
|
77
|
|
|
$this->reducerManager = $reducerManager; |
|
78
|
|
|
$this->schemaLoader = $schemaLoader; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Execute a GraphQL query. |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $schemaId |
|
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 $bypassSecurity |
|
91
|
|
|
* Bypass field security |
|
92
|
|
|
* |
|
93
|
|
|
* @return \Drupal\graphql\GraphQL\Execution\QueryResult The GraphQL query result. |
|
94
|
|
|
* The GraphQL query result. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function processQuery($schemaId, $query, array $variables = [], $bypassSecurity = FALSE) { |
|
97
|
|
|
if (!$schema = $this->schemaLoader->getSchema($schemaId)) { |
|
98
|
|
|
throw new \InvalidArgumentException(sprintf('Could not load schema %s', [$schemaId])); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** @var \Youshido\GraphQL\Schema\AbstractSchema $schema */ |
|
102
|
|
|
$secure = !!($bypassSecurity || $this->currentUser->hasPermission('bypass graphql field security') || $this->parameters['development']); |
|
103
|
|
|
$processor = new Processor($this->container, $schema, $secure); |
|
104
|
|
|
$processor->processPayload($query, $variables, $this->reducerManager->getAllServices()); |
|
105
|
|
|
|
|
106
|
|
|
// Add collected cache metadata from the query processor. |
|
107
|
|
|
$metadata = new CacheableMetadata(); |
|
108
|
|
|
$context = $processor->getExecutionContext(); |
|
109
|
|
|
$container = $context->getContainer(); |
|
110
|
|
|
if ($container->has('metadata')) { |
|
111
|
|
|
$metadata->addCacheableDependency($container->get('metadata')); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
// Add cache metadata from the schema in use. |
|
115
|
|
|
/** @var \Drupal\graphql\Plugin\GraphQL\SchemaPluginInterface $schema */ |
|
116
|
|
|
$metadata->addCacheableDependency($schema->getResponseCacheMetadata()); |
|
117
|
|
|
|
|
118
|
|
|
// Prevent caching if this is a mutation query. |
|
119
|
|
|
$request = $context->getRequest(); |
|
120
|
|
|
if (!empty($request) && $request->hasMutations()) { |
|
121
|
|
|
$metadata->setCacheMaxAge(0); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return new QueryResult($processor->getResponseData(), $metadata); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
} |