-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from pimcore/events
Add events for Query & Mutation
- Loading branch information
Showing
14 changed files
with
730 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#Events | ||
|
||
DataHub GraphQL events are based on symfony event dispatcher, which are triggered during execution of Query and Mutation requests. | ||
[Read more](https://github.com/pimcore/pimcore/blob/master/doc/Development_Documentation/20_Extending_Pimcore/11_Event_API_and_Event_Manager.md) about events on Pimcore documentation. | ||
|
||
All DataHub events are defined as a constant on component classes: | ||
- [Query](https://github.com/pimcore/data-hub/blob/master/src/Event/GraphQL\QueryEvents.php) | ||
- [Mutation](https://github.com/pimcore/data-hub/blob/master/src/Event/GraphQL\MutationEvents.php) | ||
- [Executor](https://github.com/pimcore/data-hub/blob/master/src/Event/GraphQL\ExecutorEvents.php) | ||
|
||
## Event Listener examples | ||
|
||
Add configuration in your `app/config/services.yml`: | ||
```yml | ||
services: | ||
AppBundle\EventListener\GraphQlListener: | ||
tags: | ||
- { name: kernel.event_listener} | ||
``` | ||
Add Listener class `src/AppBundle/EventListener/GraphQlListener` | ||
|
||
#### Example 1: Query & Mutation execution | ||
```php | ||
<?php | ||
namespace AppBundle\EventListener; | ||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\ExecutorEvents; | ||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorEvent; | ||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorResultEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
class GraphQlListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
ExecutorEvents::PRE_EXECUTE => 'onPreExecute', //Pre execute on Query & Mutation | ||
ExecutorEvents::POST_EXECUTE => 'onPostExecute' //Post execute on Query & Mutation | ||
]; | ||
} | ||
/** | ||
* @param ExecutorEvent $event | ||
*/ | ||
public function onPreExecute(ExecutorEvent $event) | ||
{ | ||
// do something with the query or schema | ||
$query = $event->getQuery(); | ||
$schema = $event->getSchema(); | ||
} | ||
/** | ||
* @param ExecutorResultEvent $event | ||
*/ | ||
public function onPostExecute(ExecutorResultEvent $event) | ||
{ | ||
// do something with output result | ||
$result = $event->getResult(); | ||
} | ||
} | ||
``` | ||
|
||
#### Example 2: Bypass workspace permissions with Query/Mutation build events | ||
```php | ||
<?php | ||
namespace AppBundle\EventListener; | ||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\MutationEvents; | ||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\MutationTypeEvent; | ||
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\QueryTypeEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
class GraphqlListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
MutationEvents::PRE_BUILD => 'onMutationPreBuild', | ||
QueryEvents::PRE_BUILD => 'onQueryPreBuild' | ||
]; | ||
} | ||
/** | ||
* @param ExecutorEvent $event | ||
*/ | ||
public function onMutationPreBuild(MutationTypeEvent $event) | ||
{ | ||
$mutationType = $event->getMutationType(); | ||
$mutationType->setOmitPermissionCheck(true); //omit permission check for mutations | ||
} | ||
/** | ||
* @param ExecutorResultEvent $event | ||
*/ | ||
public function onQueryPreBuild(QueryTypeEvent $event) | ||
{ | ||
$queryType = $event->getQueryType(); | ||
$queryType->setOmitPermissionCheck(true); //omit permission check for queries | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Enterprise License (PEL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PEL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\DataHubBundle\Event\GraphQL; | ||
|
||
final class ExecutorEvents | ||
{ | ||
/** | ||
* @Event("Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorEvent") | ||
* | ||
* @var string | ||
*/ | ||
const PRE_EXECUTE = 'pimcore.datahub.graphql.executor.preExecute'; | ||
|
||
/** | ||
* @Event("Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\ExecutorResultEvent") | ||
* | ||
* @var string | ||
*/ | ||
const POST_EXECUTE = 'pimcore.datahub.graphql.executor.postExecute'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
/** | ||
* Pimcore | ||
* | ||
* This source file is available under two different licenses: | ||
* - GNU General Public License version 3 (GPLv3) | ||
* - Pimcore Enterprise License (PEL) | ||
* Full copyright and license information is available in | ||
* LICENSE.md which is distributed with this source code. | ||
* | ||
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org) | ||
* @license http://www.pimcore.org/license GPLv3 and PEL | ||
*/ | ||
|
||
namespace Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model; | ||
|
||
use GraphQL\Type\Schema; | ||
use Pimcore\Event\Traits\RequestAwareTrait; | ||
use Pimcore\Event\Traits\ResponseAwareTrait; | ||
use Symfony\Component\EventDispatcher\Event; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class ExecutorEvent extends Event | ||
{ | ||
use RequestAwareTrait; | ||
use ResponseAwareTrait; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $request; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $query; | ||
|
||
/** | ||
* @var Schema | ||
*/ | ||
protected $schema; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $context; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getRequest() | ||
{ | ||
return $this->request; | ||
} | ||
|
||
/** | ||
* @param string $request | ||
*/ | ||
public function setRequest(string $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* @return Schema | ||
*/ | ||
public function getSchema() | ||
{ | ||
return $this->schema; | ||
} | ||
|
||
/** | ||
* @param Schema $schema | ||
*/ | ||
public function setSchema(Schema $schema) | ||
{ | ||
$this->schema = $schema; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getContext() | ||
{ | ||
return $this->context; | ||
} | ||
|
||
/** | ||
* @param array $context | ||
*/ | ||
public function setContext(array $context) | ||
{ | ||
$this->context = $context; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getQuery() | ||
{ | ||
return $this->query; | ||
} | ||
|
||
/** | ||
* @param string $query | ||
*/ | ||
public function setQuery($query) | ||
{ | ||
$this->query = $query; | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* @param array $query | ||
* @param Schema $schema | ||
* @param array $context | ||
*/ | ||
public function __construct(Request $request, $query, Schema $schema, $context) | ||
{ | ||
$this->request = $request; | ||
$this->query = $query; | ||
$this->schema = $schema; | ||
$this->context = $context; | ||
} | ||
} |
Oops, something went wrong.