<?php
namespace App\EventSubscriber;
use App\Service\BuildReportService;
use GraphQL\Type\Definition\Type;
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\MutationTypeEvent;
use Pimcore\Bundle\DataHubBundle\Event\GraphQL\MutationEvents;
use Pimcore\Bundle\DataHubBundle\GraphQL\ElementDescriptor;
use Pimcore\Bundle\DataHubBundle\GraphQL\FieldHelper\DataObjectFieldHelper;
use Pimcore\Model\DataObject\ClassDefinition;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BuildReportMutationSubscriber implements EventSubscriberInterface
{
/**
* @var BuildReportService
*/
protected $buildReportService;
/**
* @var DataObjectFieldHelper
*/
protected $dataObjectFieldHelper;
public function __construct(BuildReportService $buildReportService, DataObjectFieldHelper $dataObjectFieldHelper)
{
$this->buildReportService = $buildReportService;
$this->dataObjectFieldHelper = $dataObjectFieldHelper;
}
public static function getSubscribedEvents()
{
return [
MutationEvents::PRE_BUILD => 'onPreBuild',
];
}
/**
* @param MutationTypeEvent $event
*
* @throws \Exception
*/
public function onPreBuild(MutationTypeEvent $event)
{
$config = $event->getConfig();
$opName = "makeBuildReport";
$inputType = new \GraphQL\Type\Definition\InputObjectType([
'name' => "makeBuildReportInput",
'fields' => [
'buildingMaterialIds' => [
'type' => Type::listOf(Type::id())
],
'numberOfHouseHoldMembers' => [
'type' => Type::int()
],
'buildSquareFootage' => [
'type' => Type::int()
],
'budget' => [
'type' => Type::int()
],
'region' => [
'type' => Type::id()
],
]
]);
$operation = [
'type' => \Pimcore\Bundle\DataHubBundle\GraphQL\ClassTypeDefinitions::get(ClassDefinition::getByName('BuildReport')), // the result type
'args' => [
'input' => ['type' => $inputType],
], 'resolve' => function ($source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info) {
$inputData = $args['input'];
$buildReport = $this->buildReportService->createBuildReportFromMutationParams(
$inputData['buildingMaterialIds'],
$inputData['numberOfHouseHoldMembers'],
$inputData['buildSquareFootage'],
$inputData['budget'],
$inputData['region']
);
$data = new ElementDescriptor($buildReport);
$data['id'] = $buildReport->getId();
return $this->dataObjectFieldHelper->extractData($data, $buildReport, $args, $context, $info);
}
];
$config['fields'][$opName] = $operation;
$event->setConfig($config);
}
}