Skip to content

Commit

Permalink
Post-merge conflict cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
andris-sevcenko committed Oct 16, 2019
1 parent 10a312a commit 819c30a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/controllers/GraphqlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
74 changes: 74 additions & 0 deletions src/services/Gql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 819c30a

Please sign in to comment.