1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Filter\Resolver; |
12
|
|
|
|
13
|
|
|
use Doctrine\Common\Persistence\Mapping\MappingException; |
14
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
15
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\ClassAwareDefinitionInterface; |
17
|
|
|
use Ynlo\GraphQLBundle\Definition\ExecutableDefinitionInterface; |
18
|
|
|
use Ynlo\GraphQLBundle\Definition\ObjectDefinitionInterface; |
19
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\Endpoint; |
20
|
|
|
use Ynlo\GraphQLBundle\Filter\Common\NodeFilter; |
21
|
|
|
use Ynlo\GraphQLBundle\Filter\FilterResolverInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Resolve filters automatically based on GraphQL field definitions directly related to Entity columns |
25
|
|
|
*/ |
26
|
|
|
class DoctrineORMFilterResolver implements FilterResolverInterface |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var EntityManagerInterface |
30
|
|
|
*/ |
31
|
|
|
private $manager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* DoctrineORMFilterResolver constructor. |
35
|
|
|
* |
36
|
|
|
* @param EntityManagerInterface $manager |
37
|
|
|
*/ |
38
|
1 |
|
public function __construct(EntityManagerInterface $manager) |
39
|
|
|
{ |
40
|
1 |
|
$this->manager = $manager; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
1 |
|
public function resolve(ExecutableDefinitionInterface $executableDefinition,ObjectDefinitionInterface $node, Endpoint $endpoint): array |
47
|
|
|
{ |
48
|
1 |
|
$class = $node->getClass(); |
49
|
1 |
|
$filters = []; |
50
|
|
|
try { |
51
|
1 |
|
if (!$class || !($metaData = $this->manager->getClassMetadata($class))) { |
52
|
|
|
return $filters; |
53
|
|
|
} |
54
|
|
|
|
55
|
1 |
|
foreach ($node->getFields() as $field) { |
56
|
1 |
|
if (!$metaData->hasField($field->getName()) |
57
|
1 |
|
&& !$metaData->hasAssociation($field->getName()) |
58
|
1 |
|
&& !$metaData->hasField($field->getOriginName()) |
59
|
1 |
|
&& !$metaData->hasAssociation($field->getOriginName())) { |
60
|
1 |
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
//ignore ID |
64
|
1 |
|
if ($field->getName() === 'id') { |
65
|
1 |
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
$filter = FilterUtil::createFilter($endpoint, $field); |
69
|
|
|
|
70
|
1 |
|
if (NodeFilter::class === $filter->resolver) { |
71
|
|
|
try { |
72
|
|
|
/** @var ClassAwareDefinitionInterface $relatedNode */ |
73
|
1 |
|
$relatedNode = $endpoint->getType($field->getType()); |
74
|
1 |
|
$relatedEntity = $relatedNode->getClass(); |
75
|
1 |
|
if ($this->manager->getClassMetadata($relatedEntity)) { |
76
|
1 |
|
$associationType = null; |
77
|
|
|
|
78
|
1 |
|
if ($metaData->hasAssociation($field->getName())) { |
79
|
1 |
|
$associationType = $metaData->getAssociationMapping($field->getName())['type'] ?? null; |
|
|
|
|
80
|
|
|
} elseif ($metaData->hasAssociation($field->getOriginName())) { |
81
|
|
|
$associationType = $metaData->getAssociationMapping($field->getOriginName())['type'] ?? null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
//ignore filters by records only related to one record |
85
|
1 |
|
if (\in_array($associationType, [ClassMetadataInfo::ONE_TO_ONE, ClassMetadataInfo::ONE_TO_MANY], true)) { |
86
|
1 |
|
continue; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} catch (MappingException $exception) { |
90
|
|
|
//ignore |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
if ($filter->resolver) { |
95
|
1 |
|
$filters[] = $filter; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $filters; |
100
|
|
|
} catch (MappingException $exception) { |
101
|
|
|
return []; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.