|
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\Type\Definition\ResolveInfo; |
|
23
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Creates a function retrieving an item to resolve a GraphQL query. |
|
27
|
|
|
* |
|
28
|
|
|
* @experimental |
|
29
|
|
|
* |
|
30
|
|
|
* @author Alan Poulain <[email protected]> |
|
31
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
32
|
|
|
*/ |
|
33
|
|
|
final class ItemResolver |
|
34
|
|
|
{ |
|
35
|
|
|
use ClassInfoTrait; |
|
36
|
|
|
use FieldsToAttributesTrait; |
|
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
|
|
|
$baseNormalizationContext = ['attributes' => $this->fieldsToAttributes($info)]; |
|
64
|
|
|
try { |
|
65
|
|
|
$item = $this->iriConverter->getItemFromIri($args['id'], $baseNormalizationContext); |
|
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, 'query'); |
|
73
|
|
|
|
|
74
|
|
|
$normalizationContext = $resourceMetadata->getGraphqlAttribute('query', 'normalization_context', [], true); |
|
75
|
|
|
|
|
76
|
|
|
return $this->normalizer->normalize($item, ItemNormalizer::FORMAT, $normalizationContext + $baseNormalizationContext); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|