/
ObjectMetadataType.php
91 lines (77 loc) · 3.03 KB
/
ObjectMetadataType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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\ObjectType;
use GraphQL\Type\Definition\Type;
use Pimcore\Bundle\DataHubBundle\GraphQL\ClassTypeDefinitions;
use Pimcore\Bundle\DataHubBundle\GraphQL\Resolver\ObjectMetadata;
use Pimcore\Bundle\DataHubBundle\GraphQL\Service;
use Pimcore\Bundle\DataHubBundle\GraphQL\Traits\ServiceTrait;
use Pimcore\Model\DataObject\ClassDefinition\Data;
use Pimcore\Model\DataObject\Fieldcollection\Definition as FieldcollectionDefinition;
use Pimcore\Model\DataObject\Objectbrick\Definition as ObjectbrickDefinition;
class ObjectMetadataType extends ObjectType
{
use ServiceTrait;
protected $class;
/** @var Data */
protected $fieldDefinition;
/**
* ObjectMetadataType constructor.
*
* @param Service $graphQlService
* @param Data|null $fieldDefinition
* @param null $class
* @param array $config
*/
public function __construct(Service $graphQlService, Data $fieldDefinition = null, $class = null, $config = [])
{
$this->setGraphQLService($graphQlService);
$this->class = $class;
$this->fieldDefinition = $fieldDefinition;
if ($class instanceof ObjectbrickDefinition) {
$config['name'] = 'objectbrick_' . $class->getKey() . '_' . $fieldDefinition->getName();
} elseif ($class instanceof FieldcollectionDefinition) {
$config['name'] = 'fieldcollection_' . $class->getKey() . '_' . $fieldDefinition->getName();
} else {
$config['name'] = 'object_' . $class->getName() . '_' . $fieldDefinition->getName();
}
$this->build($config);
parent::__construct($config);
}
/**
* @param array $config
*/
public function build(&$config)
{
$fieldHelper = $this->getGraphQlService()->getObjectFieldHelper();
/** @var Data\AdvancedManyToManyObjectRelation $fieldDefinition */
$fieldDefinition = $this->fieldDefinition;
$class = $this->class;
$className = $fieldDefinition->getAllowedClassId();
$elementTypeDefinition = ClassTypeDefinitions::get($className);
$resolver = new ObjectMetadata($fieldDefinition, $class, $fieldHelper);
$fields = ['element' =>
[
'type' => $elementTypeDefinition,
'resolve' => [$resolver, 'resolveElement']
],
'metadata' => [
'type' => Type::listOf(new ElementMetadataKeyValuePairType()),
'resolve' => [$resolver, 'resolveMetadata']
]];
$config['fields'] = $fields;
return;
}
}