From 819c30a8b15fab96a6bb4c27e711d035244bb8e6 Mon Sep 17 00:00:00 2001 From: andris-sevcenko Date: Wed, 16 Oct 2019 20:18:48 +0300 Subject: [PATCH] Post-merge conflict cleanup. --- src/controllers/GraphqlController.php | 2 +- src/services/Gql.php | 74 +++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/controllers/GraphqlController.php b/src/controllers/GraphqlController.php index 7d8d44d8f71..708f1c97747 100644 --- a/src/controllers/GraphqlController.php +++ b/src/controllers/GraphqlController.php @@ -143,7 +143,7 @@ public function actionApi(): Response try { $schemaDef = $gqlService->getSchemaDef($schema, $devMode || StringHelper::contains($query, '__schema')); - $result = $gqlService->executeQuery($schemaDef, $query, $variables, $operationName); + $result = $gqlService->executeQuery($schemaDef, $query, $variables, $operationName, $devMode); } catch (\Throwable $e) { Craft::$app->getErrorHandler()->logException($e); diff --git a/src/services/Gql.php b/src/services/Gql.php index 0332d84f7eb..9049b4d5bbb 100644 --- a/src/services/Gql.php +++ b/src/services/Gql.php @@ -12,6 +12,7 @@ use craft\db\Table; use craft\errors\GqlException; use craft\events\DefineGqlValidationRulesEvent; +use craft\events\ExecuteGqlQueryEvent; use craft\events\RegisterGqlDirectivesEvent; use craft\events\RegisterGqlPermissionsEvent; use craft\events\RegisterGqlQueriesEvent; @@ -158,6 +159,49 @@ class Gql extends Component */ const EVENT_DEFINE_GQL_VALIDATION_RULES = 'defineGqlValidationRules'; + /** + * @event ExecuteGqlQueryEvent The event that is triggered before executing the GraphQL query. + * + * Plugins get a chance to modify the query or return a cached response. + * + * --- + * ```php + * use craft\events\ExecuteGqlQueryEvent; + * use craft\services\GraphQl; + * use yii\base\Event; + * + * Event::on(Gql::class, + * Gql::EVENT_BEFORE_EXECUTE_GQL_QUERY, + * function(ExecuteGqlQueryEvent $event) { + * // Set the result from cache + * $event->result = ...; + * } + * ); + * ``` + */ + const EVENT_BEFORE_EXECUTE_GQL_QUERY = 'beforeExecuteGqlQuery'; + + /** + * @event ExecuteGqlQueryEvent The event that is triggered after executing the GraphQL query. + * + * Plugins get a chance to do sometheing after a performed GraphQL query. + * + * --- + * ```php + * use craft\events\ExecuteGqlQueryEvent; + * use craft\services\GraphQl; + * use yii\base\Event; + * + * Event::on(Gql::class, + * Gql::EVENT_AFTER_EXECUTE_GQL_QUERY, + * function(ExecuteGqlQueryEvent $event) { + * // Cache the results from $event->result or just tweak them + * } + * ); + * ``` + */ + const EVENT_AFTER_EXECUTE_GQL_QUERY = 'afterExecuteGqlQuery'; + /** * Currently loaded schema definition * @@ -272,6 +316,36 @@ public function getValidationRules($debug = false) { return array_values($event->validationRules); } + /** + * Execute a GraphQL query for a given schema definition. + * + * @param Schema $schema The schema definition to use. + * @param string $query The query string to execute. + * @param array|null $variables The variables to use. + * @param string|null $operationName The operation name. + * @param bool $debugMode Whether debug mode validations rules should be used for GraphQL. + * @return array + */ + public function executeQuery(Schema $schemaDef, string $query, $variables, $operationName, $debugMode = false): array + { + $event = new ExecuteGqlQueryEvent([ + 'schemaDef' => $schemaDef, + 'query' => $query, + 'variables' => $variables, + 'operationName' => $operationName, + ]); + + $this->trigger(self::EVENT_BEFORE_EXECUTE_GQL_QUERY, $event); + + if ($event->result === null) { + $event->result = GraphQL::executeQuery($schemaDef, $query, $event->rootValue, $event->context, $variables, $operationName, null, $this->getValidationRules($debugMode))->toArray(true); + } + + $this->trigger(self::EVENT_AFTER_EXECUTE_GQL_QUERY, $event); + + return $event->result; + } + /** * Returns the active GraphQL schema. *