2
2
3
3
namespace Drupal \graphql_apq \GraphQL \Execution ;
4
4
5
- use Drupal \graphql \GraphQL \Execution \QueryProcessor ;
6
- use GraphQL \Language \AST \DocumentNode ;
5
+ use Drupal \Core \Cache \Cache ;
7
6
use GraphQL \Language \Parser ;
8
- use GraphQL \Server \OperationParams ;
9
7
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 ;
11
13
12
14
class APQQueryProcessor extends QueryProcessor {
13
15
@@ -19,7 +21,16 @@ public function processQuery($schema, $params) {
19
21
$ plugin = $ this ->pluginManager ->createInstance ($ schema );
20
22
$ config = $ plugin ->getServer ();
21
23
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
+ }
23
34
24
35
if (is_array ($ params )) {
25
36
return $ this ->executeBatch ($ config , $ params );
@@ -31,23 +42,12 @@ public function processQuery($schema, $params) {
31
42
/**
32
43
* Store APQ Query.
33
44
*
34
- * @param \GraphQL\Server\ServerConfig $config
35
45
* @param \GraphQL\Server\OperationParams $params
36
46
*
37
47
* @throws \GraphQL\Error\SyntaxError
38
48
* @throws \GraphQL\Server\RequestError
39
49
*/
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 ) {
51
51
$ persistedQuery = $ this ->persistedQuery ($ params );
52
52
$ storage = \Drupal::entityTypeManager ()->getStorage ('apq_query_map ' );
53
53
@@ -81,38 +81,27 @@ private function storeAPQQuery(ServerConfig $config, OperationParams $params) {
81
81
* @param \GraphQL\Server\ServerConfig $config
82
82
* @param \GraphQL\Server\OperationParams $params
83
83
*
84
- * @return bool
84
+ * @return bool|array
85
85
* @throws \GraphQL\Error\SyntaxError
86
86
* @throws \GraphQL\Server\RequestError
87
87
*/
88
- private function validateAPQQuery (ServerConfig $ config , OperationParams $ params ): bool {
88
+ private function validateAPQQuery (ServerConfig $ config , OperationParams $ params ) {
89
89
$ document = $ this ->getDocumentFromQuery ($ config , $ params );
90
- return $ this ->operationParamsValid ($ params ) && $ this ->operationValid ($ config , $ params , $ document );
91
- }
92
90
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 ());
103
93
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 ;
116
105
}
117
106
118
107
/**
0 commit comments