src/EventSubscriber/BuildReportRangQuerySubscriber.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Service\BuildReportService;
  4. use GraphQL\Type\Definition\Type;
  5. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\MutationTypeEvent;
  6. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\QueryTypeEvent;
  7. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\MutationEvents;
  8. use Pimcore\Bundle\DataHubBundle\Event\GraphQL\QueryEvents;
  9. use Pimcore\Bundle\DataHubBundle\GraphQL\ElementDescriptor;
  10. use Pimcore\Bundle\DataHubBundle\GraphQL\FieldHelper\DataObjectFieldHelper;
  11. use Pimcore\Model\DataObject\ClassDefinition;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class BuildReportRangQuerySubscriber implements EventSubscriberInterface
  14. {
  15.     public static function getSubscribedEvents()
  16.     {
  17.         return [
  18.             QueryEvents::PRE_BUILD => 'onPreBuild',
  19.         ];
  20.     }
  21.     /**
  22.      * @param MutationTypeEvent $event
  23.      *
  24.      * @throws \Exception
  25.      */
  26.     public function onPreBuild(QueryTypeEvent $event)
  27.     {
  28.         $config $event->getConfig();
  29.         $outputType = new \GraphQL\Type\Definition\ObjectType([
  30.                 'name' => "BuildReportRank",
  31.                 'fields' => [
  32.                     'Rank' => [
  33.                         'type' => \GraphQL\Type\Definition\Type::string(),
  34.                         'resolve' => function ($source$args$context\GraphQL\Type\Definition\ResolveInfo $info) {
  35.                             $db \Pimcore\Db::get();
  36.                             $rank $db->fetchAll(
  37.                                 'SELECT o_id, TotalScore, FIND_IN_SET( TotalScore, (    
  38.                                 SELECT GROUP_CONCAT( TotalScore
  39.                                 ORDER BY TotalScore DESC ) 
  40.                                 FROM object_BuildReport )
  41.                                 ) AS LeaderBoardPosition
  42.                                 FROM object_BuildReport
  43.                                 WHERE o_id =  ?',
  44.                                 [$source['buildReportId']]
  45.                             );
  46.                             if (empty($rank)) {
  47.                                 return null;
  48.                             }
  49.                             return $rank[0]['LeaderBoardPosition'];
  50.                         }
  51.                     ]
  52.                 ]
  53.             ]
  54.         );
  55.         $operation = [
  56.             'type' => $outputType,
  57.             'args' => ['id' => ['type' => Type::nonNull(Type::int())]],
  58.             'resolve' => function ($source$args$context\GraphQL\Type\Definition\ResolveInfo $info) {
  59.                 return ['buildReportId' => $args['id']];
  60.             }
  61.         ];
  62.         $config['fields']['getBuildReportRank'] = $operation;
  63.         $event->setConfig($config);
  64.     }
  65. }