vendor/overblog/graphql-bundle/src/Validator/Formatter.php line 22

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Overblog\GraphQLBundle\Validator;
  4. use Overblog\GraphQLBundle\Event\ErrorFormattingEvent;
  5. use Overblog\GraphQLBundle\Validator\Exception\ArgumentsValidationException;
  6. /**
  7.  * Class Formatter.
  8.  *
  9.  * Adds validation errors to the response.
  10.  *
  11.  * @see https://github.com/overblog/GraphQLBundle/blob/master/docs/validation/index.md#error-messages
  12.  */
  13. class Formatter
  14. {
  15.     /**
  16.      * @param ErrorFormattingEvent $event
  17.      */
  18.     public function onErrorFormatting(ErrorFormattingEvent $event): void
  19.     {
  20.         $error $event->getError()->getPrevious();
  21.         if ($error instanceof ArgumentsValidationException) {
  22.             $validation = [];
  23.             foreach ($error->getViolations() as $violation) {
  24.                 $validation[$violation->getPropertyPath()][] = [
  25.                     'message' => $violation->getMessage(),
  26.                     'code' => $violation->getCode(),
  27.                 ];
  28.             }
  29.             $formattedError $event->getFormattedError();
  30.             $formattedError['extensions']['validation'] = $validation;
  31.         }
  32.     }
  33. }