|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\GraphQl\Resolver; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
|
17
|
|
|
use ApiPlatform\Core\Exception\ItemNotFoundException; |
|
18
|
|
|
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer; |
|
19
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
|
20
|
|
|
use ApiPlatform\Core\Security\ResourceAccessCheckerInterface; |
|
21
|
|
|
use ApiPlatform\Core\Util\ClassInfoTrait; |
|
22
|
|
|
use GraphQL\Error\Error; |
|
23
|
|
|
use GraphQL\Type\Definition\ResolveInfo; |
|
24
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Creates a function retrieving an item to resolve a GraphQL query. |
|
28
|
|
|
* |
|
29
|
|
|
* @experimental |
|
30
|
|
|
* |
|
31
|
|
|
* @author Alan Poulain <[email protected]> |
|
32
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
33
|
|
|
*/ |
|
34
|
|
|
final class ItemResolver |
|
35
|
|
|
{ |
|
36
|
|
|
use ClassInfoTrait; |
|
37
|
|
|
use ResourceAccessCheckerTrait; |
|
38
|
|
|
|
|
39
|
|
|
private $iriConverter; |
|
40
|
|
|
private $resourceAccessChecker; |
|
41
|
|
|
private $normalizer; |
|
42
|
|
|
private $resourceMetadataFactory; |
|
43
|
|
|
|
|
44
|
|
|
public function __construct(IriConverterInterface $iriConverter, NormalizerInterface $normalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->iriConverter = $iriConverter; |
|
47
|
|
|
$this->normalizer = $normalizer; |
|
48
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
|
49
|
|
|
$this->resourceAccessChecker = $resourceAccessChecker; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function __invoke($source, $args, $context, ResolveInfo $info) |
|
53
|
|
|
{ |
|
54
|
|
|
// Data already fetched and normalized (field or nested resource) |
|
55
|
|
|
if (isset($source[$info->fieldName])) { |
|
56
|
|
|
return $source[$info->fieldName]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!isset($args['id'])) { |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// TODO: initialize the EagerLoading extension |
|
64
|
|
|
try { |
|
65
|
|
|
$item = $this->iriConverter->getItemFromIri($args['id']); |
|
66
|
|
|
} catch (ItemNotFoundException $e) { |
|
67
|
|
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$resourceClass = $this->getObjectClass($item); |
|
71
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
|
72
|
|
|
$this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $item); |
|
73
|
|
|
|
|
74
|
|
View Code Duplication |
if (null !== $this->resourceAccessChecker) { |
|
|
|
|
|
|
75
|
|
|
$isGranted = $resourceMetadata->getGraphqlAttribute('query', 'access_control', null, true); |
|
76
|
|
|
if (null !== $isGranted && !$this->resourceAccessChecker->isGranted($resourceClass, $isGranted, ['object' => $item])) { |
|
77
|
|
|
throw Error::createLocatedError('Access Denied.', $info->fieldNodes, $info->path); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$normalizationContext = $resourceMetadata->getGraphqlAttribute('query', 'normalization_context', [], true); |
|
82
|
|
|
|
|
83
|
|
|
return $this->normalizer->normalize($item, ItemNormalizer::FORMAT, $normalizationContext + ['attributes' => $info->getFieldSelection(PHP_INT_MAX)]); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.