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\Stage; |
||
15 | |||
16 | use ApiPlatform\Core\Api\IriConverterInterface; |
||
17 | use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface; |
||
18 | use ApiPlatform\Core\DataProvider\SubresourceDataProviderInterface; |
||
19 | use ApiPlatform\Core\Exception\ItemNotFoundException; |
||
20 | use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer; |
||
21 | use ApiPlatform\Core\GraphQl\Serializer\SerializerContextBuilderInterface; |
||
22 | use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
||
23 | use ApiPlatform\Core\Util\ClassInfoTrait; |
||
24 | use GraphQL\Error\Error; |
||
25 | use GraphQL\Type\Definition\ResolveInfo; |
||
26 | |||
27 | /** |
||
28 | * Read stage of GraphQL resolvers. |
||
29 | * |
||
30 | * @experimental |
||
31 | * |
||
32 | * @author Alan Poulain <[email protected]> |
||
33 | */ |
||
34 | final class ReadStage implements ReadStageInterface |
||
35 | { |
||
36 | use ClassInfoTrait; |
||
37 | |||
38 | private $resourceMetadataFactory; |
||
39 | private $iriConverter; |
||
40 | private $collectionDataProvider; |
||
41 | private $subresourceDataProvider; |
||
42 | private $serializerContextBuilder; |
||
43 | private $nestingSeparator; |
||
44 | |||
45 | public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, IriConverterInterface $iriConverter, ContextAwareCollectionDataProviderInterface $collectionDataProvider, SubresourceDataProviderInterface $subresourceDataProvider, SerializerContextBuilderInterface $serializerContextBuilder, string $nestingSeparator) |
||
46 | { |
||
47 | $this->resourceMetadataFactory = $resourceMetadataFactory; |
||
48 | $this->iriConverter = $iriConverter; |
||
49 | $this->collectionDataProvider = $collectionDataProvider; |
||
50 | $this->subresourceDataProvider = $subresourceDataProvider; |
||
51 | $this->serializerContextBuilder = $serializerContextBuilder; |
||
52 | $this->nestingSeparator = $nestingSeparator; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function __invoke(?string $resourceClass, ?string $rootClass, string $operationName, array $context) |
||
59 | { |
||
60 | $resourceMetadata = $resourceClass ? $this->resourceMetadataFactory->create($resourceClass) : null; |
||
61 | if ($resourceMetadata && !$resourceMetadata->getGraphqlAttribute($operationName, 'read', true, true)) { |
||
62 | return $context['is_collection'] ? [] : null; |
||
63 | } |
||
64 | |||
65 | $args = $context['args']; |
||
66 | /** @var ResolveInfo $info */ |
||
67 | $info = $context['info']; |
||
68 | |||
69 | $normalizationContext = $this->serializerContextBuilder->create($resourceClass, $operationName, $context, true); |
||
70 | |||
71 | if (!$context['is_collection']) { |
||
72 | $identifier = $this->getIdentifier($context); |
||
73 | $item = $this->getItem($identifier, $normalizationContext); |
||
74 | |||
75 | if ($identifier && $context['is_mutation']) { |
||
76 | if (null === $item) { |
||
77 | throw Error::createLocatedError(sprintf('Item "%s" not found.', $args['input']['id']), $info->fieldNodes, $info->path); |
||
78 | } |
||
79 | |||
80 | if ($resourceClass !== $this->getObjectClass($item)) { |
||
81 | throw Error::createLocatedError(sprintf('Item "%s" did not match expected type "%s".', $args['input']['id'], $resourceMetadata->getShortName()), $info->fieldNodes, $info->path); |
||
0 ignored issues
–
show
|
|||
82 | } |
||
83 | } |
||
84 | |||
85 | return $item; |
||
86 | } |
||
87 | |||
88 | if (null === $rootClass) { |
||
89 | return []; |
||
90 | } |
||
91 | |||
92 | $normalizationContext['filters'] = $this->getNormalizedFilters($args); |
||
93 | |||
94 | $source = $context['source']; |
||
95 | if (isset($source[$rootProperty = $info->fieldName], $source[ItemNormalizer::ITEM_IDENTIFIERS_KEY])) { |
||
96 | $rootResolvedFields = $source[ItemNormalizer::ITEM_IDENTIFIERS_KEY]; |
||
97 | $subresourceCollection = $this->getSubresource($rootClass, $rootResolvedFields, $rootProperty, $resourceClass, $normalizationContext, $operationName); |
||
98 | if (!is_iterable($subresourceCollection)) { |
||
99 | throw new \UnexpectedValueException('Expected subresource collection to be iterable'); |
||
100 | } |
||
101 | |||
102 | return $subresourceCollection; |
||
103 | } |
||
104 | |||
105 | return $this->collectionDataProvider->getCollection($resourceClass, $operationName, $normalizationContext); |
||
106 | } |
||
107 | |||
108 | private function getIdentifier(array $context): ?string |
||
109 | { |
||
110 | $args = $context['args']; |
||
111 | |||
112 | if ($context['is_mutation']) { |
||
113 | return $args['input']['id'] ?? null; |
||
114 | } |
||
115 | |||
116 | return $args['id'] ?? null; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return object|null |
||
121 | */ |
||
122 | private function getItem(?string $identifier, array $normalizationContext) |
||
123 | { |
||
124 | if (null === $identifier) { |
||
125 | return null; |
||
126 | } |
||
127 | |||
128 | try { |
||
129 | $item = $this->iriConverter->getItemFromIri($identifier, $normalizationContext); |
||
130 | } catch (ItemNotFoundException $e) { |
||
131 | return null; |
||
132 | } |
||
133 | |||
134 | return $item; |
||
135 | } |
||
136 | |||
137 | private function getNormalizedFilters(array $args): array |
||
138 | { |
||
139 | $filters = $args; |
||
140 | |||
141 | foreach ($filters as $name => $value) { |
||
142 | if (\is_array($value)) { |
||
143 | if (strpos($name, '_list')) { |
||
144 | $name = substr($name, 0, \strlen($name) - \strlen('_list')); |
||
145 | } |
||
146 | $filters[$name] = $this->getNormalizedFilters($value); |
||
147 | } |
||
148 | |||
149 | if (\is_string($name) && strpos($name, $this->nestingSeparator)) { |
||
150 | // Gives a chance to relations/nested fields. |
||
151 | $filters[str_replace($this->nestingSeparator, '.', $name)] = $value; |
||
152 | } |
||
153 | } |
||
154 | |||
155 | return $filters; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @return iterable|object|null |
||
160 | */ |
||
161 | private function getSubresource(string $rootClass, array $rootResolvedFields, string $rootProperty, string $subresourceClass, array $normalizationContext, string $operationName) |
||
162 | { |
||
163 | $resolvedIdentifiers = []; |
||
164 | $rootIdentifiers = array_keys($rootResolvedFields); |
||
165 | foreach ($rootIdentifiers as $rootIdentifier) { |
||
166 | $resolvedIdentifiers[] = [$rootIdentifier, $rootClass]; |
||
167 | } |
||
168 | |||
169 | return $this->subresourceDataProvider->getSubresource($subresourceClass, $rootResolvedFields, $normalizationContext + [ |
||
170 | 'property' => $rootProperty, |
||
171 | 'identifiers' => $resolvedIdentifiers, |
||
172 | 'collection' => true, |
||
173 | ], $operationName); |
||
174 | } |
||
175 | } |
||
176 |
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.