Skip to content

Commit

Permalink
Add mutation datatype 'input quantity value' (#812)
Browse files Browse the repository at this point in the history
* Update 20_DataObject_Mutations.md

* Create InputQuantityValue.php

* Create InputQuantityValue.php

* Create InputQuantityValueInputType.php

* Update graphql.yml
  • Loading branch information
leitge authored Feb 1, 2024
1 parent 06e61ef commit 5456da0
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/10_GraphQL/07_Mutation/20_DataObject_Mutations.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Also check out the Pimcore's [data type documentation](https://pimcore.com/docs/
* Image
* ImageGallery
* Input
* Input Quantity Value
* Language
* Lastname
* Link
Expand Down
48 changes: 48 additions & 0 deletions src/GraphQL/DataObjectInputProcessor/InputQuantityValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* 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 PCL
*/

namespace Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectInputProcessor;

use GraphQL\Type\Definition\ResolveInfo;
use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
use Pimcore\Model\DataObject\Concrete;
use Pimcore\Model\DataObject\Fieldcollection\Data\AbstractData;

class InputQuantityValue extends Base
{
/**
* @param Concrete|AbstractData $object
* @param array $newValue
* @param array $args
* @param array $context
* @param ResolveInfo $info
*
* @throws \Exception
*/
public function process($object, $newValue, $args, $context, ResolveInfo $info)
{
$attribute = $this->getAttribute();
Service::setValue($object, $attribute, function ($container, $setter) use ($newValue) {
if ($newValue) {
$unit = \Pimcore\Model\DataObject\QuantityValue\Unit::getByAbbreviation($newValue['unit']);
$inputQuantityValue = new \Pimcore\Model\DataObject\Data\InputQuantityValue($newValue['value'], $unit);

return $container->$setter($inputQuantityValue);
}

return null;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* 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 PCL
*/

namespace Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectMutationFieldConfigGenerator;

class InputQuantityValue extends Base
{
/** {@inheritdoc } */
public function getGraphQlMutationFieldConfig($nodeDef, $class, $container = null, $params = [])
{
$processor = new \Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectInputProcessor\InputQuantityValue($nodeDef);
$processor->setGraphQLService($this->getGraphQlService());

return [
'arg' => $this->getGraphQlService()->getDataObjectTypeDefinition('input_quantity_value_input'),
'processor' => [$processor, 'process']
];
}
}
51 changes: 51 additions & 0 deletions src/GraphQL/DataObjectType/InputQuantityValueInputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* 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 PCL
*/

namespace Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType;

use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\Type;
use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;

class InputQuantityValueInputType extends InputObjectType
{
use ServiceTrait;

/**
* @param Service $graphQlService
* @param array $config
*/
public function __construct(Service $graphQlService, $config = ['name' => 'InputQuantityValueInput'])
{
$this->setGraphQLService($graphQlService);
$this->build($config);
parent::__construct($config);
}

/**
* @param array $config
*/
public function build(&$config)
{
$resolver = new \Pimcore\Bundle\DataHubBundle\GraphQL\Resolver\DataObject($this->getGraphQlService());
$resolver->setGraphQLService($this->getGraphQlService());

$config['fields'] = [
'value' => Type::string(),
'unit' => Type::string(),
];
}
}
10 changes: 10 additions & 0 deletions src/Resources/config/graphql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ services:
pimcore.datahub.graphql.dataobjecttype.elementdescriptor_input: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\ElementDescriptorInputType'
pimcore.datahub.graphql.dataobjecttype.geopoint_input: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\GeopointInputType'
pimcore.datahub.graphql.dataobjecttype.quantity_value_input: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\QuantityValueInputType'
pimcore.datahub.graphql.dataobjecttype.input_quantity_value_input: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\InputQuantityValueInputType'

pimcore.datahub.graphql.dataobjecttype.object_tree: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\ObjectTreeType'
pimcore.datahub.graphql.dataobjecttype._object_folder: '@Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\ObjectFolderType'
Expand Down Expand Up @@ -732,6 +733,10 @@ services:
tags:
- { name: pimcore.datahub.graphql.dataobjecttype, id: input_quantity_value }

Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\InputQuantityValueInputType:
tags:
- { name: pimcore.datahub.graphql.dataobjecttype, id: input_quantity_value_input }

Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectType\QuantityValueType:
tags:
- { name: pimcore.datahub.graphql.dataobjecttype, id: quantity_value }
Expand Down Expand Up @@ -1000,6 +1005,11 @@ services:
tags:
- { name: pimcore.datahub.graphql.dataobjectmutationtypegenerator, id: typegenerator_dataobjectmutationdatatype_quantityValue }

pimcore.datahub.graphql.dataobjectmutationtypegenerator_datatype_inputQuantityValue:
class: Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectMutationFieldConfigGenerator\InputQuantityValue
tags:
- { name: pimcore.datahub.graphql.dataobjectmutationtypegenerator, id: typegenerator_dataobjectmutationdatatype_inputQuantityValue }

pimcore.datahub.graphql.dataobjectmutationtypegenerator_datatype_table:
class: Pimcore\Bundle\DataHubBundle\GraphQL\DataObjectMutationFieldConfigGenerator\Table
tags:
Expand Down

0 comments on commit 5456da0

Please sign in to comment.