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 ResourceAccessCheckerTrait; |
|
|
|
|
37
|
|
|
|
38
|
|
|
private $iriConverter; |
39
|
|
|
private $resourceAccessChecker; |
40
|
|
|
private $normalizer; |
41
|
|
|
private $resourceMetadataFactory; |
42
|
|
|
|
43
|
|
|
public function __construct(IriConverterInterface $iriConverter, NormalizerInterface $normalizer, ResourceMetadataFactoryInterface $resourceMetadataFactory, ResourceAccessCheckerInterface $resourceAccessChecker = null) |
44
|
|
|
{ |
45
|
|
|
$this->iriConverter = $iriConverter; |
46
|
|
|
$this->normalizer = $normalizer; |
47
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
48
|
|
|
$this->resourceAccessChecker = $resourceAccessChecker; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function __invoke($source, $args, $context, ResolveInfo $info) |
52
|
|
|
{ |
53
|
|
|
// Data already fetched and normalized (field or nested resource) |
54
|
|
|
if (isset($source[$info->fieldName])) { |
55
|
|
|
return $source[$info->fieldName]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (!isset($args['id'])) { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$baseNormalizationContext = ['attributes' => $info->getFieldSelection(PHP_INT_MAX)]; |
63
|
|
|
try { |
64
|
|
|
$item = $this->iriConverter->getItemFromIri($args['id'], $baseNormalizationContext); |
65
|
|
|
} catch (ItemNotFoundException $e) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$resourceClass = $this->getObjectClass($item); |
70
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
71
|
|
|
$this->canAccess($this->resourceAccessChecker, $resourceMetadata, $resourceClass, $info, $item, 'query'); |
72
|
|
|
|
73
|
|
|
$normalizationContext = $resourceMetadata->getGraphqlAttribute('query', 'normalization_context', [], true); |
74
|
|
|
|
75
|
|
|
return $this->normalizer->normalize($item, ItemNormalizer::FORMAT, $normalizationContext + $baseNormalizationContext); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|