Skip to content

Commit caa950b

Browse files
committed
added error handling from default rules validation.
1 parent 18e656b commit caa950b

File tree

1 file changed

+32
-43
lines changed

1 file changed

+32
-43
lines changed

Diff for: src/GraphQL/Execution/APQQueryProcessor.php

+32-43
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
namespace Drupal\graphql_apq\GraphQL\Execution;
44

5-
use Drupal\graphql\GraphQL\Execution\QueryProcessor;
6-
use GraphQL\Language\AST\DocumentNode;
5+
use Drupal\Core\Cache\Cache;
76
use GraphQL\Language\Parser;
8-
use GraphQL\Server\OperationParams;
97
use GraphQL\Server\ServerConfig;
10-
use Drupal\Core\Cache\Cache;
8+
use GraphQL\Server\OperationParams;
9+
use GraphQL\Language\AST\DocumentNode;
10+
use Drupal\graphql\GraphQL\Execution\QueryResult;
11+
use Drupal\graphql\GraphQL\Execution\QueryProcessor;
12+
use GraphQL\Validator\DocumentValidator;
1113

1214
class APQQueryProcessor extends QueryProcessor {
1315

@@ -19,7 +21,16 @@ public function processQuery($schema, $params) {
1921
$plugin = $this->pluginManager->createInstance($schema);
2022
$config = $plugin->getServer();
2123

22-
$this->storeAPQQuery($config, $params);
24+
// Store query when present.
25+
if (!empty($params->getOriginalInput('query'))) {
26+
// Validate query before storing.
27+
$errors = $this->validateAPQQuery($config, $params);
28+
if (is_array($errors)) {
29+
return new QueryResult(NULL, $errors);
30+
}
31+
32+
$this->storeAPQQuery($params);
33+
}
2334

2435
if (is_array($params)) {
2536
return $this->executeBatch($config, $params);
@@ -31,23 +42,12 @@ public function processQuery($schema, $params) {
3142
/**
3243
* Store APQ Query.
3344
*
34-
* @param \GraphQL\Server\ServerConfig $config
3545
* @param \GraphQL\Server\OperationParams $params
3646
*
3747
* @throws \GraphQL\Error\SyntaxError
3848
* @throws \GraphQL\Server\RequestError
3949
*/
40-
private function storeAPQQuery(ServerConfig $config, OperationParams $params) {
41-
// Request without query.
42-
if (empty($params->getOriginalInput('query'))) {
43-
return;
44-
}
45-
46-
// Query is invalid.
47-
if (!$this->validateAPQQuery($config, $params)) {
48-
return;
49-
}
50-
50+
private function storeAPQQuery(OperationParams $params) {
5151
$persistedQuery = $this->persistedQuery($params);
5252
$storage = \Drupal::entityTypeManager()->getStorage('apq_query_map');
5353

@@ -81,38 +81,27 @@ private function storeAPQQuery(ServerConfig $config, OperationParams $params) {
8181
* @param \GraphQL\Server\ServerConfig $config
8282
* @param \GraphQL\Server\OperationParams $params
8383
*
84-
* @return bool
84+
* @return bool|array
8585
* @throws \GraphQL\Error\SyntaxError
8686
* @throws \GraphQL\Server\RequestError
8787
*/
88-
private function validateAPQQuery(ServerConfig $config, OperationParams $params): bool {
88+
private function validateAPQQuery(ServerConfig $config, OperationParams $params) {
8989
$document = $this->getDocumentFromQuery($config, $params);
90-
return $this->operationParamsValid($params) && $this->operationValid($config, $params, $document);
91-
}
9290

93-
/**
94-
* Check if operation params are valid.
95-
*
96-
* @param \GraphQL\Server\OperationParams $params
97-
*
98-
* @return bool
99-
*/
100-
private function operationParamsValid(OperationParams $params) {
101-
return count($this->validateOperationParams($params)) === 0;
102-
}
91+
// Add default validation rules.
92+
$config->setValidationRules(DocumentValidator::defaultRules());
10393

104-
/**
105-
* Check if operation is valid.
106-
*
107-
* @param \GraphQL\Server\ServerConfig $config
108-
* @param \GraphQL\Server\OperationParams $params
109-
* @param \GraphQL\Language\AST\DocumentNode $document
110-
*
111-
* @return bool
112-
* @throws \Exception
113-
*/
114-
private function operationValid(ServerConfig $config, OperationParams $params, DocumentNode $document) {
115-
return count($this->validateOperation($config, $params, $document)) === 0;
94+
$paramsErrors = $this->validateOperationParams($params);
95+
if (!empty($paramsErrors)) {
96+
return $paramsErrors;
97+
}
98+
99+
$operationErrors = $this->validateOperation($config, $params, $document);
100+
if (!empty($operationErrors)) {
101+
return $operationErrors;
102+
}
103+
104+
return TRUE;
116105
}
117106

118107
/**

0 commit comments

Comments
 (0)