Skip to content

Latest commit

 

History

History
162 lines (121 loc) · 3.78 KB

index.md

File metadata and controls

162 lines (121 loc) · 3.78 KB

Events

Events are a way to hook into the execution flow. This bundle provides various events from executor and context value initialization, executor result formatting (data, errors, extensions), errors/warnings formatting.

With this in mind we now turn to explain each one of them.

Executor context value

Event: graphql.executor.context

This event can be listen to initialize executor contextValue argument.

Executor initialisation

Event: graphql.pre_executor

This event can be listen to initialize executor execute arguments (schema, requestString, rootValue, contextValue, variableValue, operationName).

For example:

  • Initializing rootValue with the current user (we assume that user is fully authenticated)

    App\EventListener\RootValueInitialization:
        tags:
            - { name: kernel.event_listener, event: graphql.pre_executor, method: onPreExecutor }
    <?php
    
    namespace App\EventListener;
    
    use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    
    class RootValueInitialization
    {
      private $token;
    
      public function __construct(TokenInterface $token)
      {
           $this->token = $token;
      }
    
      public function onPreExecutor(ExecutorArgumentsEvent $event)
      {
          $event->setRootValue($this->token->getUser());
      }
    }

Executor result

Event: graphql.post_executor

This event can be listen to set custom error handling and formatting, it can also be use to add information to extensions section.

For example:

  • How to add credits entry in extensions section

    App\EventListener\Credits:
        tags:
            - { name: kernel.event_listener, event: graphql.post_executor, method: onPostExecutor }
    <?php
    
    namespace App\EventListener;
    
    use Overblog\GraphQLBundle\Event\ExecutorResultEvent;
    
    class Credits
    {
      public function onPostExecutor(ExecutorResultEvent $event)
      {
          $event->getResult()->extensions['credits'] = 'This api was powered by "OverblogGraphQLBundle".';
      }
    }

    result: